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

Allow __ to stand for the argument which accepts a do block #4

Open
wants to merge 2 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
15 changes: 15 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ julia> @_ map("X $_2 $(repeat(_1,_2))", ["a","b","c"], [1,2,3])
"X 3 ccc"
```

### do blocks and double underscores

The double underscore placeholder `__` can be used to pass the closure created
by a `do` block to some function argument other than the first. Say we had a
complex binary operator in a map-reduction. With a do block this could look
like:

```jldoctest
@_ mapreduce(extrema, __, [[1,2],[3,4]]) do (min1,max1), (min2,max2)

end
```

"escapes an extra level", so `@_ f(__)` means

### Tabular data

`@_` is handy for manipulating tabular data. Let's filter a list of named
Expand Down
28 changes: 26 additions & 2 deletions src/Underscores.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function add_closures(ex, prefix, pattern)
end
end
if plain_nargs && numbered_nargs > 0
throw(ArgumentError("Cannot mix plain and numbered `$prefix` placeholders in `$ex`"))
throw(ArgumentError("`@_` expansion: Cannot mix plain and numbered `$prefix` placeholders in `$ex`"))
end
nargs = max(plain_nargs, numbered_nargs)
if nargs == 0
Expand All @@ -74,9 +74,33 @@ function lower_underscores(ex)
return ex
elseif ex.head == :call && length(ex.args) > 1 &&
ex.args[1] in _pipeline_ops
# Special case for pipelining and composition operators
# Pipelining and composition delimits "outer" expression
return Expr(ex.head, ex.args[1],
map(lower_underscores, ex.args[2:end])...)
elseif ex.head == :do
# don't expand the RHS - it's already a closure.
rhs = ex.args[2]
# `do` syntax delimits an "outer" expression; expand inside the
# left hand side.
lhs = lower_underscores(ex.args[1])
if lhs isa Expr && lhs.head == :call
# lhs had no __ placeholders - lower as a normal do block.
Expr(ex.head, lhs, rhs)
else
# lhs has a placeholder and it's now a closure. We could use it
# directly with a do block, but lets avoid one extra closure
# here by lowering to a let block instead.
@assert lhs isa Expr && lhs.head == :->
lhsargs = lhs.args[1].args
if length(lhsargs) != 1
throw(ArgumentError("`@_` expansion: multiple `__` placeholders `$((lhsargs...,))` make no sense in `do` block form"))
end
quote
let $(lhsargs[1]) = $rhs
$(lhs.args[2])
end
end
end
elseif ex.head == :. && length(ex.args) == 2 &&
ex.args[2] isa Expr && ex.args[2].head == :tuple
# Broadcast calls treated as normal calls for underscore lowering
Expand Down