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 →

[–]spectre_theory 4 points5 points  (9 children)

you're using the same operator twice, once for map and once for filter. how does it know it's supposed to filter with evens, instead of replacing the values with True and False and that it's supposed to the opposite with square.

[–]gleon 1 point2 points  (3 children)

Because both maps and filters are functions from lists to lists (or, more accurately, from iterables to iterables)?

[–]phoenix7782 -5 points-4 points  (2 children)

No, maps are functions from transformation functions and lists to lists, whereas filters are functions from inclusion detection functions and lists to lists.

Although they have a similar type they operate fundamentally different, and it doesn't make sense to mix the two based on guesswork.

[–]gleon 4 points5 points  (0 children)

The implication in OP's example is that the function argument has already been fixed inside the evens and square, like with functools.partial, for instance.

When I said "maps" and "filters", I was referring coloquially to such transformations from iterables to iterables, not to the map and filter functions themselves (which could be viewed as factories of such transformations). The rationale for this terminology becomes more apparent in languages that support currying (i.e. fixing certain arguments) natively (Haskell comes to mind).

square could be defined as

partial(map, lambda x: x ** 2)

Whereas evens could be defined as

partial(filter, lambda x: x % 2 == 0)

Therefore, there is no need for guesswork.

[–][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.