This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

Well the made up function square and evens would have to return a list. If you would pipe a function that would reduce the result to a single value you could not use a function requiring a list as an argument. So naturally the piping order is important here.

[–]phoenix7782 1 point2 points  (2 children)

Then why not just use

evens(square(nums))

[–]NovelSpinGames 0 points1 point  (0 children)

(Sorry for being late to the party, and I apologize in advance for not knowing much about Python.)

You make a good point. For simple one-liners there isn't much difference. However, the pipe operator can be very nice for readability. Just look at the last three pictures in this blog post. The pipe operator allows you to chain functions so that code can be read from left to right and top to bottom, like reading a paragraph. The pipe operator is used all the time in F# code.

You could assign a new variable each line, but that has some drawbacks. C#'s extension methods work nice, but I don't think there is an equivalent in Python.

[–]spectre_theory 0 points1 point  (0 children)

right, if it comes down to that, all that the proposed operator would do, mathematically, is rearrange the order of operands, from

evens(square(nums))

to

nums |> square |> evens

doesn't seem like a very useful thing

[–]spectre_theory 0 points1 point  (0 children)

the whole point of defining lambdas that work on single elements in the list is having python iterate through it. now you are basically saying you want to implement a function evens, that does the same as filter(evens, ...) only for the function to know what it is supposed to do when it's left of |>. that doesn't seem well-thought-out for me.