Skip to content

Instantly share code, notes, and snippets.

@happy-bracket
Last active December 26, 2019 11:18
Show Gist options
  • Select an option

  • Save happy-bracket/abe8a51fae4784717ac808f9c97870c4 to your computer and use it in GitHub Desktop.

Select an option

Save happy-bracket/abe8a51fae4784717ac808f9c97870c4 to your computer and use it in GitHub Desktop.

Revisions

  1. happy-bracket revised this gist Dec 26, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions tea2.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    -- 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
  2. happy-bracket created this gist Dec 26, 2019.
    22 changes: 22 additions & 0 deletions tea.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    {-# 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)])