mdgriffith/elm-codegen/5.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
unpack : Arg arg -> Expression -> Let (arg -> b) -> Let b
type Let a

Here's a brief example to get you started

import Elm
import Elm.Let as Let


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

Will translate into

let
    one =
        "Hello!"


    two =
        "World"
in
one ++ two

Destructing values

Here's an example destructing a tuple. This code

import Elm
import Elm.Let as Let
import Elm.Arg as Arg




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

Will generate

let
    ( first, second ) =
        ( "Hello", "World!" )
in
first ++ second

And extracting fields from a record.

import Elm
import Elm.Let as Let
import Elm.Arg as Arg


Let.letIn
    (\{first, second } ->
        Elm.Op.append first second
    )
    |> Let.unpack
        (Arg.record (\first second -> {first, second})
        |> Arg.field "first"
        |> Arg.field "second"


        )
        (Elm.record
            [ ( "first", Elm.string "Hello" )
            , ( "second", Elm.string "world!" )
            ]
        )
    |> 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:

import Elm
import Elm.Let as Let


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

will generate

let
    myFn arg =
        if arg then
            "True"


        else
            "False"
in
myFn True
fn :
String
-> Arg arg
-> (arg -> Expression)
-> Let ((Expression -> Expression) -> a)
-> Let a
fn2 :
String
-> Arg one
-> Arg two
-> (one -> two -> Expression)
-> Let ((Expression -> Expression -> Expression) -> a)
-> Let a
fn3 :
String
-> Arg one
-> Arg two
-> Arg three
-> (one -> two -> three -> Expression)
-> Let ((Expression -> Expression -> Expression -> Expression) -> a)
-> Let a

Converting to an Expression

toExpression : Let Expression -> Expression
withBody : (val -> Expression) -> Let val -> Expression

Define the body of your let at the bottom instead of the top so it matches the generated syntax a bit closer.

These two are equivalent import Elm import Elm.Let as Let

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




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

And will generate

  let
      one = "Hello"
      two = "World!"
  in
  one ++ two