是否可以在Clojure中创建循环引用? [英] Is it possible to create circular references in Clojure?

查看:166
本文介绍了是否可以在Clojure中创建循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忽略本机互操作和瞬变,是否可以在Clojure中创建包含直接循环引用的任何数据结构?



看起来不可变的数据结构只能包含对其自身的先前版本的引用。是否有任何Clojure API可以创建一个引用自身的新数据结构?



Scheme具有允许创建相互递归结构的letrec形式,就我可以告诉,Clojure没有什么类似的。



这个问题是关于移植Clojure到iOS - 没有垃圾收集,但有参考计数。

解决方案

您可以通过在数据结构中放置某种形式的引用来创建一个循环引用,

一个简单的例子:

 ($ a 


(a)

这将创建一个包含一个元素的列表,它是一个返回列表的原子。


Ignoring native interop and transients, is it possible to create any data structures in Clojure that contain direct circular references ?

It would seem that immutable data structures can only ever contain references to previous versions of themselves. Are there any Clojure APIs that could create a new data structure that has a reference to itself ?

Scheme has the letrec form which allows mutually recursive structures to be created - but, as far as I can tell, Clojure does not have anything similar.

This question is related to porting Clojure to iOS - which does not have garbage collection, but does have reference counting.

解决方案

You can create a circular reference very easily by putting some form of reference inside a data structure, then updating the reference to point back to the overall structure.

A trivial example:

(def a [(atom nil)])

(reset! (first a) a)

This will create a list with one element, which is an atom that points back at the list.

这篇关于是否可以在Clojure中创建循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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