tags:

views:

164

answers:

3

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)))
A: 

It might be a copy/paste issue, but it looks like you didn't close the quotes in the last line.

(map show '(HELLO Sweet love)'))

Velociraptors
That is a list and not a single atom.
kunjaan
It was just a guess; I'm not actually familiar with Clojure syntax.
Velociraptors
' is shorthand for (quote ...whatever comes next...). meaning do not attempt to run this as a function.
Arthur Ulfeldt
+4  A: 

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.

Brian Carper
+1  A: 

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.

Arthur Ulfeldt
Thanks for the detailed answer. Are all the list abstractions lazy? How do I know which ones are and which ones are not?
kunjaan
i believe they all are except reverse, though vectors have an (rsec [1 2 3]) function that creates a sec starting at the end which usually solves that problem.
Arthur Ulfeldt