identity = λa. aconst identity = a => afunction identity(a){ return a; }identity = λa. aidentity = \a -> aidentity a = aidentity : a -> a
identity x = xxs : List Int
xs = [1,2,3]empty : List a
empty = []hw : String
hw = "hello world"w : Char
w = 'a'y : Int
y = 0
x : String
x = "hello"z : Float
z = 5.0type Teeth
= Jagged
| Twisted
| Rotten
type alias Monster =
{ name : String
, age : Float
, teeth : Teeth
}type Miles = Miles Int
three : Miles
three = Miles 3
add : Miles -> Miles -> Miles
add (Miles x) (Miles y) = Miles (x + y)type Foo = Bar Int | Foo String Int | Baz String
fooish : Foo
fooish = Bar 3
displayWith : Foo -> String -> String
displayWith foo name = case foo of
Bar x -> name ++ " " ++ toString x
Foo s x -> name ++ " " ++ s ++ ": " ++ toString x
Baz s -> name ++ " " ++ stype Foo = Bar
type alias Foo = Bar
x : Maybe Int
x = Just 3
f : Int -> Maybe Int
f x = if x > 0
then Just x
else Nothing
f : a -> btype Foo a = B a
type Baz = C Int | Dx : Foo Baz
x = B (C 12)Foo (C 20)
Foo Dadd3 : Int -> (Int -> (Int -> Int))
add3 : Int -> Int -> Int -> Int
add3 x y z = x + y + zconst add3 = x => y => z => x + y + zfunction add3(x){
return function(y){
return function(z){
return (x + y + z);
}}}someFunc : Int -> Int -> Int
someFunc x y = add3 x y 18 - 100add3 x : Int -> Int -> Int
add3 x y : Int -> Int
add3 x y 18 : Int
someFunc : Int -> Int -> Int
someFunc x y = add3 x y 18 - 100add3 x y 18 : Int
{
(-) (add3 x y 18) : Int -> Int
(-) (add3 x y 18)(100) : Int
type Maybe a = Just a | Nothing
threeInThere : Maybe Int
threeInThere = Just 3
putIntoMaybe : a -> Maybe a
putIntoMaybe = Just
notThere : Maybe a
notThere = Nothingadd : Maybe Int -> Maybe Int -> Maybe Int
add x y =
case x of
Just x_ ->
case y of
Just y_ -> Just (x_ + y_)
Nothing -> Nothing
Nothing -> Nothingadd : Maybe Int -> Maybe Int -> Maybe Int
add x y =
case (x, y) of
(Just x_, Just y_) -> Just (x_ + y_)
_ -> Nothingadd : Maybe Int -> Maybe Int -> Maybe Int
add = map2 (+)TEA can be represented in almost any language. In Elm, this pattern is enforced, and inescapable.
Other implementations of TEA include:
PUX - A PureScript library backed by React.js
Spork - A PureScript library backed by Halogen-VDOM
Franken Elm - React + Redux + TypeScript
Gloss - A Haskell library for OpenGL
program
: { init : (model, Cmd msg)
, update : msg -> model -> (model, Cmd msg)
, subscriptions : model -> Sub msg }
-> Program Never model msg{ init : (model, Cmd msg)
, update : msg -> model -> (model, Cmd msg)
, subscriptions : model -> Sub msg
}
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
0
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
0
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
0
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
0
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
0
1
type Count = Count
type alias Knocks = Int
main : Program Never Knocks Count
main = program
{ init = (0, Cmd.none)
, update = \Count knocks -> (knocks + 1, Cmd.none)
, view = \knocks ->
Html.div
[ Html.Attributes.style
[ ("padding", "20px") ] ]
[ Html.h3 []
[ Html.text ("Knocks: " ++ toString knocks) ]
, Html.button
[ Html.Events.onClick Count ]
[ Html.text "Count" ] ]
, subscriptions = \knocks -> Sub.none }Runtime
1
1
Do not fear installing extras,
they are not spooky.
Use packages from these sources when possible.
Packages that do IO are generally only in the elm-lang namespace.
Some packages expose their own version of `program`. You will want to identify those early in your development, as changing out the program function has deep implications.
Debug.log : String -> a -> aDebug.crash : String -> aelm make Main.elm --debugencounter : Witness -> Ghost -> Result Causality Witness
encounter witness ghost =
let
_ = Debug.log "witnessed" (witness, ghost)
in
case compareGhosts ghost AnneBoleyn of
LT ->
Ok witness
_ ->
Err Vanishedwitnessed: ({ name = "Jeff"} , FlyingDutchman)
encounter : Witness -> Ghost -> Result Causality Witness
encounter witness ghost =
case compareGhosts (Debug.log "ghost" ghost) AnneBoleyn of
LT ->
Ok witness
_ ->
Err Vanishedghost: FlyingDutchman
enhance : ColdAudio -> HotAudio
enhance = Debug.crash "not yet implemented `enhance`"
recordAndAnalyze : Time -> Time -> Maybe Conclusion
recordAndAnalyze start now =
if start >= now then
record now
|> enhance
|> analyze
|> Just
else NothingrevealMystery : Bool -> Response
revealMystery handle =
case handle of
False ->
Relax
True ->
Debug.crash "you can't handle the truth"
This function is not total!
Uncover the mysteries of FP frontend development,
and get out there and write some awesome code!