Ad
Code
Diff
  • // Code is in the preload
    fn person_builder(first_name: &str, last_name: &str) -> Person {
      Person { first_name: first_name.into(), last_name: last_name.into() }
    }
    • // Code is in the preload
    • fn person_builder(first_name: &str, last_name: &str) -> Person {
    • Person { first_name: String::from(first_name), last_name: String::from(last_name) }
    • Person { first_name: first_name.into(), last_name: last_name.into() }
    • }
Code
Diff
  • use std::iter::Iterator;
    use std::collections::{BTreeMap, btree_map};
    
    // The use of InitF allows us to remove the Clone bound on V - you supply a closure that returns a start value
    fn reduce<K, V, InitF, F, I> (iter: I, init: InitF, f: F) -> btree_map::IntoIter<K, V>
    where
        // Reference because we don't want it to modify the previous value, only look at it.
        F: Fn(V, &V) -> V,
        InitF: Fn() -> V,
        K: Ord,
        I: Iterator<Item=(K, V)>,
    {
        let mut out_map = BTreeMap::new();
        for (k, v) in iter {
            // Entry API lets us avoid repeating ourselves
            let slot = out_map.entry(k).or_insert_with(&init);
            *slot = f(v, slot);
        }
        out_map.into_iter()
    }
    
    • 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>
    • // The use of InitF allows us to remove the Clone bound on V - you supply a closure that returns a start value
    • fn reduce<K, V, InitF, F, I> (iter: I, init: InitF, f: F) -> btree_map::IntoIter<K, V>
    • where
    • F : Fn(TV, TV) -> TV,
    • TK : Eq + Ord,
    • TV : Clone,
    • I : Iterator<Item=(TK, TV)>,
    • // Reference because we don't want it to modify the previous value, only look at it.
    • F: Fn(V, &V) -> V,
    • InitF: Fn() -> V,
    • K: Ord,
    • I: Iterator<Item=(K, V)>,
    • {
    • 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()));
    • }
    • let mut out_map = BTreeMap::new();
    • for (k, v) in iter {
    • // Entry API lets us avoid repeating ourselves
    • let slot = out_map.entry(k).or_insert_with(&init);
    • *slot = f(v, slot);
    • }
    • m.into_iter()
    • out_map.into_iter()
    • }