I have a function called show which shows a dialog with the message. I need to map this function to all the items in alist. But Clojure doesnt 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)))
map
is lazy. Nothing is going to be evaluated until you force it to be evaluated. Either (dorun (action))
or use doseq
instead of map
.
the map function doesnt actually run the mapped function on each memeber of the collection. rather it returns a 'lazy-cons' cell. this looks a lot like your classic singly linked list with one very important difference, the data in each cell is computed at the time that it is read not the time it is defined (this result is of course stored for later reads). So in order to have the function actually run you have to read the result of running the function. Because in this case you dont care about the result of the function only that it ran clojure provides a great wrapper function called
(dorun .... insert your map here .... )
that will create the map, read the results and promptly throw them out with out wasting memory storing them for later.
If you where mapping a function with results you wanted to keep then use doseq instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With