Clojure 中的 commute函数

首页 / Clojure入门教程 / Clojure 中的 commute函数

通勤还用于更改引用类型的值,就像alter和ref-set一样,唯一的区别是,这也需要放在" dosync"块中。

commute - 语法

(commute refname fun)

参数      -'refname'是保存参考值的变量的名称。 "fun"是用于更改引用类型的值的函数。

返回值  - 引用及其相应的新值。

commute - 示例

以下程序显示了有关如何使用它的示例。

(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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

趣谈网络协议 -〔刘超〕

程序员进阶攻略 -〔胡峰〕

零基础学Java -〔臧萌〕

SQL必知必会 -〔陈旸〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

Serverless入门课 -〔蒲松洋(秦粤)〕

程序员的测试课 -〔郑晔〕

深入剖析Java新特性 -〔范学雷〕

AI 应用实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)