Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate a sequence of impure functions in Clojure?

Tags:

clojure

How can I evaluate a list of (impure) functions in Clojure? For instance:

[#(println "1") #(println "2") #(println "3")]

The expected output is:

1
2
3

Is there a way to achieve this without using macros? Something like (map evaluate fns-seq), maybe?

(I need this for drawing some graphics using the Clojure.processing API.)

like image 348
Bernhard Kausler Avatar asked Jun 21 '10 22:06

Bernhard Kausler


2 Answers

user> (let [fs [#(println "1") #(println "2") #(println "3")]]
         (doseq [f fs] (f)))
1
2
3
nil
like image 160
Brian Carper Avatar answered Sep 28 '22 04:09

Brian Carper


This will eagerly consume the whole seq, calling all functions for side effects and returning whatever the last one returns:

(reduce #(%2) nil [#(println :foo) #(println :bar)])
; => prints :foo, then :bar, then returns nil

If you want to hold onto the return values, you can use reductions instead:

(reductions #(%2) nil [#(println :foo) #(println :bar)])
; => prints :foo, then :bar, then returns (nil nil)

reductions is found in clojure.contrib.seq-utils in Clojure 1.1 and in clojure.core in current snapshots of 1.2.

Update: Note that reductions returns a lazy seq, so it's no improvement over map (NB. in map you'd want to use #(%) rather than #(%2)). I mentioned it here mostly for completeness. In fact, I posted the whole answer for completeness, because normally I'd go with the doseq approach (see Brian's answer).

like image 29
Michał Marczyk Avatar answered Sep 28 '22 05:09

Michał Marczyk