Hacker News new | ask | show | jobs
by ndr 1043 days ago
> The good parts of laziness: Avoiding unnecessary work

Actually be very careful with side effects. Some functions like `map` and `for` take things in chunks, typically in steps of 32 as most underlying structures are in log-32 leaves.

```

  (let [printing-range (map (fn [i] (print "debug: " i) i) (range))
        first-10 (take 10 printing-range)]
   first-10)
  debug: 0
  debug: 1
  debug: 2
  debug: 3
  debug: 4
  debug: 5
  debug: 6
  debug: 7
  debug: 8
  debug: 9
  debug: 10
  debug: 11
  debug: 12
  debug: 13
  debug: 14
  debug: 15
  debug: 16
  debug: 17
  debug: 18
  debug: 19
  debug: 20
  debug: 21
  debug: 22
  debug: 23
  debug: 24
  debug: 25
  debug: 26
  debug: 27
  debug: 28
  debug: 29
  debug: 30
  debug: 31
  (0 1 2 3 4 5 6 7 8 9)

```
1 comments

It can be legitimate to have side effects in lazy processing and in particular to rely that a lazy sequence is not accessed beyond the visible access that is coded in the program.

Suppose we make a sequence of numbers which grows very rapidly, so that by the time we hit the 17th one, we have a bignum that is gigabytes wide.

You probably don't want this to be chunked in batches of 32.

Another situation might be if we have some side effect: the lazy sequence is connected to some external API somehow or foreign code. You might want it so that the observable behaviors happen only to the extent that the sequence is materialized.

The advice to be careful with side effects is good in general; not sure why you're downvoted.