Last active
December 20, 2015 06:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
この抽象化に名前はあるでしょうか? 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