Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator extension traits for folding monoids/semigroups #104

Open
ExpHP opened this issue Mar 25, 2018 · 1 comment
Open

Iterator extension traits for folding monoids/semigroups #104

ExpHP opened this issue Mar 25, 2018 · 1 comment

Comments

@ExpHP
Copy link
Collaborator

ExpHP commented Mar 25, 2018

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)
@Centril
Copy link
Collaborator

Centril commented Mar 26, 2018

this will require some sort of Newtype or Iso(-morphism) trait)

Generic ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants