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

Introduce better piping syntax #38090

Closed
sa- opened this issue Oct 18, 2020 · 5 comments
Closed

Introduce better piping syntax #38090

sa- opened this issue Oct 18, 2020 · 5 comments

Comments

@sa-
Copy link

sa- commented Oct 18, 2020

Hello,

This is inspired by R's dplyr lib (and sort of by Pipe.jl)

Proposal TLDR

Introduce .. as an operator such that
x..f(y) is equivalent to f(x,y)
in order to improve readability, and writing productivity.

Problem

Consider the scenario where you would like to perform a 3 operations on a dataframe: dropmissing, innerjoin, and then select. The current pipe operator is hard to type on a German keyboard which makes it prohibitive to use.

In scala we would get code that is very succinct and readable like this

val result = df
  .dropmissing
  .innerjoin(df2, colA)
  .select(colA, colB)

In Julia we would we have a few options, neither of which are particularly promising

Option 1

result = select(innerjoin(dropmissing(df), df2, colA), colA, colB)

This is not ideal because the order or execution does not match the order of code-reading

Option 2

result = dropmissing(df)
result = innerjoin(result, df2, colA),
result = select(result, colA, colB)

This is not succinct, and the redundant code increases time-to-read/time-to-understand. One also sacrifices immutability here. One could come up with new variable names for each assignment to maintain immutability but that doesn't feel like the best idea either.

Option 3

Use @pipe from Pipe.jl, but it is very annoying to write |> on a German keyboard, and the need to add @pipe everywhere destroys readability and succinctness (at no fault of the author to Pipe.jl, I appreciate that package very much)

Solution with proposed syntax

result = df..dropmissing..innerjoin(df2, colA)..select(colA, colB)

This is succinct, backwards compatible, way more readable (faster time-to-understand), and much easier to code on a German (or any other) keyboard.

Reasons why ..

  • Easier to type on a German keyboard than |>
  • Backwards compatible, will not break previous code that has fields defined with the same name as functions
  • Won't give people the idea that functions belong to struct with the familiar struct.function() syntax
  • special syntax for piping to functions can make parenths () redundant for functions with just one argument. E.g. df..dropmissing can be equivalent to df..dropmissing()
@JeffBezanson
Copy link
Member

See the end of #5571 (in case you haven't already) for some discussion of this. I think this specific proposal is unworkable since .. is already an operator used for other things. (It also visually does not make sense to me for this.)

@sa-
Copy link
Author

sa- commented Oct 20, 2020

Thanks for taking a look either way!

@sa- sa- closed this as completed Oct 20, 2020
@igotfr
Copy link
Contributor

igotfr commented Nov 2, 2020

See the end of #5571 (in case you haven't already) for some discussion of this. I think this specific proposal is unworkable since .. is already an operator used for other things. (It also visually does not make sense to me for this.)

.. would be interesting as another operator like .. from Dart, I suggest

mutable struct S
  number::Int
end

S §(
  number = 7;
  print(_ <: Number);
)

@jkrumbiegel
Copy link
Contributor

jkrumbiegel commented Nov 26, 2020

Use @pipe from Pipe.jl, but it is very annoying to write |> on a German keyboard

In case you haven't seen it yet, I wrote https://github.com/jkrumbiegel/Chain.jl for that reason (I know the struggles of German keyboards..)

@sa-
Copy link
Author

sa- commented Nov 26, 2020

That looks very nice

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

No branches or pull requests

4 participants