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

Doesn't with ternary operators #18

Closed
xiaodaigh opened this issue Aug 31, 2020 · 3 comments · Fixed by #19
Closed

Doesn't with ternary operators #18

xiaodaigh opened this issue Aug 31, 2020 · 3 comments · Fixed by #19

Comments

@xiaodaigh
Copy link

The below doesn't work

@_ _ == "X" ? 0 : 1 #doesn't work

but this works

x-> x == "X" ? 0 : 1
@mcabbott
Copy link
Collaborator

The macro needs to be outside the function to which you pass the anonymous function, as in @_ map(_+1, xs). This is the usage that works as advertised:

julia> @_ map(_ == 'X' ? 0 : 1, collect("XYZ"))
3-element Array{Int64,1}:
 0
 1
 1

These aren't meant to work:

julia> @_ (_+1)
ERROR: syntax: all-underscore identifier used as rvalue

julia> @_ _ == "X" ? 0 : 1 #doesn't work
ERROR: TypeError: non-boolean (var"#7#8") used in boolean context

although it seems like the second one does trigger some transformation. It's possible that since the ternary operator doesn't "look like a function call" #12, , the macro ought to recurse inside, to make a function inside any(_>3, 1:4) here:

julia> @macroexpand @_ (_+1)
:(_ + 1)

julia> @macroexpand @_ _ == "X" ? 0 : 1
:(if ((_1,)->begin
              _1 == "X"
          end)
      0
  else
      1
  end)

julia> any(>(3), 1:4) ? "yes" : "no"
"yes"

julia> @_ any(_>3, 1:4) ? "yes" : "no"
ERROR: TypeError: non-boolean (var"#13#14") used in boolean context

@xiaodaigh
Copy link
Author

This doesn't work either

 @_ bureau_bal |>
        transform(__, :MONTHS_BALANCE => (_))

Basically, I am gonna give u on Underscores.jl it's not intuitive at all how these rules work.

@mcabbott
Copy link
Collaborator

mcabbott commented Sep 3, 2020

This one expands to

bureau_bal |>
        b -> transform(b, c -> :MONTHS_BALANCE => (c))

where the _ rule is that since @_ is outside the function transform, that gets passed a function.

However I guess you wanted something else, for this DataFrames syntax? xref #16 for discussion of that.

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 a pull request may close this issue.

2 participants