frunk
frunk copied to clipboard
Iterator extension traits for folding monoids/semigroups
There should be something to fold an iterator of monoids down to a single value, or to create the Monoids first and then fold them. fold_map is one of my favorite things in haskell.
Vague ideas: (not thinking too hard about references and whatnot, since I'd rather see #57 before this)
vec![Product(1), Product(2)].iter().combine();
xs.iter().fold_map(Product); // -> Product(i32) (for Monoids)
xs.iter().fold1_map(Product); // -> Option<Product(i32)> (for Semigroups)
xs.iter().fold_map_onto(Product(1), Product); // -> Product(i32) (for Semigroups)
or maybe we can do better, with the understanding that fold_map is really only useful for newtypes (so it might as well get unwrapped at the end; this will require some sort of Newtype or Iso(-morphism) trait)
// where Product<i32> implements some trait that provides
// conversions for i32 <-> Product<i32>
vec![1, 2].iter().combine_like(Product); // -> i32 (for Monoids)
vec![1, 2].iter().combine_option_like(Product); // -> Option<i32> (for Semigroups)
vec![1, 2].iter().combine_onto_like(1, Product); // -> i32 (for Semigroups)
this will require some sort of Newtype or Iso(-morphism) trait)
Generic ;)