artella
artella

Reputation: 5118

Convert an old lazy sequence to a new lazy sequence via application of univariate function

suppose in clojure I have a lazy sequence (a b c d .....) and suppose that I have a univariate function f(x). How would I transform the old lazy sequence into a new lazy sequence (f(a) f(b) ....). i.e. I seek the transformation. Thanks :

(a b ....) [lazy] -> (f(a) f(b) ....) [also lazy]

Upvotes: 3

Views: 87

Answers (1)

mikera
mikera

Reputation: 106401

map does everything you need. It preserves the "laziness" of sequences that you apply it to.

(map f old-lazy-sequence)
=> [new-lazy-sequence]

Example with infinite ranges:

(take 5 
  (map (partial * 2) 
       (range)))
=> (0 2 4 6 8)

Upvotes: 6

Related Questions