-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
functional.jl
410 lines (343 loc) Β· 13.9 KB
/
functional.jl
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# This file is a part of Julia. License is MIT: http://julialang.org/license
# tests related to functional programming functions and styles
# map -- array.jl
@test isequal(map((x)->"$x"[end:end], 9:11), ["9", "0", "1"])
# TODO: @test map!() much more thoroughly
let a = [1.0, 2.0]
map!(sin, a)
@test isequal(a, sin([1.0, 2.0]))
end
# map -- ranges.jl
@test isequal(map(i->sqrt(i), 1:5), [sqrt(i) for i in 1:5])
@test isequal(map(i->sqrt(i), 2:6), [sqrt(i) for i in 2:6])
# map on ranges should evaluate first value only once (#4453)
let io=IOBuffer(3)
map(x->print(io,x), 1:2)
@test takebuf_string(io)=="12"
end
# map over Bottom[] should return Bottom[]
# issue #6719
@test isequal(typeof(map(x -> x, Array(Union{},0))), Array{Union{},1})
# maps of tuples (formerly in test/core.jl) -- tuple.jl
@test map((x,y)->x+y,(1,2,3),(4,5,6)) == (5,7,9)
@test map((x,y)->x+y,
(100001,100002,100003),
(100004,100005,100006)) == (200005,200007,200009)
# maps of strings (character arrays) -- string.jl
@test map((c)->Char(c+1), "abcDEF") == "bcdEFG"
# filter -- array.jl
@test isequal(filter(x->(x>1), [0 1 2 3 2 1 0]), [2, 3, 2])
# TODO: @test_throws isequal(filter(x->x+1, [0 1 2 3 2 1 0]), [2, 3, 2])
@test isequal(filter(x->(x>10), [0 1 2 3 2 1 0]), [])
@test isequal(filter((ss)->length(ss)==3, ["abcd", "efg", "hij", "klmn", "opq"]), ["efg", "hij", "opq"])
# zip and filter iterators
# issue #4718
@test collect(filter(x->x[1], zip([true, false, true, false],"abcd"))) == [(true,'a'),(true,'c')]
let z = zip(1:2)
@test collect(z) == [(1,), (2,)]
# Issue #13979
@test eltype(z) == Tuple{Int}
end
let z = zip(1:2, 3:4)
@test collect(z) == [(1,3), (2,4)]
@test eltype(z) == Tuple{Int,Int}
end
let z = zip(1:2, 3:4, 5:6)
@test collect(z) == [(1,3,5), (2,4,6)]
@test eltype(z) == Tuple{Int,Int,Int}
end
# typed `collect`
@test collect(Float64, Filter(isodd, [1,2,3,4]))[1] === 1.0
# enumerate (issue #6284)
let b = IOBuffer("1\n2\n3\n"), a = []
for (i,x) in enumerate(eachline(b))
push!(a, (i,x))
end
@test a == [(1,"1\n"),(2,"2\n"),(3,"3\n")]
end
# zip eachline (issue #7369)
let zeb = IOBuffer("1\n2\n3\n4\n5\n"),
letters = ['a', 'b', 'c', 'd', 'e'],
res = []
for (number, letter) in zip(eachline(zeb), letters)
push!(res, (parse(Int,strip(number)), letter))
end
@test res == [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
end
@test length(zip(cycle(1:3), 1:7)) == 7
@test length(zip(cycle(1:3), 1:7, cycle(1:3))) == 7
@test length(zip(1:3,Base.product(1:7,cycle(1:3)))) == 3
@test length(zip(1:3,Base.product(1:7,cycle(1:3)),8)) == 1
# rest
# ----
let s = "hello"
_, st = next(s, start(s))
@test collect(rest(s, st)) == ['e','l','l','o']
end
@test_throws MethodError collect(rest(countfrom(1), 5))
# countfrom
# ---------
let i = 0
for j = countfrom(0, 2)
@test j == i*2
i += 1
i <= 10 || break
end
end
# take
# ----
let t = take(0:2:8, 10), i = 0
@test length(collect(t)) == 5
for j = t
@test j == i*2
i += 1
end
@test i == 5
end
let i = 0
for j = take(0:2:100, 10)
@test j == i*2
i += 1
end
@test i == 10
end
@test length(take(1:3,typemax(Int))) == 3
@test length(take(countfrom(1),3)) == 3
@test length(take(1:6,3)) == 3
# drop
# ----
let i = 0
for j = drop(0:2:10, 2)
@test j == (i+2)*2
i += 1
end
@test i == 4
end
@test length(drop(1:3,typemax(Int))) == 0
@test Base.iteratorsize(drop(countfrom(1),3)) == Base.IsInfinite()
@test_throws MethodError length(drop(countfrom(1), 3))
# cycle
# -----
let i = 0
for j = cycle(0:3)
@test j == i % 4
i += 1
i <= 10 || break
end
end
# repeated
# --------
let i = 0
for j = repeated(1, 10)
@test j == 1
i += 1
end
@test i == 10
end
let i = 0
for j = repeated(1)
@test j == 1
i += 1
i <= 10 || break
end
end
# product
# -------
# empty?
for itr in [Base.product(1:0),
Base.product(1:2, 1:0),
Base.product(1:0, 1:2),
Base.product(1:0, 1:1, 1:2),
Base.product(1:1, 1:0, 1:2),
Base.product(1:1, 1:2 ,1:0)]
@test isempty(itr)
@test isempty(collect(itr))
end
# collect a product - first iterators runs faster
@test collect(Base.product(1:2)) == [(i,) for i=1:2]
@test collect(Base.product(1:2, 3:4)) == [(i, j) for i=1:2, j=3:4]
@test collect(Base.product(1:2, 3:4, 5:6)) == [(i, j, k) for i=1:2, j=3:4, k=5:6]
# iteration order
let expected = [(1,3,5), (2,3,5), (1,4,5), (2,4,5), (1,3,6), (2,3,6), (1,4,6), (2,4,6)]
i = 1
for el in Base.product(1:2, 3:4, 5:6)
@test el == expected[i]
i+=1
end
end
# is this the correct behaviour?
@test collect(Base.product([1 2; 3 4], [5 6; 7 8])) == [(1,5) (1,7) (1,6) (1,8);
(3,5) (3,7) (3,6) (3,8);
(2,5) (2,7) (2,6) (2,8);
(4,5) (4,7) (4,6) (4,8)]
let
a, b, c, d, e = 1:2, 1.0:10.0, 4f0:6f0, 0x01:0x08, Int8(1):Int8(0)
# length
@test length(Base.product(a)) == 2
@test length(Base.product(a, b)) == 20
@test length(Base.product(a, b, c)) == 60
@test length(Base.product(a, b, c, d)) == 480
@test length(Base.product(a, b, c, d, e)) == 0
# size
@test size(Base.product(a)) == (2, )
@test size(Base.product(a, b)) == (2, 10)
@test size(Base.product(a, b, c)) == (2, 10, 3)
@test size(Base.product(a, b, c, d)) == (2, 10, 3, 8)
@test size(Base.product(a, b, c, d, e)) == (2, 10, 3, 8, 0)
# eltype
@test eltype(Base.product(a)) == Tuple{Int}
@test eltype(Base.product(a, b)) == Tuple{Int, Float64}
@test eltype(Base.product(a, b, c)) == Tuple{Int, Float64, Float32}
@test eltype(Base.product(a, b, c, d)) == Tuple{Int, Float64, Float32, UInt8}
@test eltype(Base.product(a, b, c, d, e)) == Tuple{Int, Float64, Float32, UInt8, Int8}
# ndims
@test ndims(Base.product(a)) == 1
@test ndims(Base.product(a, b)) == 2
@test ndims(Base.product(a, b, c)) == 3
@test ndims(Base.product(a, b, c, d)) == 4
@test ndims(Base.product(a, b, c, d, e)) == 5
f, g, h = randn(1, 1), randn(1, 1, 1), randn(1, 1, 1, 1)
@test ndims(Base.product(f)) == ndims(collect(Base.product(f))) == 2
@test ndims(Base.product(g)) == ndims(collect(Base.product(g))) == 3
@test ndims(Base.product(h)) == ndims(collect(Base.product(h))) == 4
@test ndims(Base.product(f, f)) == ndims(collect(Base.product(f, f))) == 2
@test ndims(Base.product(f, g)) == ndims(collect(Base.product(f, g))) == 2
@test ndims(Base.product(g, g)) == ndims(collect(Base.product(g, g))) == 2
@test ndims(Base.product(g, h)) == ndims(collect(Base.product(g, h))) == 2
@test ndims(Base.product(h, h)) == ndims(collect(Base.product(h, h))) == 2
@test ndims(Base.product(f, f, f)) == ndims(collect(Base.product(f, f, f))) == 3
@test ndims(Base.product(f, f, g)) == ndims(collect(Base.product(f, f, g))) == 3
@test ndims(Base.product(f, g, g)) == ndims(collect(Base.product(f, g, g))) == 3
@test ndims(Base.product(g, g, g)) == ndims(collect(Base.product(g, g, g))) == 3
@test ndims(Base.product(g, g, h)) == ndims(collect(Base.product(g, g, h))) == 3
@test ndims(Base.product(g, h, h)) == ndims(collect(Base.product(g, h, h))) == 3
@test ndims(Base.product(h, h, h)) == ndims(collect(Base.product(h, h, h))) == 3
@test ndims(Base.product(f, f, f)) == ndims(collect(Base.product(f, f, f))) == 3
@test ndims(Base.product(g, g, g, g)) == ndims(collect(Base.product(g, g, g, g))) == 4
@test ndims(Base.product(h, h, h, h)) == ndims(collect(Base.product(h, h, h, h))) == 4
end
# iteratorsize trait business
let f1 = Filter(i->i>0, 1:10)
@test Base.iteratorsize(Base.product(f1)) == Base.SizeUnknown()
@test Base.iteratorsize(Base.product(1:2, f1)) == Base.SizeUnknown()
@test Base.iteratorsize(Base.product(f1, 1:2)) == Base.SizeUnknown()
@test Base.iteratorsize(Base.product(f1, f1)) == Base.SizeUnknown()
@test Base.iteratorsize(Base.product(f1, countfrom(1))) == Base.IsInfinite()
@test Base.iteratorsize(Base.product(countfrom(1), f1)) == Base.IsInfinite()
end
@test Base.iteratorsize(Base.product(1:2, countfrom(1))) == Base.IsInfinite()
@test Base.iteratorsize(Base.product(countfrom(1), 1:2)) == Base.IsInfinite()
@test Base.iteratorsize(Base.product(1:2)) == Base.HasShape()
@test Base.iteratorsize(Base.product(1:2, 1:2)) == Base.HasShape()
@test Base.iteratorsize(Base.product(take(1:2, 1), take(1:2, 1))) == Base.HasShape()
@test Base.iteratorsize(Base.product(take(1:2, 2))) == Base.HasLength()
@test Base.iteratorsize(Base.product([1 2; 3 4])) == Base.HasShape()
# iteratoreltype trait business
let f1 = Filter(i->i>0, 1:10)
@test Base.iteratoreltype(Base.product(f1)) == Base.HasEltype() # FIXME? eltype(f1) is Any
@test Base.iteratoreltype(Base.product(1:2, f1)) == Base.HasEltype() # FIXME? eltype(f1) is Any
@test Base.iteratoreltype(Base.product(f1, 1:2)) == Base.HasEltype() # FIXME? eltype(f1) is Any
@test Base.iteratoreltype(Base.product(f1, f1)) == Base.HasEltype() # FIXME? eltype(f1) is Any
@test Base.iteratoreltype(Base.product(f1, countfrom(1))) == Base.HasEltype() # FIXME? eltype(f1) is Any
@test Base.iteratoreltype(Base.product(countfrom(1), f1)) == Base.HasEltype() # FIXME? eltype(f1) is Any
end
@test Base.iteratoreltype(Base.product(1:2, countfrom(1))) == Base.HasEltype()
@test Base.iteratoreltype(Base.product(countfrom(1), 1:2)) == Base.HasEltype()
@test Base.iteratoreltype(Base.product(1:2)) == Base.HasEltype()
@test Base.iteratoreltype(Base.product(1:2, 1:2)) == Base.HasEltype()
@test Base.iteratoreltype(Base.product(take(1:2, 1), take(1:2, 1))) == Base.HasEltype()
@test Base.iteratoreltype(Base.product(take(1:2, 2))) == Base.HasEltype()
@test Base.iteratoreltype(Base.product([1 2; 3 4])) == Base.HasEltype()
# flatten
# -------
import Base.flatten
@test collect(flatten(Any[1:2, 4:5])) == Any[1,2,4,5]
@test collect(flatten(Any[flatten(Any[1:2, 6:5]), flatten(Any[10:7, 10:9])])) == Any[1,2]
@test collect(flatten(Any[flatten(Any[1:2, 4:5]), flatten(Any[6:7, 8:9])])) == Any[1,2,4,5,6,7,8,9]
@test collect(flatten(Any[flatten(Any[1:2, 6:5]), flatten(Any[6:7, 8:9])])) == Any[1,2,6,7,8,9]
@test collect(flatten(Any[2:1])) == Any[]
@test eltype(flatten(UnitRange{Int8}[1:2, 3:4])) == Int8
@test_throws ArgumentError collect(flatten(Any[]))
# foreach
let
a = []
foreach(()->push!(a,0))
@test a == [0]
a = []
foreach(x->push!(a,x), [1,5,10])
@test a == [1,5,10]
a = []
foreach((args...)->push!(a,args), [2,4,6], [10,20,30])
@test a == [(2,10),(4,20),(6,30)]
end
# generators (#4470, #14848)
@test sum(i/2 for i=1:2) == 1.5
@test collect(2i for i=2:5) == [4,6,8,10]
@test collect((i+10j for i=1:2,j=3:4)) == [31 41; 32 42]
@test collect((i+10j for i=1:2,j=3:4,k=1:1)) == reshape([31 41; 32 42], (2,2,1))
let I = Base.IteratorND(1:27,(3,3,3))
@test collect(I) == reshape(1:27,(3,3,3))
@test size(I) == (3,3,3)
@test length(I) == 27
@test eltype(I) === Int
@test ndims(I) == 3
end
let A = collect(Base.Generator(x->2x, Real[1.5,2.5]))
@test A == [3,5]
@test isa(A,Vector{Float64})
end
let f(g) = (@test size(g.iter)==(2,3))
f(i+j for i=1:2, j=3:5)
end
@test_throws DimensionMismatch Base.IteratorND(1:2, (2,3))
@test collect(Base.Generator(+, [1,2], [10,20])) == [11,22]
# generator ndims #16394
let gens_dims = [((i for i = 1:5), 1),
((i for i = 1:5, j = 1:5), 2),
((i for i = 1:5, j = 1:5, k = 1:5), 3),
((i for i = Array{Int}()), 0),
((i for i = Array{Int}(1)), 1),
((i for i = Array{Int}(1, 2)), 2),
((i for i = Array{Int}(1, 2, 3)), 3)]
for (gen, dim) in gens_dims
@test ndims(gen) == ndims(collect(gen)) == dim
end
end
# generator with destructuring
let d = Dict(:a=>1, :b=>2), a = Dict(3=>4, 5=>6)
@test Dict( v=>(k,) for (k,v) in d) == Dict(2=>(:b,), 1=>(:a,))
@test Dict( (x,b)=>(c,y) for (x,c) in d, (b,y) in a ) == Dict((:a,5)=>(1,6),(:b,5)=>(2,6),(:a,3)=>(1,4),(:b,3)=>(2,4))
end
# partition(c, n)
let v = collect(Base.partition([1,2,3,4,5], 1))
@test all(i->v[i][1] == i, v)
end
let v = collect(Base.partition([1,2,3,4,5], 2))
@test v[1] == [1,2]
@test v[2] == [3,4]
@test v[3] == [5]
end
let v = collect(Base.partition(enumerate([1,2,3,4,5]), 3))
@test v[1] == [(1,1),(2,2),(3,3)]
@test v[2] == [(4,4),(5,5)]
end
for n in [5,6]
@test collect(Base.partition([1,2,3,4,5], n))[1] == [1,2,3,4,5]
@test collect(Base.partition(enumerate([1,2,3,4,5]), n))[1] ==
[(1,1),(2,2),(3,3),(4,4),(5,5)]
end
@test join(map(x->string(x...), Base.partition("Hello World!", 5)), "|") ==
"Hello| Worl|d!"
let s = "Monkey πππ"
tf = (n)->join(map(x->string(x...), Base.partition(s,n)), "|")
@test tf(10) == s
@test tf(9) == "Monkey ππ|π"
@test tf(8) == "Monkey π|ππ"
@test tf(7) == "Monkey |πππ"
@test tf(6) == "Monkey| πππ"
@test tf(5) == "Monke|y πππ"
@test tf(4) == "Monk|ey π|ππ"
@test tf(3) == "Mon|key| ππ|π"
@test tf(2) == "Mo|nk|ey| π|ππ"
@test tf(1) == "M|o|n|k|e|y| |π|π|π"
end