Skip to content

Commit c576573

Browse files
committed
Add function nestedmap2 and deepmap
1 parent 28512e7 commit c576573

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/ArraysOfArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using Compat: axes
1111
using UnsafeArrays
1212

1313
include("util.jl")
14+
include("functions.jl")
1415
include("array_of_similar_arrays.jl")
1516
include("vector_of_arrays.jl")
1617

src/array_of_similar_arrays.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ UnsafeArrays.unsafe_uview(A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N} =
178178
ArrayOfSimilarArrays{T,M,N}(uview(A.data))
179179

180180

181+
function nestedmap2(f::Base.Callable, A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N}
182+
new_data = map(f, A.data)
183+
U = eltype(new_data)
184+
ArrayOfSimilarArrays{U,M,N}(new_data)
185+
end
186+
187+
188+
function deepmap(f::Base.Callable, A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N}
189+
new_data = deepmap(f, A.data)
190+
U = eltype(new_data)
191+
ArrayOfSimilarArrays{U,M,N}(new_data)
192+
end
193+
194+
181195

182196
const VectorOfSimilarArrays{
183197
T, M, L,

src/functions.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file is a part of ArraysOfArrays.jl, licensed under the MIT License (MIT).
2+
3+
4+
"""
5+
nestedmap2(f::Base.Callable, A::AbstractArray{<:AbstractArray})
6+
7+
Nested `map` at depth 2. Equivalent to `map(X -> map(f, X) A)`.
8+
"""
9+
function nestedmap2 end
10+
export nestedmap2
11+
12+
nestedmap2(f::Base.Callable, A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N} =
13+
map(X -> map(f, X), A)
14+
15+
16+
17+
"""
18+
deepmap(f::Base.Callable, A::AbstractArray)
19+
deepmap(f::Base.Callable, A::AbstractArray{<:AbstractArray{<:...}})
20+
21+
Applies `map` at the deepest possible layer of nested arrays.
22+
"""
23+
function deepmap end
24+
export deepmap
25+
26+
deepmap(f::Base.Callable, x::Any) = map(f, x)
27+
28+
deepmap(f::Base.Callable, A::AbstractArray{<:AbstractArray}) =
29+
map(X -> deepmap(f, X), A)

src/vector_of_arrays.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ function UnsafeArrays.uview(A::VectorOfArrays)
300300
end
301301

302302

303+
function nestedmap2(f::Base.Callable, A::VectorOfArrays)
304+
new_data = map(f, A.data)
305+
VectorOfArrays(new_data, A.elem_ptr, A.kernel_size, simple_consistency_checks)
306+
end
307+
308+
309+
function deepmap(f::Base.Callable, A::VectorOfArrays)
310+
new_data = deepmap(f, A.data)
311+
VectorOfArrays(new_data, A.elem_ptr, A.kernel_size, simple_consistency_checks)
312+
end
313+
314+
303315

304316
const VectorOfVectors{
305317
T,

0 commit comments

Comments
 (0)