Giter Site home page Giter Site logo

Comments (9)

mcabbott avatar mcabbott commented on August 25, 2024

I think being explicit is a virtue of this package. Automatically inserting __ as the first (or should it be last?) argument would also make it impossible to chain functions which do already return a closure:

g(x) = begin y=exp(x); z -> z .* y end
@_ data |> filter(abs(_)>1, __) |> g(x) |> scatter

(Note that @_ a |> f(_, b) means a |> f(identity, b), but I assume you meant @_ a |> f(__, b) with two underscores.)

from underscores.jl.

xiaodaigh avatar xiaodaigh commented on August 25, 2024

Interesting argument. But g(x)() would work. And also it's more often the case that we need _ as first argument then it is to get a closure as return from a function.

from underscores.jl.

mcabbott avatar mcabbott commented on August 25, 2024

Ah right, there's a way... and I guess |> g(x)(__) |> scatter would likewise be the most explicit way to write my example today.

Do you have examples of where you'd want first rather than last-argument __? The tests have many map / sum / filter (last) and one sort (only).

from underscores.jl.

xiaodaigh avatar xiaodaigh commented on August 25, 2024

The way that Lazy.jl deals with it is that

@> and @>> are for first and last. Perhaps @_ will optionally allow _ anywhere and also have a @_last macro?

Also

@_ obj begin
  function1
  function2
  function3
end

without needing |> would be nice.

from underscores.jl.

c42f avatar c42f commented on August 25, 2024

Overall I think this is an interesting idea, and might resolve some of the uglyness of __. I think the rule might be:

  • For Expr(:call) on the right hand side of an |> expression, if __ doesn't appear in the call, insert it as the last argument.

Thus, we'd have the following work:

@_ data |> filter(abs(_)>1) |> map(_+1) |> scatter

If there's no piping present, no __ would be inserted.

The need to explicitly use __ for use with functions-which-return-functions is unfortunate, however, and would make Underscores.jl unattractive for use with libraries which are designed with currying in mind. @tkf — that's probably your case case?

Another option could be to... just add more underscores, and have a macro @__ which does this transformation, leaving @_ as it is?


It's worth keeping in mind that this package is not just about pipelines, though that's one of the nice use cases for it (I think where you mention _ above, you really mean __.)

without needing |> would be nice.

Personally I don't like the implicit chaining syntax in Lazy.jl at all so I'd rather not emulate it!

from underscores.jl.

tkf avatar tkf commented on August 25, 2024

Yeah, transducers are always "curried". So, we'd need extra () like @_ collection |> Filter(abs(_) > 1)() |> sum if __ is automatically inserted.

from underscores.jl.

c42f avatar c42f commented on August 25, 2024

@xiaodaigh I still think this is a good idea; we'd just need to modify it a little.

What if we implemented a @__ macro which followed the rule I laid out in #17 (comment) ? Would that serve your use case?

from underscores.jl.

tkf avatar tkf commented on August 25, 2024

A bit crazy idea is to use some infix combinator like

julia> f $ args::Tuple = x -> f(args..., x)
       f $ a = x -> f(a, x)
$ (generic function with 2 methods)

julia> @_ -3:3 |> filter$(abs(_)>1) |> map$(_+1) |> collect
4-element Array{Int64,1}:
 -2
 -1
  3
  4

Though it's not ideal that f$(x) may or may not splat x when its type is unknown. You'd have to write f$(x,) or f$(x...,) to be very specific.

from underscores.jl.

c42f avatar c42f commented on August 25, 2024

I've found a couple of uses of @_ in the last few weeks while where having __ as the last parameter wouldn't work. For example

@_ read(`git status --porcelain`) |> String |> split(__,'\n')

from underscores.jl.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.