> Suggestions for entries welcome here. > > > > Rich >
Here's another that was a "gotcha" for me for an hour or two... Why after using map/reduce/for to change a java object does the object remain unchanged? (defn initv1 [myseq] (let [v (java.util.Vector.)] (for [x myseq] (.addElement v x)) v)) (initv1 [1 2 3]) ; => (java.util.Vector. []) ; or... (defn initv2 [myseq] (let [v (java.util.Vector.)] (map (fn [x] (.addElement v x)) myseq) v)) (initv2 [1 2 3]) ; => (java.util.Vector. []) As map/reduce/for are lazy, the "let" construct does not actually "take" any elements from the map/reduce/for. In this case, use doseq... (defn initv3 [myseq] (let [v (java.util.Vector.)] (doseq [x myseq] (.addElement v x)) v)) (initv3 [1 2 3]) ; => (java.util.Vector. [1 2 3]) (Actually this happened in a more complex function, but the essence was that a new java object was instantiated in a let and I'd used a map to call a method on that object with all the items from a sequence. I could not understand why the object remained unchanged afterwards until I simplified it down to the above example). Regards, Adrian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---