Clojure: rest vs next

next is stricter than (the new behaviour of) rest because it needs to evaluate the structure of the lazy cons to know whether to return nil or a seq.

rest on the other hand always returns a seq, so nothing needs to be evaluated until you actually use the result of rest. In other words, rest is more lazy than next.

next is like (seq (rest ...)).

rest will return the remaining piece of a sequence. If that piece of the sequence has not yet been realized, rest doesn't force it. It won't even tell you if there are more elements left in the sequence.

next does the same thing but then forces at least one element of the sequence to be realized. So if next returns nil, you know there aren't any more elements left in the sequence.

next会去实例化第一个元素,然后返回第一个实例化元素+剩余序列,如果没有元素了,next会告诉你没有剩余元素了,就会返回nil

rest会返回剩余序列,rest不回去实例化序列的任何一个元素,也不会告诉你剩余序列还有没有元素。

2018-12-12 23:54

留言