Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm Syntax, Pipe forward a match case without using temp variable

Tags:

elm

F#, Pipe forward a match case without using temp variable

Similar to the question above, is there a way to pipe-forward a variable to a match case without using a temp variable or a lambda?

The idea:

let temp =
    x 
    |> Function1
    |> Function2
    // ........ Many functions later.
    |> FunctionN
in

result =
    case temp of
        Case1 -> "Output 1"
        Case2 -> "Output 2"
        _ -> "Other Output"

I hope to achieve the following:

result =
    x 
    |> Function1
    |> Function2
    // ........ Many functions later.
    |> FunctionN
    |> case of     // Syntax Error! Should use "case temp of" 
        Case1 -> "Output 1"
        Case2 -> "Output 2"
        _ -> "Other Output"

I can use a lambda function, but I would still be "naming" the temp variable.

result =
    x 
    |> Function1
    |> Function2
    // ........ Many functions later.
    |> FunctionN
    |> \temp -> case temp of      
        Case1 -> "Output 1"
        Case2 -> "Output 2"
        _ -> "Other Output"

Is there a way in the Elm syntax to "get rid" of the temp variable? Thanks.

like image 974
Hai Bin Chang Avatar asked Mar 22 '18 08:03

Hai Bin Chang


1 Answers

No, Elm does not have that ability.

Other languages like Haskell allow something similar via the LambdaCase extension, but Elm tends to avoid having too many ways to say the same thing, erring on the side of keeping syntax simple.

The issue has been raised before, and Elm's author rejected the proposal with the following comment:

More generally though, the focus right now is not on growing the syntax of Elm. (We're actually dropping stuff more often.) If something can be expressed in Elm already, I'm not hugely interested in providing alternate ways to express it. In this case, I think we would be adding syntax to make things less regular and harder to read.

like image 86
Chad Gilbert Avatar answered Sep 26 '22 14:09

Chad Gilbert