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
use super::*;
pub(super) fn new(iterable: &Rc<Class>) -> Rc<Class> {
Class::new(
"Iterator".into(),
Class::join_class_maps(
Class::map_from_funcs(vec![
NativeFunction::new(
"to",
["self", "type"],
"Converts this iterator into the target collection type",
|globals, args, _| {
let mut args = args.into_iter();
let owner = args.next().unwrap();
let type_ = args.next().unwrap();
type_.apply_method(globals, "__from_iterable", vec![owner], None)
},
),
NativeFunction::new(
"list",
["self"],
"Converts this iterator into a list",
|globals, args, _| {
let mut args = args.into_iter();
let owner = args.next().unwrap();
Ok(Value::from(owner.unpack(globals)?))
},
),
NativeFunction::new(
"set",
["self"],
"Converts this iterator into a set",
|globals, args, _| {
let mut args = args.into_iter();
let owner = args.next().unwrap();
Ok(Value::from(owner.unpack_into_set(globals)?))
},
),
NativeFunction::new(
"enumerate",
ArgSpec::builder().req("self").def("start", 0),
"",
|_globals, args, _| {
let mut args = args.into_iter();
let owner = args.next().unwrap();
let mut i = args.next().unwrap().number()?;
Ok(
NativeGenerator::new("Iterator.enumerate", move |globals, arg| {
match owner.resume(globals, arg) {
ResumeResult::Yield(value) => {
let n = i;
i += 1.0;
ResumeResult::Yield(vec![Value::from(n), value].into())
}
r => r,
}
})
.into(),
)
},
),
NativeFunction::new(
"zip",
ArgSpec::builder().req("self").var("others"),
"",
|globals, args, _| {
let iters = args
.into_iter()
.map(|v| v.iter(globals))
.collect::<Result<Vec<_>>>()?;
Ok(NativeGenerator::new("Iterator.zip", move |globals, _arg| {
let mut results = Vec::new();
for iter in &iters {
match iter.resume(globals, Value::Nil) {
ResumeResult::Yield(x) => results.push(x),
r @ ResumeResult::Err(_) => return r,
ResumeResult::Return(_) => {
return ResumeResult::Return(Value::Nil)
}
}
}
ResumeResult::Yield(results.into())
})
.into())
},
),
NativeFunction::new(
"filter",
ArgSpec::builder().req("self").def("f", ()),
"",
|_globals, args, _| {
let mut args = args.into_iter();
let owner = args.next().unwrap();
let f = args.next().unwrap();
Ok(
NativeGenerator::new("Iterator.filter", move |globals, _| loop {
match owner.resume(globals, Value::Nil) {
ResumeResult::Yield(value) => {
let cond = if f.is_nil() {
value.truthy()
} else {
gentry!(f.apply(globals, vec![value.clone()], None))
.truthy()
};
if cond {
return ResumeResult::Yield(value);
}
}
r => return r,
}
})
.into(),
)
},
),
NativeFunction::new(
"map",
ArgSpec::builder().req("self").def("f", ()),
concat!(
"Duals as a map (in the sense of monads) and map as in, ",
"the data structure.\n",
"It is determined based on the presence of a second argument",
),
|globals, args, _| {
if args[1].is_nil() {
// converts the iterable into a Map
let mut args = args.into_iter();
let owner = args.next().unwrap();
let map = owner
.unpack(globals)?
.into_iter()
.map(|p| p.unpack_keyval(globals))
.collect::<Result<Map>>()?;
Ok(map.into())
} else {
// creates a new iterable with 'f' applied to all arguments
let mut args = args.into_iter();
let owner = args.next().unwrap();
let f = args.next().unwrap();
Ok(NativeGenerator::new(
"Iterator.map",
move |globals, arg| match owner.resume(globals, arg) {
ResumeResult::Yield(value) => ResumeResult::Yield(gentry!(
f.apply(globals, vec![value], None)
)),
r => r,
},
)
.into())
}
},
),
]),
vec![iterable],
),
HashMap::new(),
)
}