Re: Is there a better way to do this than reduce + nested map?

2015-09-14 Thread Nicolás Berger
There is clojure.set/join to do exactly what you are asking for: ``` (-> (clojure.set/join entities locations) (clojure.set/rename {:id :location})) ;=> ({:name "bar", :location 101} {:name "foo", :location 100}) ``` (I'm also using clojure.set/rename to rename :id to :location) http://cloju

Re: Is there a better way to do this than reduce + nested map?

2015-09-13 Thread Colin Yates
You’re right, I missed a step :-). Something like (typed in-line so it really won’t compile): :locations [{“us” 100} {“de” 101}] :locations-seq (into {} (map (fn [{:keys [location]}] [:location (get locations location)])…) ;; the above produces a sequence symmetrical with entities, each map cont

Re: Is there a better way to do this than reduce + nested map?

2015-09-13 Thread Brian Platz
Not sure I follow Colin's suggestion entirely, but his suggestion led me to this which is better. I'd still love any other suggestions to improve! (def entities [{:name "foo" :location "us"} {:name "bar" :location "de"}]) (def locations [{:location "us" :id 100} {:location "de" :id 101}]) ;; mak

Re: Is there a better way to do this than reduce + nested map?

2015-09-13 Thread Colin Yates
I would transform the locations into {location id} and then use merge. > On 13 Sep 2015, at 3:14 PM, Brian Platz wrote: > > I have a pattern that comes up frequently, when I need to merge some value > into one map list from matching keys in a second map list. > > I've developed a way to handle

Is there a better way to do this than reduce + nested map?

2015-09-13 Thread Brian Platz
I have a pattern that comes up frequently, when I need to merge some value into one map list from matching keys in a second map list. I've developed a way to handle it, but I think it could be better. Here is a simple example of it: In the below case, we are looking to replace the :location in

Re: Is there a better way to do this

2012-06-17 Thread Joao_Salcedo
Hi Sean, That works just perfect !!! JS On Saturday, June 16, 2012 11:58:30 AM UTC+10, Sean Corfield wrote: > > On Fri, Jun 15, 2012 at 7:30 AM, Baishampayan Ghose > wrote: > > This is how it should be done, really - > > > > (defn get-events-hlpr > > "Retrieves events from MongoDB." > >

Re: Is there a better way to do this

2012-06-17 Thread Joao_Salcedo
Hi ALL, Thanks that is exactly what i was looking for :) On Saturday, June 16, 2012 12:30:45 AM UTC+10, Baishampayan Ghose wrote: > > > Hi Baishampayan, > > > >> (defn get-events-hlpr [] > >> "Retrieves events from MongoDB." > >> (vec (mc/find-maps "events"))) > > > > Is that Emacs Lisp

Re: Is there a better way to do this

2012-06-15 Thread Sean Corfield
On Fri, Jun 15, 2012 at 7:30 AM, Baishampayan Ghose wrote: > This is how it should be done, really - > > (defn get-events-hlpr >  "Retrieves events from MongoDB." >  [] >  (vec (map #(dissoc :_id %) (mc/find-maps "events" ;; remove the :_id as > well Or in Clojure 1.4.0 and later: (defn get

Re: Is there a better way to do this

2012-06-15 Thread Baishampayan Ghose
> Hi Baishampayan, > >> (defn get-events-hlpr [] >>   "Retrieves events from MongoDB." >>   (vec (mc/find-maps "events"))) > > Is that Emacs Lisp or Common Lisp? > > Bye, > Tassilo > > Just nitpicking that you adapted the OP's error of adding the docstring > after the parameter vector. ;-) You are

Re: Is there a better way to do this

2012-06-15 Thread Tassilo Horn
Baishampayan Ghose writes: Hi Baishampayan, > (defn get-events-hlpr [] > "Retrieves events from MongoDB." > (vec (mc/find-maps "events"))) Is that Emacs Lisp or Common Lisp? Bye, Tassilo Just nitpicking that you adapted the OP's error of adding the docstring after the parameter vector. ;-

Re: Is there a better way to do this

2012-06-15 Thread Baishampayan Ghose
> I am receiving a sequence from a particular function so I want to get that > sequence and converted to a vector o I can return a vector instead. > > (defn get-events-hlpr [] > "Retrieves events from MongoDB." > (init) > (def items (mc/find-maps "events")) ;; get the sequence > (loop [vtr [] >   d

Re: Is there a better way to do this

2012-06-15 Thread Walter Tetzner
> You could just do (vec (mc/find-maps "events")). > Also, to dissoc :_id from each item, do (vec (map #(dissoc % :_id) (mc/find-maps "events"))). -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googleg

Re: Is there a better way to do this

2012-06-15 Thread Walter Tetzner
On Friday, June 15, 2012 9:54:37 AM UTC-4, Joao_Salcedo wrote: > Is the right way? > You could just do (vec (mc/find-maps "events")). Also, using def creates a top-level var. You probably wanted (let [items (mc/find-maps "events")] ...) instead. -- You received this message because you are s

Is there a better way to do this

2012-06-15 Thread Joao_Salcedo
HI All, I am receiving a sequence from a particular function so I want to get that sequence and converted to a vector o I can return a vector instead. (defn get-events-hlpr [] "Retrieves events from MongoDB." (init) (def items (mc/find-maps "events")) ;; get the sequence (loop [vtr [] data it

Re: Q: is there a better way to do this

2009-09-23 Thread Timothy Pratley
Ok so there are two use cases: 1) testing constraint 2) referencing a snapshot And two operations A) @ B) ensure 1A is incorrect 1B is correct 2A is correct and concurrent 2B is correct but not concurrent "More concurrency" is the default. This increases the chance of incorrectness due to progr

Re: Q: is there a better way to do this

2009-09-23 Thread Roger Gilliar
> And even then, ensure is often not needed and overkill. Make sure you > have a real business requirement that the predicate remain true (or > value fixed) on transaction completion. We need to move to a world in -- It seems that my problem falls exactly in this category. Ensuring *

Re: Q: is there a better way to do this

2009-09-23 Thread Rich Hickey
On Wed, Sep 23, 2009 at 10:17 AM, Christophe Grand wrote: > > > On Wed, Sep 23, 2009 at 7:14 AM, Timothy Pratley > wrote: >> >> When it useful to be able to deref inside a dosync without ensuring? > > > In a read-only transaction. > > >> >> Ensure seems like a more safe default of what I'd expec

Re: Q: is there a better way to do this

2009-09-23 Thread Christophe Grand
On Wed, Sep 23, 2009 at 7:14 AM, Timothy Pratley wrote: > When it useful to be able to deref inside a dosync without ensuring? > In a read-only transaction. > Ensure seems like a more safe default of what I'd expect from a > transaction. So why not make deref automatically ensure within a > d

Re: Q: is there a better way to do this

2009-09-23 Thread Dave Jack
On Sep 23, 9:23 am, Dave Jack wrote: > Maybe @ should expand to ensure rather > than deref inside a transaction, instead? Should've thought about this more. How is the reader supposed to know that this code is called in a transaction? And it would leak if you deref'd inside a function called a

Re: Q: is there a better way to do this

2009-09-23 Thread Dave Jack
> When it useful to be able to deref inside a dosync without ensuring? When you deref and alter/set the same ref, the ref is protected from modification as well. I couldn't think of an example of what I think you had in mind, something that requires a transaction but is tolerant of modification o

Re: Q: is there a better way to do this

2009-09-22 Thread Timothy Pratley
> You need to prevent the modification of consuming by using ensure > rather than deref (@). Oh - excellent point! I didn't think of that. When it useful to be able to deref inside a dosync without ensuring? Ensure seems like a more safe default of what I'd expect from a transaction. So why not

Re: Q: is there a better way to do this

2009-09-22 Thread Roger Gilliar
>> .. It waits until the value is actually needed. For > more details on this, see http://ociweb.com/mark/stm/article.html. . Great article.Thanks ! Roger > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojur

Re: Q: is there a better way to do this

2009-09-22 Thread Dave Jack
> How can I be sure that no more data is added to data after @consuming   > was set to true ? You need to prevent the modification of consuming by using ensure rather than deref (@). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the

Re: Q: is there a better way to do this

2009-09-22 Thread Mark Volkmann
On Tue, Sep 22, 2009 at 3:18 PM, Roger Gilliar wrote: > >> thanks a lot. Your code looks much better than mine. But there is one >> part that I don't understand: >> >> (defn producer [] >>  (if (dosync (if (not @consuming) >>                (alter data conj 1))) >>    (recur))) >> > After rereadi

Re: Q: is there a better way to do this

2009-09-22 Thread Roger Gilliar
> thanks a lot. Your code looks much better than mine. But there is one > part that I don't understand: > > (defn producer [] > (if (dosync (if (not @consuming) >(alter data conj 1))) >(recur))) > > After rereading the docs several times It seems that I begin to understand h

Re: Q: is there a better way to do this

2009-09-22 Thread hoeck
Hi, I gave it a try to find a solution to your problem. I ended up using a single atom to hold the produced data and as means to detect that consuming started (by swapping in a fn wrapping the produced value). Don't know wether this fits your problem better than the one already posted. (def data

Re: Q: is there a better way to do this

2009-09-22 Thread Roger Gilliar
Hi, thanks a lot. Your code looks much better than mine. But there is one part that I don't understand: (defn producer [] (if (dosync (if (not @consuming) (alter data conj 1))) (recur))) How can I be sure that no more data is added to data after @consuming was set to

Re: Q: is there a better way to do this

2009-09-21 Thread Timothy Pratley
I can't claim this is better, but it is shorter - I simply removed what seemed like some unnecessary syntax: (def data (ref ())) (def consuming (ref false)) (defn producer [] (if (dosync (if (not @consuming) (alter data conj 1))) (recur))) (defn consumer [] (dosync (ref-

Q: is there a better way to do this

2009-09-21 Thread Roger Gilliar
This is some sort of repost from yesterday. But I hope that this one describes my problem more precisely. I have n threads that produce something. At some point in time a consumer starts and if the consumer starts, no more data should be produced. Right now I can only come up with the foll