forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.j
227 lines (208 loc) · 4.19 KB
/
reduce.j
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
## reductions ##
function reduce(op, itr)
if is(op,max)
return max(itr)
elseif is(op,min)
return min(itr)
elseif is(op,+)
return sum(itr)
elseif is(op,*)
return prod(itr)
elseif is(op,any)
return any(itr)
elseif is(op,all)
return all(itr)
end
s = start(itr)
if done(itr, s)
return op() # empty collection
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = op(v,x)
end
return v
end
function max(itr)
s = start(itr)
if done(itr, s)
return typemin(eltype(itr))
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = max(v,x)
end
return v
end
function min(itr)
s = start(itr)
if done(itr, s)
return typemax(eltype(itr))
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = min(v,x)
end
return v
end
function sum(itr)
s = start(itr)
if done(itr, s)
return +()
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = v+x
end
return v
end
function prod(itr)
s = start(itr)
if done(itr, s)
return *()
end
(v, s) = next(itr, s)
while !done(itr, s)
(x, s) = next(itr, s)
v = v*x
end
return v
end
function reduce(op::Function, v0, itr)
v = v0
if is(op,max)
for x in itr
v = max(v,x)
end
elseif is(op,min)
for x in itr
v = min(v,x)
end
elseif is(op,+)
for x in itr
v = v+x
end
elseif is(op,*)
for x in itr
v = v*x
end
else
u = v0
for x in itr
u = op(u,x)
end
return u
end
return v
end
function mapreduce(op, f, itr)
s = start(itr)
if done(itr, s)
return op() # empty collection
end
(x, s) = next(itr, s)
v = f(x)
while !done(itr, s)
(x, s) = next(itr, s)
v = op(v,f(x))
end
return v
end
function mapreduce(op::Function, f::Function, v0, itr)
v = v0
for x in itr
v = op(v,f(x))
end
return v
end
function any(itr)
for x in itr
if x
return true
end
end
return false
end
any(args::Bool...) = any(args)
function all(itr)
for x in itr
if !x
return false
end
end
return true
end
all(args::Bool...) = all(args)
max(f::Function, itr) = mapreduce(max, f, itr)
min(f::Function, itr) = mapreduce(min, f, itr)
sum(f::Function, itr) = mapreduce(+, f, itr)
prod(f::Function, itr) = mapreduce(*, f, itr)
any(f::Function, itr) = anyp(f, itr)
all(f::Function, itr) = allp(f, itr)
count(f::Function, itr) = countp(f, itr)
function count(itr)
c = 0
for x in itr
c += (x ? 1 : 0)
end
return c
end
function countp(pred, itr)
c = 0
for x in itr
if pred(x)
c += 1
end
end
return c
end
function anyp(pred, itr)
for x in itr
if pred(x)
return true
end
end
return false
end
function allp(pred, itr)
for x in itr
if !pred(x)
return false
end
end
return true
end
function contains(itr, x)
for y in itr
if isequal(y,x)
return true
end
end
return false
end
## Scans ##
scan(op::Function, x::()) = ()
function scan(op::Function, x::Tuple)
n = length(x)
s = (x[1],)
for i=2:n
s = tuple(s..., op(s[i-1], x[i]))
end
return s
end
cumsum(itr...) = cumsum(itr)
cumsum(x::NTuple{1,Number}) = (x[1],)
cumsum(x::NTuple{2,Number}) = (x[1], x[1]+x[2])
cumsum(x::NTuple{3,Number}) = (x[1], x[1]+x[2], x[1]+x[2]+x[3])
cumsum(x::NTuple{4,Number}) = (x[1], x[1]+x[2], x[1]+x[2]+x[3], x[1]+x[2]+x[3]+x[4])
cumsum(itr::Tuple) = scan(+, itr)
cumprod(itr...) = cumprod(itr)
cumprod(x::NTuple{1,Number}) = (x[1],)
cumprod(x::NTuple{2,Number}) = (x[1], x[1]*x[2])
cumprod(x::NTuple{3,Number}) = (x[1], x[1]*x[2], x[1]*x[2]*x[3])
cumprod(x::NTuple{4,Number}) = (x[1], x[1]*x[2], x[1]*x[2]*x[3], x[1]*x[2]*x[3]*x[4])
cumprod(itr::Tuple) = scan(*, itr)