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
fn fun<'a, Q: ?Sized>(&'a self) -> Box<Fn(&Q) -> Option<&'a V> + 'a> where
K: Borrow<Q>,
Q: Hash + Ord,
K: Borrow<Q>,
Q: Hash + Ord,
a map maps from keys to values.
fn inc(self, k: K, v: V) -> Self
adds v
at k
.
like clojure
's assoc
.
fn dec<Q: ?Sized>(self, k: &Q) -> Self where
K: Borrow<Q>,
Q: Hash + Ord,
K: Borrow<Q>,
Q: Hash + Ord,
removes key k
.
like clojure
's dissoc
.
fn zero(self) -> Self
clear
.
fn shrink(self) -> Self
shrink_to_fit
.
fn update<F>(self, k: K, f: F) -> Self where
F: FnOnce(Option<V>) -> V,
F: FnOnce(Option<V>) -> V,
Provided Methods
fn plus<I>(self, coll: I) -> Self where
I: IntoIterator<Item = (K, V)>,
I: IntoIterator<Item = (K, V)>,
pours another collection into this one.
like clojure
's into
.
fn update_all<F>(self, f: F) -> Self where
Self: IntoIterator<Item = (K, V)> + FromIterator<(K, V)>,
F: FnMut(&K, V) -> V,
Self: IntoIterator<Item = (K, V)> + FromIterator<(K, V)>,
F: FnMut(&K, V) -> V,
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]);
fn merge<I, F>(self, coll: I, f: F) -> Self where
I: IntoIterator<Item = (K, V)>,
F: FnMut(V, V) -> V,
I: IntoIterator<Item = (K, V)>,
F: FnMut(V, V) -> V,
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]);