blackened
blackened

Reputation: 903

Clojure “repeatedly”

Clojure source for repeatedly reads:

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.

Without knowing the above explanation, how do I infer that the below definition of repeatedly takes a function of no arguments?

(defn repeatedly
  ([f] (lazy-seq (cons (f) (repeatedly f))))
  ([n f] (take n (repeatedly f))))

Upvotes: 1

Views: 407

Answers (1)

Carcigenicate
Carcigenicate

Reputation: 45826

By looking for references of the function being passed in.

Note:

(f)

f is only referenced three times in the definition. Two of those times are it being passed in to recursive calls (you can tell it's not being called because it's not surrounded by brackets), and once where it's called.

In the case where it's called (surrounded by brackets), you can tell it expects 0 arguments since none are being passed to it.

Upvotes: 4

Related Questions