On Mon, Aug 25, 2008 at 9:16 AM, Chouser <[EMAIL PROTECTED]> wrote: > On Mon, Aug 25, 2008 at 8:45 AM, Stuart Halloway > <[EMAIL PROTECTED]> wrote: >> >> (defn keys-with-value-matching [map test-fn] >> (for [pair (map identity map) :when (test-fn (last pair))] >> (first pair))) >> >> (defn basic-colors-in [color] >> (keys-with-value-matching color (comp not zero?))) > > I'm not sure why you used "(map identity map)" instead of just "map",
Sorry to reply to myself, but immediately after sending I realized (map identity map) isn't doing what I had thought it was, and since others may be similarly confused... You've already shadowed the builtin clojure/map function with your function argument named map. So inside that function, it doesn't matter whether you use the word "map" in the initial position or in the last position -- both uses of the word "map" in (map identity map) refer to your argument. In order to get to the builtin function you'd have to say "clojure/map". When I first read your code I assumed, probably partially because of the use of "identity", that that expression was calling clojure/map. But since it's not, how is it working? Let's say the hash-map {:foo :bar} is passed in for "map". That means the expression in question is the same as ({:foo :bar} identity {:foo :bar}). A hash-map in the function position like that looks up it's first parameter ("identity" in this case) in itself. Chances are the identity function wasn't used as a key the hash-map, so the lookup will fail. Therefore the lookup falls back to the default expression given in the next argument, which in this case is the hash-map itself {:foo :bar}. And thus we get the return value we were expecting, but for entirely different reasons. This also means there's a subtle bug lurking in the code. Not likely to be a problem, I admit, but it would nonetheless be surprising: user=> (color-string (assoc (struct color 20 30 10) identity {:red 99})) "Red: 20" By supplying a color struct that has an "identity" key, color-string uses the key list from identity's value (that is, just :red), but the value number from the original struct. I don't know if that made any sense to anyone, but anyway... --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---