Clojure Ref、Var、Agent、Atom 之间的差异,并举例 [英] Clojure differences between Ref, Var, Agent, Atom, with examples

查看:18
本文介绍了Clojure Ref、Var、Agent、Atom 之间的差异,并举例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Clojure 很陌生,你们能给我解释一下真实世界的场景吗?我的意思是,在哪里使用 Ref、Var、Agent、Atom.我读过书,但是,仍然无法理解现实世界的例子.

I'm very new to Clojure, Can you guys give me explanation with real world scenarios. I mean, where to use Ref, Var, Agent, Atom. I read book, but, still couldn't understand the real world examples.

推荐答案

我强烈推荐Clojure 的乐趣"或Clojure 编程"来真正回答这个问题,我可以复制一个简短的动机片段对于每个:

I highly recommend "The Joy of Clojure" or "programming Clojure" for a real answer to this question, I can reproduce a short snip-it of the motivations for each:

首先观看关于身份概念的视频 和/或 在这里学习.

  • Refs 用于协调同步访问许多身份".
  • 原子用于对单个身份的非协调同步访问.
  • 代理用于对单个身份的非协调异步访问.
  • Var 用于具有共享默认值的线程本地独立身份.
  • Refs are for Coordinated Synchronous access to "Many Identities".
  • Atoms are for Uncoordinated synchronous access to a single Identity.
  • Agents are for Uncoordinated asynchronous access to a single Identity.
  • Vars are for thread local isolated identities with a shared default value.

协调访问用于两个身份需要一起更改时,经典示例是将钱从一个银行帐户转移到另一个帐户,它需要完全转移或根本不转移.

Coordinated access is used when two Identities needs to change together, the classic example is moving money from one bank account to another, it needs to either move completely or not at all.

非协调访问用于只有一个身份需要更新时,这是一种非常常见的情况.

Uncoordinated access is used when only one Identity needs to update, this is a very common case.

同步 访问用于预期调用要等到所有身份都确定后再继续时.

Synchronous access is used when the call is expected to wait until all Identities have settled before continuing.

异步访问是即发即忘",让身份在自己的时间达到新的状态.

Asynchronous access is "fire and forget" and let the Identity reach its new state in its own time.

这篇关于Clojure Ref、Var、Agent、Atom 之间的差异,并举例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆