Clojure recursion and lazy sequence

Ok, I'm a little stuck with this, can I 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 that is passed to the original call of the link processes, and then should be returned.

The first link that I feed in works, but then the second link in which I try to connect one sequence to another gives me the following error.

 " Clojure.lang.LazySeq@xxxxxxx " 

Now I am wondering if this refers to a link to a command to generate β€œrest” (rest links) of an unappreciated 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 am completely mistaken in my approach to this, let me know.

+4
source share
2 answers

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

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) 
+7
source

As for the object Clojure.lang.LazySeq @xxxxxxx ":

The problem is this snippet:

 (println (str (first links) " is working")) 

Here you use the str string concatenation function for gluing (first links) , which is not a string in this case, and " is working" , which is a string. What does str do with a non-string argument? He calls the .toString method. What does .toString do for Clojure data? Not always what you need.

The solution is to use the pr family of functions. pr writes Clojure data to the stream in a way that is recognized by the Clojure reader. Two examples of how this can be rewritten can be rewritten:

 (do (pr (first links)) (println " is working")) ;; Sligtly less efficient since a string must be created (println (pr-str (first links)) "is working") 

Note that if you pass multiple println arguments, they will print all elements with spaces between them.

+1
source

Source: https://habr.com/ru/post/1386186/


All Articles