Only when the transformed argument is the first argument of every method in your call notation/the last argument of every method in parenthesized Polish notation. That’s often not the case, hence the need for Pipe.jl, Chain.jl, etc. where you can specify the location of the argument in each transformation call.
Associating to the right its actually the last argument, i.e., it works nicely with map and filter, but no longer with the intended use – at least originally – with the object being the first argument. Too bad, guess we have to live with @chain and friends for now .
We certainly have been carried away a bit from the original idea, but it was fun to think it through, i.e., as an alternative syntax for partial function application in chaining pipelines. Guess for most use cases, @chain and friends are quite nice already.
No, it would not work if -- is defined as a function:
julia> minusminus(obj, meth) = (args...; kwargs...) -> meth(obj, args...; kwargs...) # --(obj, meth)
minusminus (generic function with 1 method)
julia> meth1(obj, arg1, arg2) = (obj, arg1, arg2) # just an example method
meth1 (generic function with 1 method)
julia> minusminus(:my_object, meth1(:arg1, :arg2)) # :my_object -- meth1(:arg1, :arg2)
ERROR: MethodError: no method matching meth1(::Symbol, ::Symbol)
Closest candidates are:
meth1(::Any, ::Any, ::Any) at REPL[13]:1
The problem is that meth1(:arg1, :arg2) needs to be interpreted as partial application here, i.e., the former would work if you define a suitable partial1 application and then apply the resulting function:
Remember that we have decided -- should bind more tightly than the function call, so that my_object--meth1(arg1, arg2) will evaluate as (my_object--meth1)(arg1, arg2)
Cool, would be a very nice infix operator indeed. Unfortunately, right associativity and precedence requires a (breaking ?) change to the parser. Would try it out in Haskell, if I knew how to type it
Insisting we all use macros and non-standardizable syntax which can never be part of the core language, just to pipe into arbitrary argument positions, seems sadomasochistic. For what? So that we can pretend functional multidispatch languages somehow magically make one argument position not matter more than any other? when the do statement already shows we don’t actually believe that, as do Clojure’s threading macros?
The ability to insert into arbitrary positions is usually more hassle than not, requiring more characters and introducing more mental load, because for functions that have been defined well the object to be chained is usually the first or second argument anyway. Which the proposed operator-- handles nicely:
Yeah you probably won’t actually want to pull all the arguments out of the parentheses in reverse order, but as we’ve seen usually all you care for is the first one or two.
Nice example of how an argument can be inserted at any position. Just to try out the new syntax, I have hacked together a small macro which rewrites ++ (a valid and as of now unused operator) into right-associative calls of minusminus (our higher-order function corresponding to the invalid operator --):
using MacroTools: postwalk, @capture
minusminus(obj, meth) = (args...; kwargs...) -> meth(obj, args...; kwargs...)
function unchain(terms::AbstractVector, stack::AbstractVector)
if isempty(terms)
:(foldr(minusminus, [$(stack...)]))
elseif @capture(terms[1], f_(args__)) && f != :foldr # don't touch nested transforms again
arg = :(foldr(minusminus, [$(stack...), $f])($(args...)))
unchain(terms[2:end], push!([], arg))
else
unchain(terms[2:end], push!(stack, terms[1]))
end
end
macro calumet(expr)
postwalk(expr) do ex
if @capture(ex, ++(args__))
unchain(args, [])
else
ex
end
end
end
Both of your examples now work as follows:
julia> @calumet "Hello, world!"++split(",")++uppercase++map()++join(":")
"HELLO: WORLD!"
julia> @calumet let x = [1,2,missing,4,5,missing]; x++skipmissing()++isodd++filter() end
2-element Vector{Int64}:
1
5
Also nested transformations should work, but I have not tested extensively:
Are /> and \> available where one could curry into the first argument and the other the last, otherwise using the same semantics as -- ? or would this be too confusing
Holy wow that was fast! You’re a wizard! Admittedly I began looking into writing a macro to do this, but you beat me to the punch by a wide country mile
This is really cool! Although, two complaints:
I’ve begun to use the ++ operator for the parallel sum, as seen here.
This macro doesn’t yet work for broadcasting a function across a collection, e.g. [1,2,3]++sqrt.().
I know I know, eventually we’d settle on dash--, I’m pulling your leg.
This is really great. I like the “peace pipe” reference too.
Masterful. I’m going to play with this for a while, and maybe some other folks can play with it too and see what else it’s good for (and what it’s not).
Similar to dash --, /> and \> are not valid operators, which means we would need them to be added into the language first; it also means we could claim them without breaking backwards compatibility in any packages or programs, if we can convince the Julia devs to incorporate them.
There was a brief moment when I was considering emdash --- for piping the object into the last argument position, such as arr---map(sqrt). If it has otherwise the same properties as we have chosen for dash -- (higher precedence than function call, and right-associativity), then the following is true:
f(a, b, c, d) ==
d---f(a, b, c) ==
c---d---f(a, b) ==
b---c---d---f(a) ==
a---b---c---d---f()
This operator would be nice for map, reduce, filter, etc., and especially well-suited for mapreduce, e.g.:
[1, 2, 3]---filter(iseven)---map(sqrt) == # using emdash
[1, 2, 3]--iseven--filter()--sqrt--map() # using dash
[1, 2, 3]---mapreduce(x->x^2, +) == # using emdash
[1, 2, 3]--(+)--(x->x^2)--mapreduce() # using dash
And if dash -- and emdash --- have the same precedence, then they should chain as expected, e.g.:
However, I’ve begun thinking that dash -- is sufficiently expressive to cover the vast majority of use cases, that the ease of typing it beats the other options we’ve considered (except perhaps ..), and that adding more and other operators to serve these purposes would bring more confusion and mental loading than it’s worth.
But I’m open to the idea that I’m wrong. Maybe you can convince me?
meaning that -- partials an object into the first argument position and --- partials an object into the last argument position. Make them bind more tightly than function call, be right-associative, and have equal precedence such that:
And although they are numerous, they are not arbitrary; arguments can only be picked off the argument list from the left end or the right end, one at a time. (Note: for a single-argument function, arg--f() and arg---f() are equivalent.)
This seems so insidious that I’m leaning toward disallowing it, and banishing the emdash --- operator altogether in favor of having only the dash -- operator. As mentioned previously, most well-designed functions have whatever object you might wish to chain in the first or second argument position anyway; even if it might be somewhat uglier to use -- for calling map or filter, the Pandora’s box that --- opens up isn’t worth the convenience imo.
Heck, I’d rather use the half dozen piping packages than live in a world where -- and --- coexist.
I’m coming around to the idea of having both operators, and I’m starting to like @adienes’ idea to use /> and \>. Apologies for changing my mind so quickly.
which bind more tightly than function calls, have right-associativity, and have equal precedence to each other.
These operators generate partially applied functions, calling the method to the right on the object to the left—frontfill /> filling the object as a first argument and backfill \> filling the object as a last (non-keyword) argument—operating as follows:
Why am I changing my mind? Because as coders we already have plenty of ways to make code illegible, yet we don’t because it doesn’t help us. We might as well offer both tools—frontfill and backfill—and trust that we won’t abuse them.
Although /> and \> are slightly more awkward to type than --, it’s not too bad. Especially if you type them a lot because it’s actually a useful feature, you get good at it (also anybody who’s typed a lot of XML should already be good at typing />). In addition, they also exhibit stylistic cohesion with the existing pipe operator |>. (may or may not be a good thing.)
I’m really excited to watch autocomplete work with this and pop up dialogs with matching method signatures.
I confess to having been somewhat exasperated by this thread. First it was apparent shoehorning of unmotivated OOP syntax into Julia, then it seemed as if Julia-is-not-at-that-stage was being willfully ignored, and finally the -- and --- operators were proposed. Maybe it’s just me, but I find those operators almost comically terrible, as if developers of the Brainfuck language had snuck into our forum and were drunk-posting just to troll us. And at first I felt the same way about /> and \>.
But on further reflection they’re actually starting to look pretty interesting and useful. As you say, /> and \> work together with |> as an extended family of piping operators. Yes, they can be abused to make illegible code but so can everything. And I don’t think newbies will find them more confusing than existing argument transformation syntax like f() do … end blocks.
So I’m tentatively going to click like on your proposal because I hope people like me who’ve been averse to this thread will notice and comment on it - especially those with insight into Julia’s parser who can clarify whether this is at all feasible. And kudos to everyone involved for persisting!
(But consider editing your post again to add spaces around the pipes. Legibility is everything, and you need this to be attractive to sell it.)