Skip to content

Commit 8a9290d

Browse files
committed
note on flatten
1 parent cbd7315 commit 8a9290d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/flatten.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Should be equal to `collect(Iterators.flatten(iter))`, but faster.
1010
Acting on a vector of vectors, this is equal to `vcat(iter...)` or `reduce(vcat, iter)`,
1111
and to `vec(stack(iter))` when this is allowed, i.e. when `allequal(length.(iter))`.
1212
13+
!!! Warning
14+
`flatten` is an experiment which got committed here by accident, more or less.
15+
I'm not sure it's part of the long-term future of LazyStack.
16+
1317
# Examples
1418
```
1519
julia> v = flatten((1:2, 8:9))
@@ -96,6 +100,8 @@ end
96100

97101
# _typed_flatten(::Type{T}, ::Union{HasShape, HasLength}, A) where {T} = _flatten(collect(A))
98102

103+
# Non-array iterators, whose elements are arrays or tuples, should probably be collected & sent to the above path.
104+
99105
function _typed_flatten(::Type{T}, ::IteratorSize, A) where {T}
100106
xit = iterate(A)
101107
nothing === xit && return T[]
@@ -297,4 +303,41 @@ vectors of varying eltype
297303
min 2.713 μs, mean 9.107 μs (2 allocations, 78.17 KiB)
298304
min 68.750 μs, mean 77.771 μs (9 allocations, 326.55 KiB)
299305
306+
=#
307+
308+
309+
#=
310+
311+
julia> f(x, y) = reduce(vcat, map(current -> searchsorted(x, current), y)); # collected
312+
313+
julia> g(x, y) = mapreduce(current -> searchsorted(x, current), vcat, y); # pairwise
314+
315+
julia> h(x, y) = flatten(current -> searchsorted(x, current), y); # append!
316+
317+
julia> let
318+
Random.seed!(1)
319+
list = sort(rand(1:99, 10^6))
320+
needed = unique(sort(rand(1:99, 10)))
321+
@btime f($list, $needed) # reduce(vcat, map(...
322+
# @btime g($list, $needed) # mapreduce(...
323+
@btime h($list, $needed) # flatten(...
324+
@btime flatten([searchsorted($list, current) for current in $needed])
325+
end;
326+
min 17.958 μs, mean 74.229 μs (4 allocations, 789.84 KiB)
327+
min 19.917 μs, mean 57.096 μs (2 allocations, 807.81 KiB) # here the guess is good
328+
min 19.542 μs, mean 72.105 μs (3 allocations, 789.70 KiB)
329+
330+
julia> let
331+
Random.seed!(42)
332+
list = sort(rand(1:99, 10^6))
333+
needed = unique(sort(rand(1:99, 10)))
334+
@btime f($list, $needed) # reduce(vcat, map(...
335+
# @btime g($list, $needed) # mapreduce(...
336+
@btime h($list, $needed) # flatten(...
337+
@btime flatten([searchsorted($list, current) for current in $needed])
338+
end;
339+
min 17.791 μs, mean 67.903 μs (4 allocations, 790.41 KiB)
340+
min 37.375 μs, mean 128.942 μs (3 allocations, 2.13 MiB) # here the guess is not so good
341+
min 19.417 μs, mean 77.225 μs (3 allocations, 790.27 KiB)
342+
300343
=#

0 commit comments

Comments
 (0)