-
-
Save xitology/347e01ed012e9e2a5530c719b7163500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct It{F} | |
f::F | |
end | |
const it = It(identity) | |
(i::It)(x) = getfield(i, :f)(x) | |
(i::It)(xs...) = getfield(i, :f)(xs) | |
Base.getproperty(i::It, name::Symbol) = getproperty.(i, name) | |
Base.getindex(i::It, key) = getindex.(i, key) | |
struct ItStyle <: Base.BroadcastStyle | |
end | |
Base.BroadcastStyle(::Type{<:It}) = ItStyle() | |
Base.BroadcastStyle(::ItStyle, ::Base.Broadcast.DefaultArrayStyle{0}) = ItStyle() | |
Base.broadcastable(i::It) = i | |
Base.Broadcast.instantiate(bc::Base.Broadcast.Broadcasted{ItStyle}) = bc | |
Base.copy(bc::Base.Broadcast.Broadcasted{ItStyle}) = | |
It(x -> bc.f(((arg isa It) ? arg(x) : arg[] for arg in bc.args)...)) | |
Base.filter(f) = xs -> filter(f, xs) | |
Base.map(f) = xs -> map(f, xs) | |
Base.foldl(f) = xs -> foldl(f, xs) | |
@show 1:10 |> filter(isodd.(it)) |> map(it .* 2) |> foldl(it[1] .+ it[2]) | |
#-> 50 | |
@show [(x = 10, y = 20), (x = 30, y = 20)] |> filter(it.x .> it.y) |> map(it.x .+ it.y) |> sum | |
#-> 50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment