-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorialmapreduce.rs
More file actions
101 lines (88 loc) · 2.51 KB
/
Copy pathtutorialmapreduce.rs
File metadata and controls
101 lines (88 loc) · 2.51 KB
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
extern crate collections;
use std::fmt::Show;
use std::hash::Hash;
use collections::HashMap;
fn main() {
// some strings with some words
let mut strings: Vec<String> = vec!("these are a bunch of words".to_string(),
"those are a bunch of words too".to_string(),
"lots of words".to_string(),
"there certainly are a lot of words floating around here".to_string(),
"never before have I seen so many words just sitting about".to_string(),
"with not a thing to do".to_string());
// function for map
fn create_pairs(s: &String) -> Vec<(String, int)> {
let mut retvals: Vec<(String,int)> = vec!();
for word in s.as_slice().split(' ') {
retvals.push((word.to_string(), 1));
}
retvals
}
// function for reduce
fn reduce_pairs(key: String, vals: Vec<int>) -> Vec<(String, int)> {
let mut total: int = 0;
for val in vals.iter() {
total += *val;
}
vec!((key, total))
}
// let's do it
strings.mapreduce::<String,int>(create_pairs, reduce_pairs);
}
trait MapReduce {
fn mapreduce<K: Clone + Show + Hash + Equiv<K> + Eq + Send, V: Clone + Show + Send>(&mut self, fn(&String) -> Vec<(K, V)>,
fn(K, Vec<V>) -> Vec<(K, V)>);
}
impl MapReduce for Vec<String> {
fn mapreduce<K: Clone + Show + Hash + Equiv<K> + Eq + Send, V: Clone + Show + Send>(&mut self, mapf: fn(&String) -> Vec<(K, V)>,
redf: fn(K, Vec<V>) -> Vec<(K, V)>) {
let (sender, receiver): (Sender<Vec<(K, V)>>, Receiver<Vec<(K, V)>>) = channel();
let mut tasks: int = 0;
// map
for item in self.iter() {
tasks += 1;
let item_owned = item.clone();
let sender_child = sender.clone();
spawn(proc() {
sender_child.send(mapf(&item_owned));
});
}
// intermediate
let mut kv_map: HashMap<K, Vec<V>> = HashMap::new();
for _ in range(0, tasks) {
let ivals: Vec<(K, V)> = receiver.recv();
for pair in ivals.iter() {
let mut key: K;
let mut val: V;
match pair.clone() {
(a, b) => {
key = a.clone();
val = b.clone();
}
}
if kv_map.contains_key_equiv(&key) {
kv_map.get_mut(&key).push(val);
}
else {
kv_map.find_or_insert(key, vec!(val));
}
}
}
// reduce
tasks = 0;
for key in kv_map.keys() {
tasks += 1;
let vals = kv_map.get(key).clone();
let key_owned = key.clone();
let sender_child = sender.clone();
spawn(proc() {
sender_child.send(redf(key_owned, vals));
});
}
// print final values
for _ in range(0, tasks) {
let rvals: Vec<(K, V)> = receiver.recv();
println!("{}", rvals);
}
}
}