Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure recursion and a lazy sequence

Tags:

clojure

Ok, I'm a bit stuck on this one, can I actually do what I'm trying to do with this part of the code below:

(recur (conj (get-links (first links)) (rest links))))

get-links returns a sequence of urls which is fed into the initial process-links call then should recurse.

The first link i feed in works, but then the second link where I'm trying to conj one sequence on to another gives me the following error.

"Clojure.lang.LazySeq@xxxxxxx"

Now I'm wondering, is this conj'ing the reference to the instruction to generate the "rest" (rest links) of the un-evaluated sequence?

(defn process-links
  [links]
  (if (not (empty? links))
    (do
      (if (not (is-working (first links)))
        (do
          (println (str (first links) " is not working"))
          (recur (rest links)))
        (do
          (println (str (first links) " is working"))
          (recur (conj (get-links (first links)) (rest links))))))))

If I'm totally wrong in my approach to this, let me know.

like image 469
Dale Avatar asked Feb 23 '23 03:02

Dale


1 Answers

conj adds an item to a collection. Using it on two collections creates a nested structure. You probably want to concat the two sequences instead.

To illustrate:

user> (conj [1 2 3] [4 5 6])
[1 2 3 [4 5 6]]
user> (concat [1 2 3] [4 5 6])
(1 2 3 4 5 6)
like image 106
Matthias Benkard Avatar answered Mar 05 '23 21:03

Matthias Benkard