c42f/Underscores.jl

Doesn't with ternary operators

Closed this issue · 3 comments

The below doesn't work

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

but this works

x-> x == "X" ? 0 : 1

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

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.

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.