Hello everybody,I would like to know as to how I would write the following code in a more idiomatic way.
(let [mp (atom {})d [#{#{1 2}#{3 4}} ;node1#{#{5 6}#{7 8}} ;node2]](dorun (for [nd d nd-pair nd face nd-pair](swap! mp update-in [face] #(conj % nd))))@mp)
; if you consider that points 1 2 3 4 form the node node1 and 5 6 7 8 form node2;I would like a map in the opposite direction ..;i.e. I should be able to find out all the nodes of which 1 is part of and;that is what the above code is doing.. but would like to know as to what;would be a more idiomatic way of doing this.
I don't like the fact that I am using atom and swap .. can anybody suggest a better way to do it?Thanks, Sunil.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
"If you find yourself wishing yourself to repeatedly check a work queue to see if there's an item of work to be popped off,
or if you want to use a queue to send a task to another thread, you do *not* want the PersistenQueue discussed in this section"
Why do I not want to use clojure.lang.PersistentQueue for that purpose and what would I want to use instead?
Can anyone fill me in please?
clojure.lang.PersistentQueue is a collection data structure with queue
operations. It is persistent, so it lets you (and other threads)
create modified versions of it with elements enqueued or dequeued in a
thread-safe manner.
java.util.concurrent.LinkedBlockingQueue[1] is not a persistent data
structure. It is often used as a communication channel between
threads, since it allows its operations to block (potentially with a
timeout). One thread can dequeue (and block if the queue is empty)
simultaneously as another thread can enqueue (and block if the queue
is full) at the other end. It makes it easy to make programs in a
producer-consumer style.
// raek
[1] http://download.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html
If you have:
(defn foo [coll]
(let [x (atom bar)]
(dorun [y coll]
(swap! x f y))
@x))
you can change it to:
(defn foo [coll]
(reduce f bar coll))
:)
g