Skip to content

Instantly share code, notes, and snippets.

@iopq
Created September 7, 2016 21:33
Show Gist options
  • Save iopq/b0276633436af60ea3e9a6ae66053f39 to your computer and use it in GitHub Desktop.
Save iopq/b0276633436af60ea3e9a6ae66053f39 to your computer and use it in GitHub Desktop.
//does the monoid operation on the slice of tuples if the closure evaluates to true
fn accumulate<'a, T: Monoid>(tuples: &[(&'a str, &Fn(i32) -> bool)], i: i32) -> Option<T>
where &'a str: Into<T> {
tuples.iter()
.filter(|&&(_,include_fn)| include_fn(i))
.map(first)
.fold(None, |acc, concat_str| acc.op(Some(concat_str.into())))
}
fn first<A, B>(&(ref one, _): &(A, B)) -> A {
*one
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment