Clojure之间的区别Ref,Var,Agent,Atom,以及例子 [英] Clojure differences between Ref, Var, Agent, Atom, with examples

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

问题描述

我是Clojure的新手,你能给我解释真实世界的场景。我的意思是,在哪里使用Ref,Var,代理,原子。我读书,但是,仍然不能理解真实世界的例子。

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:

首先观看

start by watching this video on the notion of Identity


  • 参考协同同步访问许多身份。

  • 原子用于非协调同步访问单个身份。


  • 变量是用于具有共享默认值的线程本地隔离标识

  • 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 need to be changes together, the classic example being 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 where the call expects to wait until all the identities are 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天全站免登陆