2009-06-24 31 views
5

Farklı referanslar için bir vektör oluşturmanın en kolay yolu nedir?Referanslar Clojure Vector of

listesini döndürür (repeat 5 (ref nil)) kullanarak, ama hepsi aynı ref referans alır:

user=> (repeat 5 (ref nil)) 
(#<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<R 
[email protected]: nil>) 

aynı sonuç (replicate 5 (ref nil)) ile:

user=> (replicate 5 (ref nil)) 
(#<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> 
#<[email protected]: nil>) 

cevap

4

Tamam, bu oldukça brüt, ama çalışır:

user=> (map (fn [_] (ref nil)) (range 5)) 
(#<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil>) 

Bu, bir LazySeq değerini döndürür; Vektör, o zaman sadece kullanın: daha sonra

user=> (vec (map (fn [_] (ref nil)) (range 5))) 
[#<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil> #<[email protected]: nil>]