From 454643675f260e5c681b69129a29165a0aa2c2b3 Mon Sep 17 00:00:00 2001 From: Felisp Date: Fri, 13 Sep 2024 21:17:23 +0200 Subject: [PATCH] Add fkmap and vectorize macro --- src/rss_thread_watch/utils.clj | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/rss_thread_watch/utils.clj b/src/rss_thread_watch/utils.clj index db53c12..afab99f 100644 --- a/src/rss_thread_watch/utils.clj +++ b/src/rss_thread_watch/utils.clj @@ -47,6 +47,11 @@ ~x result#))) +(defmacro vectorize + "If arg is not a vector, put into vector, otherwise return it" + [v] + (if (vector? v) v [v])) + ;; ===== Generic functions ==== (defn indices @@ -71,13 +76,25 @@ (defn fmap "Applies function [f] to every key and value in map [m] - Function signature should be (f [key value])." + Function signature should be (f [key value]). + Key stays unchanged" [f m] (into (empty m) (for [[key val] m] [key (f key val)]))) +(defn fkmap + ;; I am horrible with docstrings, I don't deny that + "Applies function [f] to every key and value in map [m] + Function signature should be (f [key value]). + Unlike fmap, you can change key too, so return both {key value} in map" + [f m] + (into + (empty m) + (for [[key val] m] + (f key val)))) + (defn expand-home "Expands ~ to home directory" ;;modified from sauce: https://stackoverflow.com/questions/29585928/how-to-substitute-path-to-home-for