Skip to content

Instantly share code, notes, and snippets.

@iopq
Created September 7, 2016 21:33
Show Gist options
  • Select an option

  • Save iopq/b0276633436af60ea3e9a6ae66053f39 to your computer and use it in GitHub Desktop.

Select an option

Save iopq/b0276633436af60ea3e9a6ae66053f39 to your computer and use it in GitHub Desktop.

Revisions

  1. iopq created this gist Sep 7, 2016.
    13 changes: 13 additions & 0 deletions lib.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    //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
    }