Skip to content

Commit

Permalink
Refactor state changes into single-minded transformers
Browse files Browse the repository at this point in the history
This way, higher-level functions that handle events can be expressed in
a series of easier-to-read functions
  • Loading branch information
hoelzro committed Mar 6, 2016
1 parent 9b570d8 commit 27726f5
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,29 @@ generateRandomCard state =
let (ngram, seed1) = Random.generate (randomNgram ngrams) state.seed
in ({state | seed = seed1}, makeCard ngram)

initializeRNG : Time -> State -> State
initializeRNG t state = { state | seed = Random.initialSeed <| round t, initialized = True }

decrementLockTime : Int -> State -> State
decrementLockTime lockTime state = { state | lockTime = Just <| lockTime - clockSpeed }

clearLock : State -> State
clearLock state = { state | lockTime = Nothing }

resetCard : State -> State
resetCard state = { state | currentCard = blankCard state.currentCard }

lockExpired : Int -> Bool
lockExpired lockTime = lockTime - clockSpeed < 0

setUpNewCard : State -> State
setUpNewCard state =
let (newState, card) = generateRandomCard state
in { newState | currentCard = card }

lockUI : State -> State
lockUI state = { state | lockTime = Just incorrectLockTime }

handleClock : Time -> State -> State
handleClock t state =
if state.initialized
Expand All @@ -40,11 +58,10 @@ handleClock t state =
Nothing -> state
Just lockTime ->
if lockTime - clockSpeed < 0
then { state | lockTime = Nothing, currentCard = blankCard state.currentCard }
else { state | lockTime = Just <| lockTime - clockSpeed }
then clearLock <| resetCard state
else decrementLockTime lockTime state
else
let tempState = { state | seed = Random.initialSeed <| round t, initialized = True }
in setUpNewCard tempState
setUpNewCard <| initializeRNG t state

handleKeypress : Char -> State -> State
handleKeypress c state =
Expand All @@ -55,7 +72,7 @@ handleKeypress c state =
in case cardState newCard of
Tutor.Card.Complete -> setUpNewCard state
Tutor.Card.Incomplete -> { state | currentCard = newCard }
Tutor.Card.Incorrect -> { state | currentCard = newCard, lockTime = Just incorrectLockTime }
Tutor.Card.Incorrect -> lockUI <| { state | currentCard = newCard }

view : State -> Html
view {currentCard} =
Expand Down

0 comments on commit 27726f5

Please sign in to comment.