Skip to content

Instantly share code, notes, and snippets.

@mfeineis
Created March 17, 2018 23:56
Show Gist options
  • Save mfeineis/be0cf24bfbd7458853b209dd452048e5 to your computer and use it in GitHub Desktop.
Save mfeineis/be0cf24bfbd7458853b209dd452048e5 to your computer and use it in GitHub Desktop.
An exploration of the IntentsAndFacts approach to see what can be improved further - https://ellie-app.com/7TNrzrB6Na1/0
module Main exposing (main)
import Html exposing (Html)
import Html.Events as Events exposing (onClick)
import Http
import Json.Decode as Decode exposing (Decoder, Value)
type CounterState
= CounterState
{ count : Int
}
type CounterMsg
= Decrement
| Increment
initCounter : Int -> CounterState
initCounter count =
CounterState { count = count }
applyCounter : (model -> CounterState) -> CounterMsg -> model -> CounterState
applyCounter selector msg model =
let
(CounterState { count }) =
selector model
in
case msg of
Decrement ->
CounterState { count = count - 1 }
Increment ->
CounterState { count = count + 1 }
viewCounter : (model -> CounterState) -> (CounterMsg -> msg) -> model -> Html msg
viewCounter selector toMsg model =
let
(CounterState { count }) =
selector model
in
Html.div []
[ Html.button [ onClick Decrement ] [ Html.text "-" ]
, Html.span [] [ Html.text (toString count) ]
, Html.button [ onClick Increment ] [ Html.text "+" ]
]
|> Html.map toMsg
type alias Model =
{ alice : CounterState
, bob : CounterState
, clicked : Int
}
type Intent
= AskWhoIsKingInTheNorth
| Reset
| StateFact Fact
type Fact
= CounterUpdated CounterId CounterMsg
| KingInTheNorthReceived (Result Http.Error Character)
| HasBeenReset
type Consequence
= FetchJonSnow
type alias Character =
{ name : String
, titles : List String
}
decodeCharacter : Decoder Character
decodeCharacter =
Decode.map2 Character
(Decode.field "name" Decode.string)
(Decode.field "titles" (Decode.list Decode.string))
main : Program Value Model Intent
main =
Html.programWithFlags <|
wrap
{ apply = apply
, init = init
, interpret = interpret
, join = StateFact
, produce = produce
, subscriptions = subscriptions
, view = view
}
subscriptions : Model -> Sub Intent
subscriptions model =
Sub.none
init : Value -> ( Model, List Fact, List Consequence )
init _ =
( { alice = initCounter 0
, bob = initCounter 0
, clicked = 0
}
, [ HasBeenReset ]
, [ FetchJonSnow ]
)
apply : Fact -> Model -> Model
apply fact model =
case fact of
CounterUpdated Alice subMsg ->
{ model
| clicked = model.clicked + 1
, alice = applyCounter .alice subMsg model
}
CounterUpdated Bob subMsg ->
{ model
| clicked = model.clicked + 1
, bob = applyCounter .bob subMsg model
}
HasBeenReset ->
{ alice = initCounter 1
, bob = initCounter 1
, clicked = 0
}
KingInTheNorthReceived (Ok { name, titles }) ->
Debug.log (name ++ "/" ++ String.join "," titles)
model
KingInTheNorthReceived (Err reason) ->
Debug.log ("Couldn't find the answer: " ++ toString reason)
model
interpret : Intent -> Model -> ( List Fact, List Consequence )
interpret msg model =
case msg of
AskWhoIsKingInTheNorth ->
( [], [ FetchJonSnow ] )
Reset ->
( [ HasBeenReset ], [] )
StateFact fact ->
( [ fact ], [] )
produce : Consequence -> Model -> Cmd Fact
produce fx model =
case fx of
FetchJonSnow ->
let
request =
Http.get
"https://anapioficeandfire.com/api/characters/583"
decodeCharacter
in
Http.send KingInTheNorthReceived request
type CounterId
= Alice
| Bob
renderAlice =
viewCounter .alice (StateFact << CounterUpdated Alice)
renderBob =
viewCounter .bob (StateFact << CounterUpdated Bob)
view : Model -> Html Intent
view model =
Html.div []
[ Html.text ("Times clicked: " ++ toString model.clicked)
, Html.button [ onClick Reset ] [ Html.text "Reset" ]
, renderAlice model
, renderBob model
]
-- Helpers
wrap setup =
let
merge =
do setup.apply setup.produce
coalesce ( model, cmd ) =
( model, Cmd.map setup.join cmd )
doInit ( model, facts, fxs ) =
merge model ( facts, fxs ) |> coalesce
init flags =
setup.init flags |> doInit
update intent model =
setup.interpret intent model |> merge model |> coalesce
in
{ init = init
, subscriptions = setup.subscriptions
, update = update
, view = setup.view
}
type alias ProduceEffect fx fact model =
fx -> model -> Cmd fact
type alias ApplyFact fact model =
fact -> model -> model
do : ApplyFact fact model -> ProduceEffect fx fact model -> model -> ( List fact, List fx ) -> ( model, Cmd fact )
do apply produce model ( facts, fxs ) =
mergeFxs produce (List.foldl apply model facts) Cmd.none fxs
mergeFxs : ProduceEffect fx fact model -> model -> Cmd fact -> List fx -> ( model, Cmd fact )
mergeFxs produce model cmd fxs =
case fxs of
fx :: rest ->
mergeFxs produce model (Cmd.batch [ cmd, produce fx model ]) rest
[] ->
( model, cmd )

MIT License

Copyright (c) 2018 Martin Feineis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment