On Jul 8, 2014, at 9:40 AM, Cecil Westerhof <cldwester...@gmail.com> wrote:
> In Clojure you can define a local constant with let, but I need a variable (I > think). > > I want to do the following. I have a function that checks several things. > Every time an error is found I want to set the variable errors to: > (concat errors new-error) > > Is this possible? Or is there a better way to do this? Here's a different take on your question, and a few comments about how I'd write that code. I don't think you need the atom -- kinda ugly and the reduce/map/filter family of sequence functions will take you a long way. ; This is not a predicate, so don't use the -p suffix (and in Clojure it's a ? normally) ; There's no reason not to pass those two objects into this function, so I do. (defn errors-in-datastruct [locations objects] (if (= (count objects) (count locations)) ; 'remove' makes a new sequence containing elements that had a falsey ; (false or nil) result to the provided function -- that is the objects ; with no location ; the mapv maps a function over a sequence resulting in a vector. ; the function creates a vector pair for each object containing the error ; code and object ; I find vectors more natural to use in Clojure ; I also find keywords more natural (and useful) ; In my mind, finding the errors and reporting them are two separate functions. If you want to make ; a nicer error report, then map a reporting function over the error codes. (mapv #(vector :no-location %) (remove locations objects)) ; This reduce does something similar to the mapv/remove combination above, but ; it gives you more flexibility. #_(reduce (fn [errors obj] (if (locations obj) errors (conj errors [:no-location obj]))) [] objects) [[:counts-differ]])) ; use locally scoped variables if you can (let [locations {:whiskey :living-room :bucket :living-room :chain :garden :frog :garden :dummy :nowhere}] (println (errors-in-datastruct locations [:whiskey :bucket :chain :frog :dummy])) (println (errors-in-datastruct locations [:whiskey :bucket :chain :frog :pole :dummy])) (println (errors-in-datastruct locations [:whiskey :bucket :chain :frog :pole])) (println (errors-in-datastruct locations [:whiskey :bucket :chain :frog1 :pole])) (println (errors-in-datastruct locations [:whiskey :whiskey :whiskey :whiskey :whiskey]))) ; If you need to use the same locations map and don't want to pass it around, you can ; partially apply the errors-in-datastruct function to the locations, and use that function. ; You probably don't care about this but the technique can be very handy when getting ; rid of global variables. (let [f (partial errors-in-datastruct {:whiskey :living-room :bucket :living-room :chain :garden :frog :garden :dummy :nowhere})] (println (f [:whiskey :bucket :chain :frog :dummy])) (println (f [:whiskey :bucket :chain :frog :pole :dummy])) (println (f [:whiskey :bucket :chain :frog :pole])) (println (f [:whiskey :bucket :chain :frog1 :pole]))) I hope you can make sense of that :-) Cheers, Bob > > -- > Cecil Westerhof > > -- > 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 > Note that posts from new members are moderated - please be patient with your > first post. > 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 > --- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.