npm install -g elm
add : Int -> Int -> Int add x y = x + y
add : Int -> Int -> Int
add x y =
let
sum = a + b
in
sum
type alias Description = String description : Description description = "abc"
type alias Point = { x : Float, y : Float }
origin : Point
origin =
{ x = 0, y = 0 }
type Fruit = Apple | Banana | Orange
fruitToColor: Fruit -> String
fruitToColor fruit =
case fruit of
Apple -> "green"
Banana -> "yellow"
Orange -> "orange"
type alias Point = { x : Float, y : Float }
type Shape
= Circle Point Float
| Rectangle Point Point
area : Shape -> Float
area shape =
case shape of
Circle center radius ->
pi * radius ^ 2
Rectangle c1 c2 ->
abs (c1.x - c2.x) * abs (c1.y - c2.y)
-- Here are four things that are equivalent in elm numbers : List Int numbers = [ 1,2,3,4 ] numbers : List Int numbers = [ 1..4 ] numbers : List Int numbers = 1 :: [ 2,3,4 ] numbers : List Int numbers = 1 :: 2 :: 3 :: 4 :: []
length : List a -> Int
length list =
case list of
[] ->
0
first :: rest ->
1 + length rest
type Maybe a = Nothing | Just a
type alias User =
{ name : String
, age : Maybe Int
}
canBuyAlcohol : User -> Bool
canBuyAlcohol user =
case user.age of
Nothing ->
False
Just age ->
age >= 18
concat : String -> String -> String concat a b = a ++ b listItem : String -> String listItem = concat "-> "
import Html exposing ( div, text )
import Html.Attributes exposing ( class )
sampleSection : Html
sampleSection =
div [ class "section" ] [ text "Section 1" ]
getOffers : String -> Task Http.Error (List Offer)
getOffers customerName =
let
url = "http://some.api/customer?name="
++ customerName
offersUrl id = "http://some.api/customer/"
++ id ++ "/offer"
in
Http.get customerDecode url
`andThen` (\c -> Http.get
offersDecoder (offersUrl c.id))
This code does not send any request:
getSiiliOffice : Task Http.Error String
getSiiliOffice =
Http.getString
"http://api.zippopotam.us/pl/50-073"
getSiiliOfficeCmd =
Task.perform
SiiliOfficeFailure
SiiliOfficeSuccess
getSiiliOffice
main =
Html.program
{ init = (initialModel, getSiiliOfficeCmd)
, view = view
, update = update
, subscriptions = always Sub.none
}
update msg model =
case msg of
...
GetSiiliOffice ->
(model, getSiiliOfficeCmd)
...
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = always
(Geolocation.changes CurrentPlace)
}
port check : String -> Cmd msg port suggestions : (List String -> msg) -> Sub msg
var app = Elm.Spelling.fullscreen();
app.ports.check.subscribe(function(word) {
var suggestions = spellCheck(word);
app.ports.suggestions.send(suggestions);
});
-- MODEL
type alias Model = { ... }
-- UPDATE
type Msg = Reset | ...
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Reset -> ...
...
-- VIEW
view : Model -> Html Msg
view model =
...
The basic idea is: nest The Elm Architecture pattern again and again. Example: Pair of gifs