Skip to content

Instantly share code, notes, and snippets.

@kohyama kohyama/mapf.clj
Last active Dec 20, 2015

Embed
What would you like to do?
マップの最下層の値達に他の引数と共に関数を適用する. 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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.