Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active December 20, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohyama/6089899 to your computer and use it in GitHub Desktop.
Save kohyama/6089899 to your computer and use it in GitHub Desktop.
マップの最下層の値達に他の引数と共に関数を適用する. Apply a function to values in lowest levels of a map with other arguments
(require '[clojure.test :refer (with-test is run-tests)])
(with-test
(defn mapf [f m & args]
((fn g [n]
(if (map? n)
(into {} (map (fn [[k v]] [k (g v)]) n))
(apply f n args)))
m))
(is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
{:a {:b 9 :c 16} :d 25}))
(is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
{:a {:b 10 :c 17} :d 26})))
@kohyama
Copy link
Author

kohyama commented Jul 26, 2013

この抽象化に名前はあるでしょうか? Does this abstraction have any existing name?
似たようなことをする既存の関数/マクロはあるでしょうか? Is there any function/macro to do similar things?

その他, 実装改良などご意見なんでも, この GIST のコメントまたは下記の Google グループのスレッドで, お聞かせください. Please let me hear any opinion such as an idea to improve this implementation as comments of this GIST or at a thread of Google Group below.

https://groups.google.com/forum/?hl=ja&fromgroups#!topic/clojure/HHEpVEoKELE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment