通勤还用于更改引用类型的值,就像alter和ref-set一样,唯一的区别是,这也需要放在" dosync"块中。
(commute refname fun)
参数 -'refname'是保存参考值的变量的名称。 "fun"是用于更改引用类型的值的函数。
返回值 - 引用及其相应的新值。
以下程序显示了有关如何使用它的示例。
(ns clojure.examples.example (:gen-class)) (defn Example [] (def counter (ref 0)) (defn change [counter] (dosync (commute counter inc))) (change counter) (println @counter) (change counter) (println @counter)) (Example)
上面的程序产生以下输出。
链接:https://www.learnfk.comhttps://www.learnfk.com/clojure/clojure-commute.html
来源:LearnFk无涯教程网
1 2
这一章《Clojure - 参考值 - commute函数》你学到了什么?在下面做个笔记吧!做站不易,你的分享是对我们最大的支持