-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfoldable.rs
45 lines (38 loc) · 1.13 KB
/
foldable.rs
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
use lift::Foldable;
use std::hash::Hash;
use std::collections::linked_list::LinkedList;
use std::collections::vec_deque::VecDeque;
use std::collections::{BinaryHeap, BTreeSet, HashSet};
//Implementation of Foldable for Vec
foldable!(Vec);
//Implementation of Foldable for LinkedList
foldable!(LinkedList);
//Implementation of Foldable for VeqDeque
foldable!(VecDeque);
//Implemenatation of Foldable for BinaryHeap
impl<T: Ord> Foldable for BinaryHeap<T> {
type A = T;
fn foldr<F>(&self, accum: Self::A, f: F) -> Self::A
where F: FnMut(Self::A, &Self::A) -> Self::A
{
self.iter().fold(accum, f)
}
}
//Implementation of Foldable for BTreeSet
impl<T: Ord> Foldable for BTreeSet<T> {
type A = T;
fn foldr<F>(&self, accum: Self::A, f: F) -> Self::A
where F: FnMut(Self::A, &Self::A) -> Self::A
{
self.iter().fold(accum, f)
}
}
//Implementation of Foldable for HashSet
impl<T: Hash + Eq> Foldable for HashSet<T> {
type A = T;
fn foldr<F>(&self, accum: Self::A, f: F) -> Self::A
where F: FnMut(Self::A, &Self::A) -> Self::A
{
self.iter().fold(accum, f)
}
}