My wife's great tip on how I should open my presentation is to tell you more about
myself, because according to her, it helps create interest
in the presentation, which again according to her, is way too technical,
and super boring, and she hardly thinks anyone will come, let alone
enjoy it.
gizra
// @amitaibu
Elm // Elm Architecture
Model -> Update -> View
module Person where
import Effects exposing (Effects)
import Html exposing (button, div, pre, text, Html)
import Html.Events exposing (onClick)
-- MODEL
type alias Model =
{ age : Int
, kids : Int
, name : String
}
initialModel : Model
initialModel =
{ age = 38
, kids = 3
, name = "Amitai"
}
init : (Model, Effects Action)
init =
( initialModel
, Effects.none
)
-- UPDATE
type Action = Decrement | Increment
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Decrement ->
( { model | kids = model.kids - 1 }
, Effects.none
)
Increment ->
( { model | kids = model.kids + 1 }
, Effects.none
)
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ div [] [ text <| "Name: " ++ model.name ]
, div [] [ text <| "Age: " ++ (toString model.age) ]
, div [] [ text <| "Kids num: " ++ (toString model.kids) ]
, button [ onClick address Decrement ] [ text "-" ]
, button [ onClick address Increment ] [ text "+" ]
, pre [] [ text (toString model) ]
]
--
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ viewName model.name
, viewAge model.age
, viewKids model.age
, button [ onClick address Decrement ] [ text "-" ]
, button [ onClick address Increment ] [ text "+" ]
, pre [] [ text (toString model) ]
]
viewName : String -> Html
viewName name =
div [] [ text <| "Name: " ++ name ]
viewAge : Int -> Html
viewAge age =
div [] [ text <| "Age: " ++ (toString age) ]
viewKids : Int -> Html
viewKids kids =
div [] [ text <| "Kids num: " ++ (toString kids) ]
Compile Error Vs Runtime Error
Types 101
type Bool = False | True
type Vehicle = Boat | Plane | Car Int
type alias Model =
{ name : String
, vehicle : Vehicle
}
Type Safety
--
type alias Model =
{ age : Int
, kids : Int
, name : String
}
initialModel : Model
initialModel =
{ age = 38
, kids = 3
, name = "Amitai"
}
type Kids = Kids Int
type alias Model =
{ age : Int
, kids : Kids
, name : String
}
initialModel : Model
initialModel =
{ age = 38
, kids = Kids 3
, name = "Amitai"
}
viewKids : Int -> Html
viewKids kids =
div [] [ text <| "Kids num: " ++ (toString kids) ]
--
viewKids : Kids -> Html
viewKids kids =
let
(Kids val) = kids
in
div [] [ text <| "Kids num: " ++ (toString val) ]
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Decrement ->
( { model | kids = model.kids - 1 }
, Effects.none
)
Increment ->
( { model | kids = model.kids + 1 }
, Effects.none
)
--
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Decrement ->
let
(Kids val) = model.kids
kids' = Kids (val - 1)
in
( { model | kids = kids' }
, Effects.none
)
Increment ->
let
(Kids val) = model.kids
kids' = Kids (val + 1)
in
( { model | kids = kids' }
, Effects.none
)
Business logic from the Data level
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Decrement ->
let
(Kids val) = model.kids
kids' = Kids (val - 1)
in
( { model | kids = kids' }
, Effects.none
)
Increment ->
let
(Kids val) = model.kids
kids' = Kids (val + 1)
in
( { model | kids = kids' }
, Effects.none
)
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Decrement ->
let
(Kids val) = model.kids
kids' = if val < 1 then Kids 0 else Kids (val - 1)
in
( { model | kids = kids' }
, Effects.none
)
Increment ->
let
(Kids val) = model.kids
kids' = if val > 4 then Kids 5 else Kids (val + 1)
in
( { model | kids = kids' }
, Effects.none
)
Buisness Logic requires Testing
decrementActionSuite : Test
decrementActionSuite =
suite "Decerment action"
[ test "zero count" (assertEqual 0 (updateKids Person.Decrement 0))
, test "positive count" (assertEqual 0 (updateKids Person.Decrement 1))
, test "top limit count" (assertEqual 4 (updateKids Person.Decrement 5))
]
suite "Decerment action"
[ test "negative count" (assertEqual 0 1)
]
decrementActionSuite : Test
decrementActionSuite =
suite "Decerment action"
-- Kids = 0 -> Kids = 0
[ test "zero count" (assertEqual 0 (updateKids Person.Decrement 0))
-- Kids = 1 -> Kids = 0
, test "positive count" (assertEqual 0 (updateKids Person.Decrement 1))
-- Kids = 5 -> Kids = 4
, test "top limit count" (assertEqual 4 (updateKids Person.Decrement 5))
]
updateKids : Person.Action -> Int -> Int
updateKids action initialKids =
let
model = { initialModel | kids = (Person.Kids initialKids) }
model' = fst <| Person.update action model
(Person.Kids kids') = model'.kids
in
kids'