evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.09k stars 125 forks source link

multiline pipe #722

Closed setop closed 1 year ago

setop commented 1 year ago

Instead of writing

[1,2,3] |> filter$(x -> (x & 1) == 1) |> list |> len |> print

I'd like to be able to write

[1,2,3] 
|> filter$(x -> (x & 1) == 1)  # filter odd 
|> list 
|> len 
|> print

As seen in Elixir

Currently if I try this, I get syntax error.

CoconutParseError: parsing failed (line 4)
  |> filter$(x -> (x & 1) == 1)
  ^
Coconut exiting with error: CoconutParseError
evhub commented 1 year ago

Just use Python's implicit line continuation inside parentheses, the same way you would for any other long sequence of Python operators:

(
    [1,2,3] 
    |> filter$(x -> (x & 1) == 1)  # filter odd 
    |> list 
    |> len 
    |> print
)
setop commented 1 year ago

That's awsome !

the same way you would for any other long sequence of Python operators

Honestly, I don't do that. I store intermediate results in local variables. Maybe not a best practice as it require to be pretty creative about naming... Hence my appeal for multi-line pipeline.

Maybe this notation could be explicitly stated in coconut examples, that's pretty elegant (to me at least :smile: ).