repeatedly function in Clojure

user=> (doc repeatedly)

clojure.core/repeatedly ([f] [n f]) Takes a function of no args, presumably with side effects, and returns an infinite (or length n if supplied) lazy sequence of calls to it nil

user=> (repeatedly 5 (fn [] 90)) (90 90 90 90 90)

user=> (type (repeatedly 5 (fn [] 90))) clojure.lang.LazySeq

user=> (repeatedly 5 (fn [] (do (println "hi") 75))) (hi hi 75 hi 75 hi 75 hi 75 75) user=> (repeatedly (fn [] 87)) OutOfMemoryError Java heap space java.util.Arrays.copyOf (:-1)

So you can see repeatedly can take a function with/without side effect. But how to return a function with infinite length?

posted on 2014-03-18 08:09  leechau  阅读(136)  评论(0编辑  收藏  举报

导航