Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsumes flatmap functionality under flatten #45985

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Build system changes
New library functions
---------------------

* `Iterators.flatmap` was added ([#44792]).
* `flatten(f, c...)` was added ([#44792], ), which implements `flatmap` both as iterators and
producing arrays.
* New helper `Splat(f)` which acts like `x -> f(x...)`, with pretty printing for
inspecting which function `f` was originally wrapped. ([#42717])
* New `pkgversion(m::Module)` function to get the version of the package that loaded
Expand Down
57 changes: 57 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3097,6 +3097,63 @@ julia> map(+, [1 2; 3 4], [1,10,100,1000], zeros(3,1)) # iterates until 3rd is
"""
map(f, iters...) = collect(Generator(f, iters...))

"""
flatten(A)

Takes a collection of collections and produce an array with the concatenation of all internal elements.

See also [`Iterators.flatten`](@ref)

!!! compat "Julia 1.9"
This function was added in Julia 1.9.

# Examples
```jldoctest
julia> flatten([1:2,1:3,2:4])
9-element Vector{Int64}:
1
2
1
2
3
2
3
4
```
"""
flatten(A) = collect(Iterators.flatten(A))

"""
flatten(f, c...)

Equivalent to `flatten(map(f, c...))`.

See also [`Iterators.flatten`](@ref), [`map`](@ref)

!!! compat "Julia 1.9"
This function was added in Julia 1.9.

# Examples
```jldoctest
julia> flatten(n -> -n:2:n, 1:3)
9-element Vector{Int64}:
-1
1
-2
0
2
-3
-1
1
3
```

# Extended help

This version of the `flatten` method essentialy implements what's called `flatmap` or `bind` in other languages.
"""
flatten(f, A...) = collect(Iterators.flatten(f, A...))

# multi-item push!, pushfirst! (built on top of type-specific 1-item version)
# (note: must not cause a dispatch loop when 1-item case is not defined)
push!(A, a, b) = push!(push!(A, a), b)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ export
lastindex,
filter!,
filter,
flatten,
foldl,
foldr,
foreach,
Expand Down
56 changes: 28 additions & 28 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import .Base:
getindex, setindex!, get, iterate,
popfirst!, isdone, peek, intersect

export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, flatmap
export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten

if Base !== Core.Compiler
export partition
Expand Down Expand Up @@ -1141,6 +1141,33 @@ julia> [(x,y) for x in 0:1 for y in 'a':'c'] # collects generators involving It
"""
flatten(itr) = Flatten(itr)

"""
Iterators.flatten(f, iterators...)

Equivalent to `flatten(map(f, iterators...))`.

See also [`Iterators.flatten`](@ref), [`Iterators.map`](@ref).

!!! compat "Julia 1.9"
This function was added in Julia 1.9.

# Examples
```jldoctest
julia> Iterators.flatten(n -> -n:2:n, 1:3) |> collect
9-element Vector{Int64}:
-1
1
-2
0
2
-3
-1
1
3
```
"""
flatten(f, c...) = flatten(map(f, c...))

eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I))
eltype(::Type{Flatten{Tuple{}}}) = eltype(Tuple{})
IteratorEltype(::Type{Flatten{I}}) where {I} = _flatteneltype(I, IteratorEltype(I))
Expand Down Expand Up @@ -1187,33 +1214,6 @@ end
reverse(f::Flatten) = Flatten(reverse(itr) for itr in reverse(f.it))
last(f::Flatten) = last(last(f.it))

"""
Iterators.flatmap(f, iterators...)

Equivalent to `flatten(map(f, iterators...))`.

See also [`Iterators.flatten`](@ref), [`Iterators.map`](@ref).

!!! compat "Julia 1.9"
This function was added in Julia 1.9.

# Examples
```jldoctest
julia> Iterators.flatmap(n->-n:2:n, 1:3) |> collect
9-element Vector{Int64}:
-1
1
-2
0
2
-3
-1
1
3
```
"""
flatmap(f, c...) = flatten(map(f, c...))

if Base !== Core.Compiler # views are not defined
@doc """
partition(collection, n)
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Base.Iterators.cycle
Base.Iterators.repeated
Base.Iterators.product
Base.Iterators.flatten
Base.Iterators.flatmap
Base.Iterators.partition
Base.Iterators.map
Base.Iterators.filter
Expand Down
16 changes: 8 additions & 8 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,26 +504,26 @@ end
# see #29112, #29464, #29548
@test Base.return_types(Base.IteratorEltype, Tuple{Array}) == [Base.HasEltype]

# flatmap
# flatten as flatmap
# -------
@test flatmap(1:3) do j flatmap(1:3) do k
@test flatten(1:3) do j flatten(1:3) do k
j!=k ? ((j,k),) : ()
end end |> collect == [(j,k) for j in 1:3 for k in 1:3 if j!=k]
# Test inspired by the monad associativity law
fmf(x) = x<0 ? () : (x^2,)
fmg(x) = x<1 ? () : (x/2,)
fmdata = -2:0.75:2
fmv1 = flatmap(tuple.(fmdata)) do h
flatmap(h) do x
fmv1 = flatten(tuple.(fmdata)) do h
flatten(h) do x
gx = fmg(x)
flatmap(gx) do x
flatten(gx) do x
fmf(x)
end
end
end
fmv2 = flatmap(tuple.(fmdata)) do h
gh = flatmap(h) do x fmg(x) end
flatmap(gh) do x fmf(x) end
fmv2 = flatten(tuple.(fmdata)) do h
gh = flatten(h) do x fmg(x) end
flatten(gh) do x fmf(x) end
end
@test all(fmv1 .== fmv2)

Expand Down