怎么都掉!以及在这里工作的 mmap 函数?
how are swap! and the mmap function working here?
这是 Reagent project 的片段。查看 complete-all
和 clear-done
,我明白重点是换出修改后的地图。我不明白这是怎么做到的。 mmap
的定义调用了 3 个参数 — 而 complete-all
似乎用两个参数调用它,即 map
和 #(assoc-in % [1 :done] v)
。 clear-done
调用 remove
和 #(get-in % [1 :done])
。我尝试使用 repl 进行实验,但无法使 requires
正常工作。
(ns todomvc.core
(:require [reagent.core :as r]))
(defonce todos (r/atom (sorted-map)))
(defonce counter (r/atom 0))
(defn add-todo [text]
(let [id (swap! counter inc)]
(swap! todos assoc id {:id id :title text :done false})))
(defn toggle [id] (swap! todos update-in [id :done] not))
(defn save [id title] (swap! todos assoc-in [id :title] title))
(defn delete [id] (swap! todos dissoc id))
(defn mmap [m f a] (->> m (f a) (into (empty m))))
(defn complete-all [v] (swap! todos mmap map #(assoc-in % [1 :done] v)))
(defn clear-done [] (swap! todos mmap remove #(get-in % [1 :done])))
现有地图作为函数的第一个参数传递。当所有其他方法都失败时...
这是 Reagent project 的片段。查看 complete-all
和 clear-done
,我明白重点是换出修改后的地图。我不明白这是怎么做到的。 mmap
的定义调用了 3 个参数 — 而 complete-all
似乎用两个参数调用它,即 map
和 #(assoc-in % [1 :done] v)
。 clear-done
调用 remove
和 #(get-in % [1 :done])
。我尝试使用 repl 进行实验,但无法使 requires
正常工作。
(ns todomvc.core
(:require [reagent.core :as r]))
(defonce todos (r/atom (sorted-map)))
(defonce counter (r/atom 0))
(defn add-todo [text]
(let [id (swap! counter inc)]
(swap! todos assoc id {:id id :title text :done false})))
(defn toggle [id] (swap! todos update-in [id :done] not))
(defn save [id title] (swap! todos assoc-in [id :title] title))
(defn delete [id] (swap! todos dissoc id))
(defn mmap [m f a] (->> m (f a) (into (empty m))))
(defn complete-all [v] (swap! todos mmap map #(assoc-in % [1 :done] v)))
(defn clear-done [] (swap! todos mmap remove #(get-in % [1 :done])))
现有地图作为函数的第一个参数传递。当所有其他方法都失败时...