Niemutowalność danych
Bo mutacja danych to efekt uboczny...
...a efektów ubocznych brak
Funkcja może tylko obliczyć nowy wynik i go zwrócić
Na szczęście funkcja to też wartość
import Html exposing (..)
main = Html.beginnerProgram { model = 0, update = update, view = view }
-- MODEL
type alias Model = Int
-- Message
type Msg = MakeCookie
-- UPDATE
update : Msg -> Model -> Model
update msg model =
case msg of
MakeCookie -> model + 1
-- VIEW
view : Model -> Html Msg
view model = img [src "img/cookie.jpg", class "cookie", onClick MakeCookie] []
import Html exposing (..)
main = Html.program { init = init, subscriptions = subscriptions, update = update, view = view }
init : (Model, Cmd Msg)
init = (0, Cmd.none)
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second MakeCookie
-- ..
type Msg = RemoveNotification notification | RestOfTypes
update : Msg -> Model -> (Model, Cmd msg)
update msg model =
case msg of
AddNotification notification ->
(
{ model
| notifications = model.notifications
|> List.reverse
|> (::) notification
|> List.reverse
}
, Notification.remove RemoveNotification notification
)
-- ..
module Notifications exposing (..)
add : (Notification -> msg) -> String -> Cmd msg
add tagger notification =
Task.perform tagger (Time.now
|> map (\time -> round (Time.inMilliseconds time))
|> map (\seed -> Random.step (Random.int minInt maxInt) (initialSeed seed))
|> map Tuple.first
|> map (\id -> Notification notification id))
remove : (Notification -> msg) -> Notification -> Cmd msg
remove tagger notification =
Task.perform tagger (Process.sleep (5 * Time.second)
|> Task.andThen (always <| Task.succeed notification))
port module User exposing (..)
import Json.Decode
port getUserId : String -> Cmd msg
port userId : (String -> msg) -> Sub msg
import * as Elm from './Main';
const app = Elm.Main.embed(document.getElementById('app'));
app.ports.getUserId.subscribe(() => {
getUserId()
.then((userId) => app.ports.userId.send(userId));
});