Trait Map

Source
pub trait Map<K, V>
where Self: Sized,
{ // Required methods fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a> where K: Borrow<Q>, Q: Hash + Ord + ?Sized; fn inc(self, k: K, v: V) -> Self; fn dec<Q>(self, k: &Q) -> Self where K: Borrow<Q>, Q: Hash + Ord + ?Sized; fn zero(self) -> Self; fn shrink(self) -> Self; fn update<F>(self, k: K, f: F) -> Self where F: FnOnce(Option<V>) -> V; // Provided methods 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 { ... } }
Expand description

basic protocol for maps.

Required Methods§

Source

fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
where K: Borrow<Q>, Q: Hash + Ord + ?Sized,

a map maps from keys to values.

Source

fn inc(self, k: K, v: V) -> Self

adds v at k.

like clojure’s assoc.

Source

fn dec<Q>(self, k: &Q) -> Self
where K: Borrow<Q>, Q: Hash + Ord + ?Sized,

removes key k.

like clojure’s dissoc.

Source

fn zero(self) -> Self

clear.

Source

fn shrink(self) -> Self

shrink_to_fit.

Source

fn update<F>(self, k: K, f: F) -> Self
where F: FnOnce(Option<V>) -> V,

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§

Source

fn plus<I>(self, coll: I) -> Self
where I: IntoIterator<Item = (K, V)>,

pours another collection into this one.

like clojure’s into.

Source

fn update_all<F>(self, f: F) -> Self
where 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]);
Source

fn merge<I, F>(self, coll: I, f: F) -> Self
where 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]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<K, V> Map<K, V> for BTreeMap<K, V>
where K: Ord,

Source§

fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
where K: Borrow<Q>, Q: Ord + ?Sized,

Source§

fn inc(self, k: K, v: V) -> Self

Source§

fn dec<Q>(self, k: &Q) -> Self
where K: Borrow<Q>, Q: Ord + ?Sized,

Source§

fn zero(self) -> Self

Source§

fn shrink(self) -> Self

Source§

fn update<F>(self, k: K, f: F) -> Self
where F: FnOnce(Option<V>) -> V,

Source§

impl<K, V> Map<K, V> for HashMap<K, V>
where K: Hash + Eq,

Source§

fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn inc(self, k: K, v: V) -> Self

Source§

fn dec<Q>(self, k: &Q) -> Self
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source§

fn zero(self) -> Self

Source§

fn shrink(self) -> Self

Source§

fn update<F>(self, k: K, f: F) -> Self
where F: FnOnce(Option<V>) -> V,

Implementors§

Source§

impl<K, V> Map<K, V> for VecSortedMap<K, V>
where K: Ord,