1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::collections::{HashMap,hash_map,BTreeMap,btree_map};
use std::borrow::Borrow;
use std::hash::Hash;
use std::iter::FromIterator;
/// basic protocol for maps.
pub trait Map<K,V> where Self:Sized {
/// a map maps from keys to values.
fn fun<'a,Q:?Sized>(&'a self) -> Box<Fn(&Q) -> Option<&'a V> + 'a> where K:Borrow<Q>, Q:Hash+Ord;
/// adds `v` at `k`.
///
/// like `clojure`'s [`assoc`](http://clojuredocs.org/clojure.core/assoc).
fn inc(self, k:K, v:V) -> Self;
/// removes key `k`.
///
/// like `clojure`'s [`dissoc`](http://clojuredocs.org/clojure.core/dissoc).
fn dec<Q:?Sized>(self, k:&Q) -> Self where K:Borrow<Q>, Q:Hash+Ord;
/// pours another collection into this one.
///
/// like `clojure`'s [`into`](http://clojuredocs.org/clojure.core/into).
fn plus<I>(self, coll:I) -> Self where I:IntoIterator<Item = (K,V)>
{coll.into_iter().fold(self, |m,(k,v)| Map::inc(m,k,v))}
/// `clear`.
fn zero(self) -> Self;
/// `shrink_to_fit`.
fn shrink(self) -> Self;
/// updates the value at `k` by `f`.
///
/// like `clojure`'s [`update`](http://clojuredocs.org/clojure.core/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]);
/// ```
fn update<F>(self, k:K, f:F) -> Self where F:FnOnce(Option<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 update_all<F>(self, mut f:F) -> Self
where Self:IntoIterator<Item = (K,V)> + FromIterator<(K, V)>, F:FnMut(&K,V) -> V
{self.into_iter().map(|(k,v)| {let v = f(&k,v); (k,v)}).collect()}
/// merges `coll` into this one, resolving conflicts by `f`.
///
/// like `clojure`'s [`merge-with`](http://clojuredocs.org/clojure.core/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]);
/// ```
fn merge<I,F>(self, coll:I, mut f:F) -> Self where I:IntoIterator<Item = (K,V)>, F:FnMut(V,V) -> V
{coll.into_iter().fold(self, |m,(k,v)| Map::update(m, k, |opt_u| match opt_u {Some(u) => f(u,v), None => v}))}
}
pub trait MapMut<K,V> {
/// like [`Map::update`](trait.Map.html#tymethod.update) but can be more efficient.
///
/// # example
/// ```
/// use protocoll::{Map,MapMut};
/// use std::collections::HashMap;
/// let a = [0,0,0,1,1,0,0,0];
/// let m1 = a.iter().fold
/// (HashMap::new(), |m,&k| Map::update
/// (m, k, |n| 1 + n.unwrap_or(0)));
/// let m2 = a.iter().fold
/// (HashMap::new(), |mut m, &k|
/// {m.update_mut(k, 0, |n| *n += 1); m});
/// assert_eq!(m1,m2);
/// ```
fn update_mut<F>(&mut self, k:K, fnil:V, f:F) where F:FnOnce(&mut V);
/// like [`Map::update_all`](trait.Map.html#method.update_all) but can be more efficient.
///
/// # example
/// ```
/// use protocoll::MapMut;
/// use std::collections::HashMap;
/// let mut m = [0,0,0,1,1,0,0,0].iter().fold
/// (HashMap::new(), |mut m, &k|
/// {m.update_mut(k, 0, |n| *n += 1); m});
/// m.update_all_mut(|_,v| *v += 1);
/// assert_eq!(7, m[&0]);
/// assert_eq!(3, m[&1]);
/// ```
fn update_all_mut<F>(&mut self, f:F) where F:FnMut(&K, &mut V);
/// like [`Map::merge`](trait.Map.html#method.merge) but can be more efficient.
///
/// # example
/// ```
/// use protocoll::MapMut;
/// use std::collections::HashMap;
/// let m1 = [0,0,0,1,1,0,0,0].iter().fold
/// (HashMap::new(), |mut m, &k|
/// {m.update_mut(k, 0, |n| *n += 1); m});
/// let mut m2 = m1.clone();
/// m2.merge_mut(m1, |u,v| *u += v);
/// assert_eq!(12, m2[&0]);
/// assert_eq!(4, m2[&1]);
/// ```
fn merge_mut<I,F>(&mut self, coll:I, f:F) where I:IntoIterator<Item = (K,V)>, F:FnMut(&mut V, V);
}
impl<K,V> Map<K,V> for HashMap<K,V> where K:Hash+Eq {
fn fun<'a,Q:?Sized>(&'a self) -> Box<Fn(&Q) -> Option<&'a V> + 'a> where K:Borrow<Q>, Q:Hash+Eq
{Box::new(move |k| self.get(k))}
fn inc(mut self, k:K, v:V) -> Self
{self.insert(k,v); self}
fn dec<Q:?Sized>(mut self, k:&Q) -> Self where K:Borrow<Q>, Q:Hash+Eq
{self.remove(k); self}
fn zero(mut self) -> Self
{self.clear(); self}
fn shrink(mut self) -> Self
{self.shrink_to_fit(); self}
fn update<F>(mut self, k:K, f:F) -> Self where F:FnOnce(Option<V>) -> V
{let v = f(self.remove(&k)); Map::inc(self,k,v)}
}
impl<K,V> MapMut<K,V> for HashMap<K,V> where K:Hash+Eq {
fn update_mut<F>(&mut self, k:K, fnil:V, f:F) where F:FnOnce(&mut V)
{f(self.entry(k).or_insert(fnil))}
fn update_all_mut<F>(&mut self, mut f:F) where F:FnMut(&K, &mut V)
{for (k,v) in self {f(k,v)}}
fn merge_mut<I,F>(&mut self, coll:I, mut f:F) where I:IntoIterator<Item = (K,V)>, F:FnMut(&mut V, V)
{for (k,v) in coll
{match self.entry(k)
{hash_map::Entry::Occupied(e) => f(e.into_mut(),v),
hash_map::Entry::Vacant(e) => {e.insert(v);}}}}
}
impl<K,V> Map<K,V> for BTreeMap<K,V> where K:Ord {
fn fun<'a,Q:?Sized>(&'a self) -> Box<Fn(&Q) -> Option<&'a V> + 'a> where K:Borrow<Q>, Q:Ord
{Box::new(move |k| self.get(k))}
fn inc(mut self, k:K, v:V) -> Self
{self.insert(k,v); self}
fn dec<Q:?Sized>(mut self, k:&Q) -> Self where K:Borrow<Q>, Q:Ord
{self.remove(k); self}
fn zero(mut self) -> Self
{self.clear(); self}
fn shrink(self) -> Self
{self}
fn update<F>(mut self, k:K, f:F) -> Self where F:FnOnce(Option<V>) -> V
{let v = f(self.remove(&k)); Map::inc(self,k,v)}
}
impl<K,V> MapMut<K,V> for BTreeMap<K,V> where K:Ord {
fn update_mut<F>(&mut self, k:K, fnil:V, f:F) where F:FnOnce(&mut V)
{f(self.entry(k).or_insert(fnil))}
fn update_all_mut<F>(&mut self, mut f:F) where F:FnMut(&K, &mut V)
{for (k,v) in self {f(k,v)}}
fn merge_mut<I,F>(&mut self, coll:I, mut f:F) where I:IntoIterator<Item = (K,V)>, F:FnMut(&mut V, V)
{for (k,v) in coll
{match self.entry(k)
{btree_map::Entry::Occupied(e) => f(e.into_mut(),v),
btree_map::Entry::Vacant(e) => {e.insert(v);}}}}
}