Skip to content

Instantly share code, notes, and snippets.

@happy-bracket
Last active December 26, 2019 11:18
Show Gist options
  • Save happy-bracket/abe8a51fae4784717ac808f9c97870c4 to your computer and use it in GitHub Desktop.
Save happy-bracket/abe8a51fae4784717ac808f9c97870c4 to your computer and use it in GitHub Desktop.
TEA, but in Haskell
{-# LANGUAGE GADTs #-}
module Main where
newtype Login = Login String
newtype Password = Password String
data LoginState = Auth {
login :: Login,
password :: Password
} | Nop
data LoginMutation where
Input :: Either Login Password -> LoginMutation
LoginClicked :: LoginMutation
data LoginEffect where
TryLogin :: Login -> Password -> LoginEffect
update :: LoginState -> LoginMutation -> (LoginState, [LoginEffect])
update s (Input (Left l)) = (s { login = l }, [])
update s (Input (Right p)) = (s { password = p }, [])
update s LoginClicked = (s, [TryLogin (login s) (password s)])
-- TEA with transactional-like mutations
type Upd s m e = s -> m -> (s, [e])
data TMutation s e = TMutation {
state :: s -> s,
effs :: s -> [e]
}
type TUpd s e = Upd s (TMutation s e) e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment