Вопрос

This code works:

(def n1 (range))
(println 
     (->> n1
          (take 30))
)

This code does not work:

(def n2 (iterate inc 0))
(println 
     (->> n2
          (take 20))
)

A modified code (n2 is function) works:

(def n2 #(iterate inc 0))
(println 
     (->> (n2)
          (take 20))
)

How come the second code is not allowed?

In the first code, (range) returns infinite sequence (0...)

in the second code, (iterate inc 0) also returns infinite sequence (0...)

What makes so different?

Please guide. Thanks.

(EDIT)

glad to hear the code#1 and code#1 is equivalent.

however, in my environment (light table) can't eval it:

enter image description here

Это было полезно?

Решение 2

Chris Granger, the LightTable developer Added an issue:

https://github.com/Kodowa/Light-Table-Playground/issues/657

Thanks.

Другие советы

Nothing is different. Both of your first two snippets work fine: you must have gotten confused and run some different program.

Edit in response to clarification:

Light table is presumably attempting to show you the intermediate result (iterate inc 0) as well as the final result. I'd guess the difference is that (lazy-seq? (range)) returns true, and (lazy-seq? (iterate inc 0)) returns false (because the first element is eager, even though the rest of the sequence isn't).

That seems like a pretty serious issue with light table to me, but what can you expect from alpha-quality software? At any rate it's not a clojure problem at all.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top