5 ms·
May I present to you a list of cool functions Clojure doesn't have? map-keys, map-values, map-entries, group-by-1, group-by-2 on [[k, v]], zip-with [k] -> [[k
by guard-of-terra 12y ago
May I present to you a list of cool functions Clojure doesn't have?
map-keys, map-values, map-entries, group-by-1, group-by-2 on [[k, v]], zip-with [k] -> [[k v]].
That's what you immediately need if you do any data processing. Had to write those myself.
- Skinney 12y agoDon't understand why you would need seperate functions for those three first. map-keys would basically just be map with (keys coll), map-values would be with (vals coll) and entries would just be a normal map, as hashmaps in Clojure are seqs and mapping over hashmap returns entries as [key val]. A simple destructuring in your map function and you're good to go. Don't quite understand what group-by-1 and group-by-2 does though. Elaborate?
- guard-of-terra 12y agoThe need of repeating exactly the same destructuring or transformation (keys, uncurrying tuple) constaints your thought. You need to have an instrument that fits your domain perfectly. group-by-[12] groups a [[k, v]] list by either k or v, of course you can do this with group-by first, but see above.
- coldtea 12y ago>You need to have an instrument that fits your domain perfectly. Well, then you have to built it FOR your domain. It would strange to have the built-in functions fit ANY domain.
- guard-of-terra 12y agoMap-reduce enthusiasts were wrong: the two fundamental functions are not map and reduce, rather zip-with and group-by. If you want to do real work done. I've heard Google learned this lesson and their data processing now rests upon these two.
- adambard 12y ago`zip-with` and `group-by` just represent the most common uses of `map` and `reduce` respectively. Many data processing frameworks (cascading, spark, storm, etc.) build these abstractions right on top of map-reduce, and sometimes the layer is pretty thin. Heck, zip-with in clojure is literally just map: (map + [1 1 2 3] [1 2 3 5]) ; => [2 3 5 8]
- guard-of-terra 12y agoIt's not zip-with that you wrote. You illustrate that given not enough tools, you lose the grip on the process. zip-with is (zip-with #(+ % 1) [1 1 2 3]) ; => [[1 2] [1 2] [2 3] [3 4]]
- Guthur 12y agoAm I missing something? From my understanding ZIP should be over more than one sequence, otherwise it's just a normal map really. Certainly the Haskell examples I have seen would strongly reinforce my understanding.
- Guthur 12y agoDown voted? Really come on.
- Guthur 12y agoLets race all the way down, seems like it's a meaningless metric anyway.
- guard-of-terra 12y agoWell, it zips a sequence with another derived sequence.
- darklajid 12y agoLike (defn zip-with [f xs] (map (juxt identity f) xs)) or something else? Without showing what your zip-with (and all the other 'missing' things) implementation looks like, people need to guess what they are and why these might be useful.
- darklajid 12y agoIt seems you had to explain all but the first three in this thread. That suggests to me that these are potentially nice shortcuts for you personally, but they might be more confusing than the well-defined standard building blocks you try to hide in shared code..