On Wednesday, August 5, 2015 at 8:06 PM, Andy L wrote:
This time a quick question - is there an equivalent of algo.generic.functor/fmap in PigPen.
Andy
--
You received this message because you are subscribed to the Google Groups "PigPen Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pigpen-suppor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Do you have an example of what you'd like to see it do?
=> (->>
(pig/return sales)
(pig/group-by second)
(pig/map (fn [[k v]] [k (reduce + (map price v))]))
(pig/dump)
pprint)
(["ROCKLIN" 659.6588399999999]
["ROSEVILLE" 1860.739053]
["SLOUGHHOUSE" 38.490447]
["WEST SACRAMENTO" 115.71755300000001]
And you'd get a sequence of group->sum tuples instead of a map. If you really want a single row that's a map, you can use pig/into to achieve that, but I wouldn't recommend this for large datasets since it consolidates all of the data to a single machine:
=> (->>
(pig/return sales)
(pig/group-by second)
(pig/map (fn [[k v]] [k (reduce + (map price v))]))
(pig/into {})
(pig/dump)
pprint)
({"ROCKLIN" 659.6588399999999,
"ROSEVILLE" 1860.739053,
"SLOUGHHOUSE" 38.490447,
"WEST SACRAMENTO" 115.71755300000001,
You can also use the pigpen.fold namespace to reduce the groupings as well. Here's an example of your query written as a fold:
=> (->>
(pig/return sales)
(pig/group-by second {:fold (fold/sum (fold/map price))})
(pig/dump)
pprint)
(["ROCKLIN" 659.6588399999999]
["ROSEVILLE" 1860.739053]
["SLOUGHHOUSE" 38.490447]
["WEST SACRAMENTO" 115.71755300000001]
Not everything can be written as a fold, but most interesting things can. Be cautious with using fold however, if the number of values in each group is small, the overhead of doing a fold is greater than the benefit.
See more here: https://github.com/Netflix/PigPen/wiki/Folding-Data
Let me know if that's closer to what you were looking for.
Gotcha. The pigpen.core/group-by is slightly different than clojure.core/group-by.