mdgriffith/elm-codegen/2.1.0

NOTE: the latest version is 6.0.3

Elm.Let

This module is for building let expressions.

letIn : a -> Let a
value : String -> Expression -> Let (Expression -> a) -> Let a
type Let a

Here's a brief example to get you started

Elm.Let.letIn
    (\one two ->
        Elm.Op.append one two
    )
    |> Elm.Let.value "one" (Elm.string "Hello")
    |> Elm.Let.value "two" (Elm.string "World!")
    |> Elm.Let.toExpression

Will translate into

let
    one =
        "Hello!"


    two =
        "World"
in
one ++ two

Destructing values

String
-> String
-> Expression
-> Let (( Expression, Expression ) -> a)
-> Let a

Here's an example destructing a tuple. This code

Elm.Let.letIn
    (\( first, second ) ->
        Elm.Op.append first second
    )
    |> Elm.Let.tuple "first" "second" (Elm.tuple (Elm.string "Hello") (Elm.string "World!"))
    |> Elm.Let.toExpression

Will generate

let
    ( first, second ) =
        ( "Hello", "World!" )
in
first ++ second
record : List String -> Expression -> Let (List Expression -> a) -> Let a

And extracting fields from a record.

Elm.Let.letIn
    (\fields ->
        case fields of
            [ first, second ] ->
                Elm.Op.append first second


            _ ->
                Elm.unit
    )
    |> Elm.Let.record [ "first", "second" ]
        (Elm.record
            [ ( "first", Elm.string "Hello" )
            , ( "second", Elm.string "world!" )
            ]
        )
    |> Elm.Let.toExpression

Will generate:

let
    { first, second } =
        { first = "Hello", second = "world!" }
in
first ++ second

Functions

Here's an example of declaring functions in a let expression:

Elm.Let.letIn
    (\myFn ->
        myFn (Elm.bool True)
    )
    |> Elm.Let.fn "myFn"
        ( "arg", Just Type.bool )
        (\arg ->
            Elm.ifThen arg
                (Elm.string "True")
                (Elm.string "False")
        )
    |> Elm.Let.toExpression

will generate

let
    myFn arg =
        if arg then
            "True"


        else
            "False"
in
myFn True
fn :
String
-> ( String, Maybe Annotation )
-> (Expression -> Expression)
-> Let ((Expression -> Expression) -> a)
-> Let a
fn2 :
String
-> ( String, Maybe Annotation )
-> ( String, Maybe Annotation )
-> (Expression -> Expression -> Expression)
-> Let ((Expression -> Expression -> Expression) -> a)
-> Let a
fn3 :
String
-> ( String, Maybe Annotation )
-> ( String, Maybe Annotation )
-> ( String, Maybe Annotation )
-> (Expression -> Expression -> Expression -> Expression)
-> Let ((Expression -> Expression -> Expression -> Expression) -> a)
-> Let a

Converting to an Expression

toExpression : Let Expression -> Expression