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

Add ability to work without begin. #23

Closed
wants to merge 5 commits into from

Conversation

pdeffebach
Copy link
Contributor

Fixes #17. But I would understand if you choose not to merge it. Because we don't get a new expression on every line, we rely on Julia's parsing to make expressions, meaning we can't use binary or ternary operators, or anonymous functions.

Nonetheless, I think it might be nice for a series of transformations on the same line.

See the docstring I wrote for it


@chain(initial_value, args...)

Rewrites a series of argments, either expressions or symbols, to feed the result
of each line into the next one. The initial value is given by the first argument.

In all arguments, underscores are replaced by the argument's result.
If there are no underscores and the argument is a symbol, the symbol is rewritten
to a function call with the previous result as the only argument.
If there are no underscores and the argument is a function call or a macrocall,
the call has the previous result prepended as the first argument.

When using this form of @chain, without a begin block, you cannot use infix
operators, binary operators, or anonymous functions. For example, @chain 1 (_ + 2)
will fail.

Example:

x = @chain [1, 2, 3] filter(!=(2), _) sqrt.(_) sum

x == sum(sqrt.(filter(!=(2), [1, 2, 3])))

@jkrumbiegel
Copy link
Owner

When using this form of @chain, without a begin block, you cannot use infix
operators, binary operators, or anonymous functions. For example, @chain 1 (_ + 2)
will fail.

Why is that?

julia> dump(:(@chain 1 (_ + 2)))
Expr
  head: Symbol macrocall
  args: Array{Any}((4,))
    1: Symbol @chain
    2: LineNumberNode
      line: Int64 1
      file: Symbol REPL[48]
    3: Int64 1
    4: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol +
        2: Symbol _
        3: Int64 2

Looks fine to me, arg 4 is an expression just like you would have in the block form

@pdeffebach
Copy link
Contributor Author

It has to do with block. quote returns expressions starting with block while :(t + _) does not.

julia> @chain 1 begin 
       (_ + 2)
       end
block = quote
    #= REPL[4]:2 =#
    _ + 2
end
3

julia> @chain 1 (_ + 2)
block = :(_ + 2)

I have fixed all those issues with a small fix just now.

@pdeffebach
Copy link
Contributor Author

Okay this is ready for a serious review!

@jkrumbiegel
Copy link
Owner

Thanks! I've just added a small additional test and some docs locally, then merged it here because it was quicker to do so #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do we really need begin and end in the expression?
2 participants