Skip to content
Open
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
45 changes: 43 additions & 2 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using MacroTools

import Base: replace

export @>, @>>, @as, @_, @switch, @or, @dotimes, @oncethen, @defonce, @cond, @with, @errs,
export @>, @.>, @>>, @.>>, @as, @_, @switch, @or, @dotimes, @oncethen, @defonce, @cond, @with, @errs,
@forward, @iter

"""
Expand Down Expand Up @@ -98,6 +98,26 @@ macro >(exs...)
esc(thread(exs...))
end

"""
Same as `@>`, but broadcasts.

@.> x g(y, z) f == f.(g.(x, y, z))

@.> x g f(y, z) == f.(g.(x), y, z)
"""
macro .>(exs...)
thread(x) = isexpr(x, :block) ? thread(rmlines(x).args...) : x

thread(x, ex) =
isexpr(ex, :call, :macrocall) ? Expr(Symbol("."), ex.args[1], Expr(:tuple, x, ex.args[2:end]...)) :
isexpr(ex, :block) ? thread(x, rmlines(ex).args...) :
Expr(:call, ex, x)

thread(x, exs...) = reduce(thread, x, exs)

esc(thread(exs...))
end

"""
Same as `@>`, but threads the last argument.

Expand All @@ -120,6 +140,27 @@ macro >>(exs...)
esc(thread(exs...))
end

"""
Same as `@>>`, but broadcasts.

@.>> x g(y, z) f == f.(g.(y, z, x))

@.>> x g f(y, z) == f.(y, z, g.(x))
"""
macro .>>(exs...)
thread(x) = isexpr(x, :block) ? thread(rmlines(x).args...) : x

thread(x, ex) =
isexpr(ex, Symbol, :->) ? Expr(Symbol("."), ex, Expr(:tuple, x)) :
isexpr(ex, :call, :macrocall) ? Expr(Symbol("."), ex.args[1], Expr(:tuple, ex.args[2:end]..., x)) :
isexpr(ex, :block) ? thread(x, rmlines(ex).args...) :
error("Unsupported expression $ex in @>>")

thread(x, exs...) = reduce(thread, x, exs)

esc(thread(exs...))
end

"""
# @as lets you name the threaded argmument
@as _ x f(_, y) g(z, _) == g(z, f(x, y))
Expand Down Expand Up @@ -322,7 +363,7 @@ export @init

function initm(ex)
quote

if !isdefined(@compat(@__MODULE__), :__inits__)
const $(esc(:__inits__)) = Function[]
end
Expand Down