Question

I am trying to re-implement my custom map function, but it works in a strange way. Could some one explain why it happens?

(defn my-map [f coll]
  (lazy-seq
   (when-let [s (seq coll)]
     (cons (f (first s)) (my-map f (rest s))))))

(take 10 (my-map inc (range)))

(take 10 (->> [0 1]
              (iterate (fn [[a b]] [b (+ a b)]))
              (my-map first)))

First take works as expected, but second gives me following error (looks like it evaluating all sequence):

java.lang.ArithmeticException: integer overflow
    Numbers.java:1388 clojure.lang.Numbers.throwIntOverflow
    Numbers.java:1687 clojure.lang.Numbers.add
     Numbers.java:430 clojure.lang.Numbers$LongOps.add
     Numbers.java:126 clojure.lang.Numbers.add
Was it helpful?

Solution

You are right, it is a lighttable bug. You should log it here

The following work in lighttable without issue

(->> [0 1] 
     (iterate (fn [[a b]] [b (+ a b)])) 
     (take 10)
     (my-map first))

and

(take 10 (->> [0 1M]
              (iterate (fn [[a b]] [b (+ a b)]))
              (my-map first)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top