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

Implements flatmap #44792

Merged
merged 14 commits into from
Apr 7, 2022
Prev Previous commit
Next Next commit
renamed astuple to monuple and added exports
  • Loading branch information
nlw0 committed Apr 5, 2022
commit 38fa2e3024c3e4de85c2f851176b852c242a2f6b
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ export
something,
isnothing,
nonmissingtype,
monuple,

# time
sleep,
Expand Down
2 changes: 1 addition & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import .Base:
getindex, setindex!, get, iterate,
popfirst!, isdone, peek

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

"""
Iterators.map(f, iterators...)
Expand Down
10 changes: 5 additions & 5 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ foreach(f, itr::Tuple) = foldl((_, x) -> (f(x); nothing), itr, init=nothing)
foreach(f, itrs::Tuple...) = foldl((_, xs) -> (f(xs...); nothing), zip(itrs...), init=nothing)

"""
astuple(x::Union{X, Nothing})
monuple(x::Union{X, Nothing})

Converts values so that `nothing` becomes `()` and any other values are wrapped into a singleton tuple.
Converts values so that `nothing` becomes `()` and any other values are wrapped into a singleton tuple, also known as a monuple.

# Example

Expand All @@ -577,7 +577,7 @@ julia> filter(!isnothing, data)
RegexMatch("xo", 1="xo")
RegexMatch("x", 1="x")

julia> collect(Iterators.flatten(astuple.(data)))
julia> collect(Iterators.flatten(monuple.(data)))
3-element Vector{RegexMatch}:
RegexMatch("x", 1="x")
RegexMatch("xo", 1="xo")
Expand All @@ -587,7 +587,7 @@ julia> [optx for optx in data if !isnothing(optx) && optx[1] != "x"]
1-element Vector{RegexMatch}:
RegexMatch("xo", 1="xo")

julia> Iterators.flatmap(astuple.(data)) do optx
julia> Iterators.flatmap(monuple.(data)) do optx
Iterators.flatmap(optx) do x
x[1] == "x" ? () : (x,)
end
Expand All @@ -596,4 +596,4 @@ julia> Iterators.flatmap(astuple.(data)) do optx
RegexMatch("xo", 1="xo")
```
"""
astuple(x::Union{X, Nothing}) where {X} = isnothing(x) ? () : (x,)
monuple(x::Union{X, Nothing}) where {X} = isnothing(x) ? () : (x,)
10 changes: 5 additions & 5 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ end
@test length(flatten(1:6)) == 6
@test collect(flatten(Any[])) == Any[]
@test collect(flatten(())) == Union{}[]
@test collect(flatten(astuple.([1,nothing]))) == [1]
@test collect(flatten(monuple.([1,nothing]))) == [1]
@test_throws ArgumentError length(flatten(NTuple[(1,), ()])) # #16680
@test_throws ArgumentError length(flatten([[1], [1]]))

Expand All @@ -488,15 +488,15 @@ fmg(x) = x<1 ? () : (x/2,)
fmdata = -2:0.75:2
fmv1 = flatmap(tuple.(fmdata)) do h
flatmap(h) do x
fx = fmg(x)
flatmap(fx) do x
gx = fmg(x)
flatmap(gx) do x
fmf(x)
end
end
end
fmv2 = flatmap(tuple.(fmdata)) do h
fh = flatmap(h) do x fmg(x) end
flatmap(fh) do x fmf(x) end
gh = flatmap(h) do x fmg(x) end
flatmap(gh) do x fmf(x) end
end
@test all(fmv1 .== fmv2)

Expand Down