Skip to content

Instantly share code, notes, and snippets.

@DrBoolean
Created January 7, 2016 21:04

Revisions

  1. Brian Lonsdorf created this gist Jan 7, 2016.
    29 changes: 29 additions & 0 deletions binary_baby.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import State, {get, put, modify} from 'fantasy-states'
    import {prop, compose, map, chain, merge, always} from 'ramda'


    // Unfortunately Binary Gendered Baby Page
    //==========================================

    const babies = [{id: 2, name: 'Anjali', sex: 'F'}, {id: 3, name: 'Antonio', sex: 'M'}]

    // isFemale :: Baby -> Bool
    const isFemale = b => b.sex === 'F'

    // findBaby :: Int -> Baby
    const findBaby = i => babies[i]

    // updatePrefs :: Baby -> State Prefs Baby
    const updatePrefs = b => modify(merge({bgColor: isFemale(b) ? 'pink' : 'blue'})).map(always(b))

    // html :: Baby -> Prefs -> Html
    const html = (b, prefs) => `<div style={background-color: ${prefs.bgColor}, font: ${prefs.font} }>${b.name}</div>`

    // drawPage :: Baby -> State Prefs Html
    const drawPage = b => get.map(prefs => html(b, prefs))

    // app :: State Int Html
    const app = compose(chain(drawPage), chain(updatePrefs), map(findBaby))

    app(State.of(0)).evalState({font: 'cursive'})
    // <div style={background-color: pink, font: cursive }>Anjali</div>