Ad
Code
Diff
  • use std::iter::Iterator;
    use std::collections::{BTreeMap, btree_map};
    
    // we can use "impl I" when codewars adds rust 1.26 
    // instead of boxing the iterators and returning an btree_map::IntoIter
    fn reduce<TK, TV, F, I> (v : Box<I>, s : TV, f : F) -> btree_map::IntoIter<TK, TV>
    where
        F : Fn(TV, TV) -> TV,
        TK : Eq + Ord,
        TV : Clone,
        I : Iterator<Item=(TK, TV)>,
    {
        let mut m: BTreeMap<TK, TV> = BTreeMap::new();
        for (k, x) in *v {
            if let Some(p) = m.remove(&k) {
                m.insert(k, f(x, p));
            } else {
                m.insert(k, f(x, s.clone()));
            }
        }
        m.into_iter()
    }
    
    • fn reduce<TK, TV, TS, F>(v : Vec<(TK, TV)>,
    • s : TS,
    • f : F) -> Vec<(TK, TV)>
    • where F : Fn(TS, TV) -> TS {
    • // Code
    • vec![]
    • }
    • use std::iter::Iterator;
    • use std::collections::{BTreeMap, btree_map};
    • // we can use "impl I" when codewars adds rust 1.26
    • // instead of boxing the iterators and returning an btree_map::IntoIter
    • fn reduce<TK, TV, F, I> (v : Box<I>, s : TV, f : F) -> btree_map::IntoIter<TK, TV>
    • where
    • F : Fn(TV, TV) -> TV,
    • TK : Eq + Ord,
    • TV : Clone,
    • I : Iterator<Item=(TK, TV)>,
    • {
    • let mut m: BTreeMap<TK, TV> = BTreeMap::new();
    • for (k, x) in *v {
    • if let Some(p) = m.remove(&k) {
    • m.insert(k, f(x, p));
    • } else {
    • m.insert(k, f(x, s.clone()));
    • }
    • }
    • m.into_iter()
    • }