send agent inside doseq

78 views
Skip to first unread message

Kuba Roth

unread,
Aug 20, 2013, 7:21:59 PM8/20/13
to clo...@googlegroups.com
Hi there,
I've got a range of values and I'd like to run agents for each value per thread. For some reason I've got only one agents being updated.
Not sure what's wrong here but I suspect  must be doing something terrible stupid...

Thanks!

(doseq [s (range 30 35)]
       ;(println (format "_%s" s))
       (intern *ns* (symbol (format "_%s" s) ) (agent s))         ;; set initial value
       (send @(intern *ns* (symbol (format "_%s" s))) + 100)      ;; send agent and update value
       (println @(intern *ns* (symbol (format "_%s" s)) ))        ;; deref
 )

juan.facorro

unread,
Aug 20, 2013, 7:49:51 PM8/20/13
to clo...@googlegroups.com
If on the println you don't see the value updated, it's probably because the operation sent to the agent wasn't applied yet.

Add a (Thread/sleep 500) in between the send and println expressions and you'll see the expected agents'.

Cheers,

JF   

Sean Corfield

unread,
Aug 20, 2013, 7:56:30 PM8/20/13
to clo...@googlegroups.com
Very likely Juan, as seen here:

(let [agents (map agent (range 30 35))]
(doseq [a agents]
(send a + 100)
(println @a))
(doseq [a agents]
(println @a)))

For me that prints:

30
31
32
33
34
130
131
132
133
134

but I suspect that's more luck that anything since there's no reason
the agent operations need to have finished before the second println.

Kuba, as a separate issue, why are you trying to intern all those
symbols? It's not very idiomatic.

Sean
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Kuba Roth

unread,
Aug 21, 2013, 12:31:33 AM8/21/13
to clo...@googlegroups.com
Thanks Sean, your example looks much cleaner and most important works! 
The reason I looked into 'intern' can only be explained by totally lack of experience in Clojure and more general functional programming. 
My goal was to dynamically create a var inside the 'doseq' and apparently 'intern' is used in this cases. The 'def' special form didn't work for me... (not sure why). But as I said it was rather my lame attempt to make things work then any well thought out decision :)

Thanks,
Kuba

Sean Corfield

unread,
Aug 21, 2013, 1:33:46 AM8/21/13
to clo...@googlegroups.com
On Tue, Aug 20, 2013 at 9:31 PM, Kuba Roth <kuba...@gmail.com> wrote:
> The reason I looked into 'intern' can only be explained by totally lack of
> experience in Clojure and more general functional programming.

Ah, is your background OOP?

You'll find the functional world is pretty different. No "variables"
in the traditional sense, no uncontrolled mutation, no "loops" in the
traditional sense, no state in the OOP sense.

def and defn only create top-level definitions - bindings of values to
symbols - and intern is really an implementation detail that you want
to keep away from.

As you can see from my example, you can create a sequence of agents
easily enough as a data structure, and then you can perform operations
on that data structure. Agents provide controlled mutation in that
they start out with an initial value and you send them a series of
function invocations and they take on new values as each function
invocation is applied asynchronously.

I wonder what you might be trying to achieve? Or perhaps this is just
initial experimentation? One thing you'll find about Clojure is how
rarely you need mutation.

At World Singles we have about 18,000 lines of Clojure with just two
agents (used to provide measured asynchronous DB updates for a couple
of very specific situations) and 27 atoms, which are nearly all just
caches - only half a dozen of those are really "mutable state" (and
we're consolidating that).

Welcome to Clojure!

Kuba Roth

unread,
Aug 21, 2013, 2:17:45 AM8/21/13
to clo...@googlegroups.com
Yup, I've done OOP in the past but probably even closer procedural programming. Recently I've been working pretty much exclusively in python/C++ which are somewhat at two extremes. I'm hoping to see Clojure to be a blend of these two and a replacement especially in areas where things in python are too slow or impossible (parallel stuff). The functional approach of Clojure is the down side for me at the moment ;). The lack of loops and variables is a bit mind-bending, feels like going back to start with programming in general.

kuba


--
--
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
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/yVOVvgq88hE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages