Infix operators sectioning

Is there a chance that ELM will support sectioning for the built-in infix operators?

If I’m not wrong, there is no way to do something like:

List.map (^2) [1,2,3]

or

List.map (2^) [1,2,3]

if not using a lambda:

List.map (\v -> v^2) [1,2,3]

or

List.map (\v -> 2^v) [1,2,3]

is this a feature that make sense in Elm?

You can raise a number to a set of powers by using the function format of the infix operator:

List.map ((^) 2) [1,2,3] (this would be the equivalent to what you are trying to do with List.map (2^) [1,2,3]

For the first version you need a flipped version of that function. The core used to have a function called flip

{-| Flip the order of the first two arguments to a function. -}
flip : (a -> b -> c) -> (b -> a -> c)
flip f b a =
  f a b

With this function you can write List.map (flip (^) 2) [1,2,3] as an equivalent to what you tried to do with List.map (^2) [1,2,3]. However, just because you can, it doesn’t mean that you should. I don’t think it looks good and if you do this enough, you will end up with very ugly code. flip was not removed without reason.

I would go for a lambda even for the first option.

I don’t think it would encourage clear, easy to read code.

I’ve used infix operators as functions sometime in my code. It feels great in the moment, like a prize for solving a puzzle BUT, it does not make for easy to read code. If the goal is to feel good about myself while writing code… then stuff like this might help me reach my goal. If the goal is to feel good about myself 6 months from now while reading the code… at least in my case… this stuff would not help me reach my goal.

6 Likes

You are correct, Elm does not support sectioning and likely never will. Elm only offers limited support for infix operators (on purpose) and in general tries to push you towards using a lambda in situations like the one you’re describing.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.