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.
New Proposal (Compare with old proposal)
Two infix operators, />
(“fill” or “frontfill”) and \>
(“backfill”), defined as
/>(obj, meth) = (args...; kwargs...) -> meth(obj, args...; kwargs...)
\>(obj, meth) = (args...; kwargs...) -> meth(args..., obj; kwargs...)
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:
my_obj/>meth1(args1...)/>meth2(args2...)/>meth3(args3...) ==
meth3(meth2(meth1(my_obj, args1...), args2...), args3...)
my_obj\>meth1(args1...)\>meth2(args2...)\>meth3(args3...) ==
meth3(args3..., meth2(args2..., meth1(args1..., my_obj)))
and can be used in conjunction with each other for chaining operations:
"Hello, world!"/>replace("o"=>"e")/>split(",")/>uppercase.()/>join(":") ==
join(uppercase.(split(replace("Hello, world!", "o"=>"e"), ",")), ":") ==
"HELLE: WERLD!"
"1, 2, 3, 4"\>eachmatch(r"(\d+)")\>map(x->x[1]\>parse(Int))\>filter(iseven)/>sum() ==
sum(filter(iseven, map(x->parse(Int, x[1]), eachmatch(r"(\d+)", "1, 2, 3, 4")))) ==
10
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.