Fork me on GitHub
#beginners
<
2017-08-15
>
leira03:08:40

I'm confused with next and rest, which one should I prefer to use?

noisesmith03:08:57

next returns nil if the coll is empty

leira03:08:58

I understand the difference that rest is lazier than next

noisesmith03:08:03

rest returns an empty seq

leira03:08:19

but when I write code, what's the rule of thumb to chooose which one to use?

noisesmith03:08:21

if getting nil is better than being more lazy, use next - usually you want rest

noisesmith03:08:51

@leira the reason it works that way is that in order to return nil for empty coll, it needs to try to realize the next item

noisesmith03:08:58

so it's less lazy

leira03:08:32

so sounds to me that rest is better than next, so rest should be the default?

noisesmith03:08:47

right, and you'll find that almost always rest is what we use

noisesmith03:08:11

but if you are doing a conditional based on the coll being empty, you need to check it regardless, and next is the best way to do that

leira04:08:34

Isn't (empty? coll) the same thing?

leira04:08:38

(if (next coll) ... ) vs (if (empty? (rest coll)) ...)), isn't empty? more explicit?

noisesmith04:08:53

empty? returns true or false, next returns something that counts as false, or the remaining items of the collection after the first