Trait protocoll::Map [] [src]

pub trait Map<K, V> where
    Self: Sized
{ fn fun<'a, Q: ?Sized>(&'a self) -> Box<Fn(&Q) -> Option<&'a V> + 'a>
    where
        K: Borrow<Q>,
        Q: Hash + Ord
; fn inc(self, k: K, v: V) -> Self; fn dec<Q: ?Sized>(self, k: &Q) -> Self
    where
        K: Borrow<Q>,
        Q: Hash + Ord
; fn zero(self) -> Self; fn shrink(self) -> Self; fn update<F>(self, k: K, f: F) -> Self
    where
        F: FnOnce(Option<V>) -> V
; fn plus<I>(self, coll: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>
, { ... } fn update_all<F>(self, f: F) -> Self
    where
        Self: IntoIterator<Item = (K, V)> + FromIterator<(K, V)>,
        F: FnMut(&K, V) -> V
, { ... } fn merge<I, F>(self, coll: I, f: F) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        F: FnMut(V, V) -> V
, { ... } }

basic protocol for maps.

Required Methods

a map maps from keys to values.

adds v at k.

like clojure's assoc.

removes key k.

like clojure's dissoc.

clear.

shrink_to_fit.

updates the value at k by f.

like clojure's update.

example

use protocoll::Map;
use std::collections::HashMap;
let m = [0,0,0,1,1,0,0,0].iter().fold
    (HashMap::new(), |m,&k| Map::update
     (m, k, |n| 1 + n.unwrap_or(0)));
assert_eq!(6, m[&0]);
assert_eq!(2, m[&1]);

Provided Methods

pours another collection into this one.

like clojure's into.

updates all values by f

example

use protocoll::Map;
use std::collections::HashMap;
let m = [0,0,0,1,1,0,0,0].iter().fold
    (HashMap::new(), |m,&k| Map::update
     (m, k, |n| 1 + n.unwrap_or(0)))
    .update_all(|_,v| v + 1);
assert_eq!(7, m[&0]);
assert_eq!(3, m[&1]);

merges coll into this one, resolving conflicts by f.

like clojure's merge-with.

example

use protocoll::Map;
use std::collections::HashMap;
use std::ops::Add;
let m = [0,0,0,1,1,0,0,0].iter().fold
    (HashMap::new(), |m,&k| Map::update
     (m, k, |n| 1 + n.unwrap_or(0)));
let m = Map::merge(m.clone(), m, usize::add);
assert_eq!(12, m[&0]);
assert_eq!(4, m[&1]);

Implementors