Why doesnt Clojure perform this function at all?

I have a function called show that displays a message dialog. I need to map this function to all elements in alist. But Clojure does not show me any messages. What am I doing wrong?

(defn show[message] (. javax.swing.JOptionPane (showMessageDialog nil message))) (defn action[] (map show '(HELLO Sweet love))) 
+7
clojure
source share
2 answers

The map function does not actually run the displayed function for each member of the collection. rather, it returns the "lazy" cell. this is very similar to your classic single-level list with one very important difference, the data in each cell is calculated at a time when it is not being read at the time of its determination (this result, of course, is preserved for subsequent readings). Therefore, to run a function, you must read the result of the function launch. Since in this case you do not care about the results of the function only in that it ran clojure, it provides an excellent wrapper function called

 (dorun .... insert your map here .... ) 

who will create a map, read the results and immediately throw them away, losing memory, saving them later.

If you use a mapping function to the results that you want to keep, use the dose instead.

+5
source share

map lazy. Nothing will be evaluated until you try to evaluate it. Either (dorun (action)) , or use doseq instead of map .

+7
source share

All Articles