Ruby supports recursive arrays (that is, self-containing arrays):
Ruby支持递归数组(即自包含数组):
a = []
# => []
a << a
# => [[...]]
a.first == a
# => true
This is intrinsically cool, but what work can you do with it?
这本质上很酷,但你能用它做什么?
2 个解决方案
#1
43
A directed graph with undifferentiated edges could have each vertex represented simply as an array of the the vertices reachable from that vertex. If the graph had cycles, you would have a 'recursive array', especially if an edge could lead back to the same vertex.
有向图具有无差别的边,可以将每个顶点简单地表示为从该顶点可达的顶点的数组。如果图形有循环,你就会有一个“递归数组”,特别是如果一条边可以返回到同一个顶点。
For example, this graph:
...could be represented in code as:
例如,这个图表:……可以用代码表示为:
nodes = { a:[], b:[], c:[], d:[] }
nodes[:a] << nodes[:a]
nodes[:a] << nodes[:b]
nodes[:b] << nodes[:a]
nodes[:b] << nodes[:c]
p nodes
#=> {:a=>[[[...], []], [...]], :b=>[[[...], [...]], []], :c=>[], :d=>[]}
Usually the representation of each vertex would be more 'robust' (e.g. a class instance with properties for the name and array of outgoing edges), but it's not impossible to imagine a case where you wanted a very lightweight representation of your data (for very large graphs) and so needed to use a minimal representation like this.
通常每个顶点的表示会更健壮的(例如,一个类实例的属性名称和一系列的边),但它不是无法想象的情况,你想要一个非常轻量级的表示你的数据(非常大的图),因此需要使用这样的最小表示法。
#2
8
Ruby supports recursive arrays
Ruby支持递归的数组
To me the question is why should it not support it?
对我来说,问题是它为什么不支持它?
An Array is simply a collection of references. Should it check each element and throw an error if one of the refers to the collection itself, so prevent recursion or using it for graphs like Phrogz' example.
数组只是引用的集合。如果其中一个元素引用了集合本身,它应该检查每个元素并抛出一个错误,因此应该避免递归,或者像Phrogz的例子那样使用它。
So I don't think it's a feature, but if it would be, most languages I know has it, even Java.. Just use Object as Array elements.
所以我不认为它是一个特性,但如果是的话,我认识的大多数语言都有它,甚至Java。只需将对象用作数组元素。
#1
43
A directed graph with undifferentiated edges could have each vertex represented simply as an array of the the vertices reachable from that vertex. If the graph had cycles, you would have a 'recursive array', especially if an edge could lead back to the same vertex.
有向图具有无差别的边,可以将每个顶点简单地表示为从该顶点可达的顶点的数组。如果图形有循环,你就会有一个“递归数组”,特别是如果一条边可以返回到同一个顶点。
For example, this graph:
...could be represented in code as:
例如,这个图表:……可以用代码表示为:
nodes = { a:[], b:[], c:[], d:[] }
nodes[:a] << nodes[:a]
nodes[:a] << nodes[:b]
nodes[:b] << nodes[:a]
nodes[:b] << nodes[:c]
p nodes
#=> {:a=>[[[...], []], [...]], :b=>[[[...], [...]], []], :c=>[], :d=>[]}
Usually the representation of each vertex would be more 'robust' (e.g. a class instance with properties for the name and array of outgoing edges), but it's not impossible to imagine a case where you wanted a very lightweight representation of your data (for very large graphs) and so needed to use a minimal representation like this.
通常每个顶点的表示会更健壮的(例如,一个类实例的属性名称和一系列的边),但它不是无法想象的情况,你想要一个非常轻量级的表示你的数据(非常大的图),因此需要使用这样的最小表示法。
#2
8
Ruby supports recursive arrays
Ruby支持递归的数组
To me the question is why should it not support it?
对我来说,问题是它为什么不支持它?
An Array is simply a collection of references. Should it check each element and throw an error if one of the refers to the collection itself, so prevent recursion or using it for graphs like Phrogz' example.
数组只是引用的集合。如果其中一个元素引用了集合本身,它应该检查每个元素并抛出一个错误,因此应该避免递归,或者像Phrogz的例子那样使用它。
So I don't think it's a feature, but if it would be, most languages I know has it, even Java.. Just use Object as Array elements.
所以我不认为它是一个特性,但如果是的话,我认识的大多数语言都有它,甚至Java。只需将对象用作数组元素。