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
Added a second test for flatmap
  • Loading branch information
nlw0 committed Apr 5, 2022
commit ddda481f48046d155eb30181a7de417abf3dfdca
17 changes: 17 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,23 @@ end
@test flatmap(1:3) do j flatmap(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
fx = fmg(x)
flatmap(fx) 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
end
@test all(fmv1 .== fmv2)

# partition(c, n)
let v = collect(partition([1,2,3,4,5], 1))
Expand Down