On Sat, Mar 21, 2009 at 6:12 PM, Laurent PETIT <laurent.pe...@gmail.com> wrote: > Hello Mark, > > 2 questions / suggestions : > > * couldn't step 2) be computed outside function b, and then my-data be > replaced by new-struct ? This way, if you have several flavors of function > b, you will not have to repeat the code in steps 1) and 3) in each of those > flavors of b ? And then the problem you have disappear because new-struct is > known in advance by the function calling swap!
I neglected to say that the result of step 2, the new id, is also part of the struct, so that won't work. > * may I challenge your use of atom ? When I read your post, I had an orange > light flashing "warning, warning", so I can't resist do this :-) : from the > information you give, it seems to me that my-atom is like a repository. > Wouldn't this repository be consulted by other functions ? In this case, > don't you want to sometimes ensure (via the ensure function) that the > repository remains in the same state for the duration of some transactions ? > Or write some commute code to add/remove/update parts of it from within a > transaction ? Then shouldn't your atom really be a ref ? I think this may be the best solution. It doesn't seem that I can achieve what I need with an atom. Thanks for the suggestion! > 2009/3/21 Mark Volkmann <r.mark.volkm...@gmail.com> >> >> I'm using an atom to hold a sorted-map whose keys are integer ids and >> values are a particular kind of struct. >> I have a function, call it "a", that adds an entry to this map. >> It contains just this line: >> >> (swap! my-atom b my-data) >> >> "b" is a function that: >> 1) determines the next available id by getting the last entry in the >> sorted-map and adding 1 >> 2) creates new-struct using my-data >> 3) creates and returns a new map by calling (assoc current-map id >> new-struct) >> >> All that seems simple enough. >> However, I need to be able to get the new-struct that was just added >> so I can pass it to other functions. >> In this case, swap! returns the new map that has become the new value >> of the atom, not the value that was just added to the map. >> >> I'm looking for a suggestion on how I can get new-struct. -- R. Mark Volkmann Object Computing, Inc. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---