This module is for building let expressions.
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.toExpressionWill translate into
let
one =
"Hello!"
two =
"World"
in
one ++ twoHere'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.toExpressionWill generate
let
( first, second ) =
( "Hello", "World!" )
in
first ++ secondAnd 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.toExpressionWill generate:
let
{ first, second } =
{ first = "Hello", second = "world!" }
in
first ++ secondHere'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.toExpressionwill generate
let
myFn arg =
if arg then
"True"
else
"False"
in
myFn TrueDefine 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