Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法将字段访问器转换为函数?当我尝试这样做时,我真的很惊讶
(map .x [o1 o2])
但不得不这样做
(defn x [o] (.x o)) (map x [o1 o2])
这似乎是不必要的。有没有办法为您创建此功能?
使用匿名fn
fn
(map #(.x %) [o1 o2]) (map (fn [o] (.x o)) [o1 o2])
或者memfn - 我读过一个匿名fn的比这个更受欢迎。我会试着找到这篇文章。
(map (memfn x) [o1 o2])
编辑:Stu Halloway有话要说memfn
memfn
您可以编写自己的 wee 宏来生成匿名函数:
(defmacro field [m] `(fn [x#] (. x# ~m)))
然后,例如,
((field x) (java.awt.Point. 3 5)) ;3