Should Clojure circular data structures include constructions like ref?

Today I saw some links to binding nodes and circular data structures. I have not read some answers, and the solutions seem to use ref to return to the list title. One specific SO question showed the Haskell example, but I don't know Haskell well enough to find out if the example uses the Haskell ref equivalent.

Is there a way to make a Clojure data structure circular without using a ref link or similar design?

Thank.

+5
source share
3 answers

I just translated the Haskell example into Clojure:

user> (def alternates
          (letfn [(x [] (lazy-seq (cons 0 (y))))
                  (y [] (lazy-seq (cons 1 (x))))]
            (x)))
#'user/alternates
user> (take 7 alternates)
(0 1 0 1 0 1 0)

, . cycle letfn:

user> (take 7 (cycle [0 1]))
(0 1 0 1 0 1 0)
+5

, Clojure Clojure. , () , .

, :

  • Refs, atom ..
  • Java

, , Clojure.

+5

:

;; circular list operations
(defn rotate
   ([cl] (conj (into [](rest cl)) (first cl)))
   ([cl n] (nth (iterate rotate cl) (mod n (count cl)))))

, .

+3

All Articles