first of all i want to thank everyone for answering my question so quickly!
nevertheless i have to say that i am still very much confused about seq, rest and next
now in order to explain where all of this confusion is coming from, i should perhaps say, that i have read the book ‘the joy of clojure, 2nd edition’ ( or at least large parts of it ) and to be honest i still think that it is a fantastic book. having said that, i now feel like i may have taken some things from the book without doing sufficient testing on my own.
so for example in my mind (next s) was really like (seq (rest s)) and also i took away from the book, that rest is more lazy than next.
so then i just happened to write a piece of code that used the some function, and sometimes i do look at the source from clojure itself so i found that it did use the seq check for the condition but then also next for the recursive part. so i thought to myself, well is that not like calling seq too often?
so i thought well, why not register at clojureverse and ask about this, and i am really
glad i did, because the answers that i got made me realize, that some of the preconceived ideas i had formed because of the book i mentioned, really do need some re-evaluation.
alright, so first of all i did search for the topics of seq, next and rest in the book again
( with manning you do get the pdf version as well, so that came in really handy!)… in any
case in the book you find for example under chapter 3.2 ‘nil pun with care’ the following:
(defn print-seq [s]
(when (seq s)
(prn (first s))
(recur (rest s))))
and then it goes on to say:
Second, rest is used instead of next to consume the sequence on the recursive call.
Although they’re nearly identical in behavior, rest can return a sequence that’s either
empty or not empty (has elements) C , but it never returns nil . On the other hand,
next returns a seq of the rest , or (seq (rest s)) , and thus never returns an empty
sequence, returning nil in its place. It’s appropriate to use rest here because you’re
using seq explicitly in each subsequent iteration.
also under chapter 6.3.2 ‘Understanding the lazy-seq recipe’ there is a little box
titled next vs. rest in which you can find the following code example:
(def very-lazy (-> (iterate #(do (print .) (inc %)) 1)
rest rest rest))
(def less-lazy (-> (iterate #(do (print .) (inc %)) 1)
next next next))
so i tried that out as well BUT!!! it did not come out as expected. and
it seams i was not the only one to notice this:
now in the comments i found:
I would think the advice would stand because next is still one item less lazy than rest. – webappzero Nov 3 '17 at 20:58
but i have a really hard time to even come up with a good example for showing this difference in lazy behavior.
so… some, seq, rest, next??? clojure idiom for doing nil-pun iteration like stuff,… i am
if anyone could help shed some more light on this matter i would really
appriciate it, because it seams to me that the matter at hand is pretty
important, since it seams so basic,…