Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save developer-rakeshpaul/0160a5c3f1fefd6ed2be8b8e6c7af193 to your computer and use it in GitHub Desktop.
Save developer-rakeshpaul/0160a5c3f1fefd6ed2be8b8e6c7af193 to your computer and use it in GitHub Desktop.

Revisions

  1. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 0-Simple counter: different implementations...
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Simple counter: different implementations... code verbosity vs expressiveness
    different implementations of the simple counter app... code verbosity vs expressiveness
  2. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 24, 2017. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  3. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 24, 2017. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 24, 2017. 7 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  5. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 24, 2017. 9 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion 0-Simple counter: different implementations...
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Simple counter: different implementations...
    Simple counter: different implementations... code verbosity vs expressiveness
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion 3-cyclejs-counter.js → 4a-cyclejs-counter.js
    Original file line number Diff line number Diff line change
    @@ -23,4 +23,4 @@ function main(sources) {

    Cycle.run(main, {
    DOM: makeDOMDriver('#main-container')
    });
    });
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  6. @ssrdjan-fadv ssrdjan-fadv renamed this gist Feb 23, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @ssrdjan-fadv ssrdjan-fadv renamed this gist Feb 23, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 23, 2017. 7 changed files with 2 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion 2-calmm-js-counter.js
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,4 @@ export default ({value = U.atom(0)}) =>
    <button onClick={() => value.modify(R.add(-1))}>-</button>
    </div>

    ReactDOM.render(<Main/>, document.getElementById("app"))
    ReactDOM.render(<Main/>, document.getElementById("app"))
    4 changes: 1 addition & 3 deletions 2-counter-pux.purs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    data Action = Increment | Decrement

    type State = Int

    update :: Action -> State -> State
    update Increment count = count + 1
    update Decrement count = count - 1
    @@ -13,5 +12,4 @@ view count =
    [ button [ onClick (const Increment) ] [ text "Increment" ]
    , span [] [ text (show count) ]
    , button [ onClick (const Decrement) ] [ text "Decrement" ]
    ]

    ]
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  9. @ssrdjan-fadv ssrdjan-fadv renamed this gist Feb 23, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 17, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions 2-hyperapp.js
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@ app({
    add: model => model + 1,
    sub: model => model - 1
    },
    view: (model, actions) => html`
    view: (model, actions) =>
    <div>
    <button onclick=${actions.add}>+</button>
    <h1>${model}</h1>
    <button onclick=${actions.sub} disabled=${model <= 0}>-</button>
    </div>`
    <button onclick={actions.add}>+</button>
    <h1>{model}</h1>
    <button onclick={actions.sub} disabled={model <= 0}>-</button>
    </div>
    })
  11. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 12, 2017. 2 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions 0-Simple counter: different implementations...
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Simple counter: different implementations...
    2 changes: 2 additions & 0 deletions 2-hyperapp.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import { h, app } from "hyperapp"

    app({
    model: 0,
    update: {
  12. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 11, 2017. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions 3-mobx-counter.js
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,15 @@ import React from 'react'
    import {render} from 'react-dom'

    const state = { n: 0 };
    const onclick = () => state.n++;
    const incr = () => state.n++;
    const decr = () => state.n--;

    const view = observer(() => <div>
    <h1>clicked {state.n} times</h1>
    <button onclick={onclick}>click me!</button>
    </div>)
    const view = observer(() =>
    <div>
    <h1>{state.n}</h1>
    <button onclick={incr}>+</button>
    <button onclick={decr}>-</button>
    </div>)

    render(
    <view />,
  13. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 11, 2017. 9 changed files with 0 additions and 14 deletions.
    2 changes: 0 additions & 2 deletions calmm-js-counter.elm → 1-calmm-js-counter.elm
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    -- counter: calmm-js way ported to elm

    import Dom exposing (..)
    import Lens exposing (Lens)

    2 changes: 0 additions & 2 deletions calmm-js-counter.js → 2-calmm-js-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // counter: calmm-js

    import * as R from "ramda"
    import * as U from "karet.util"
    import React from "karet"
    File renamed without changes.
    2 changes: 0 additions & 2 deletions mobx-counter.js → 3-mobx-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    / counter: mobx-react implementation

    import { observable } from 'mobx';
    import { observer } from 'mobx-react';
    import React from 'react'
    File renamed without changes.
    2 changes: 0 additions & 2 deletions cyclejs-counter.js → 5-cyclejs-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // counter: cyclejs

    import xs from 'xstream';
    import Cycle from '@cycle/xstream-run';
    import {div, button, p, makeDOMDriver} from '@cycle/dom';
    2 changes: 0 additions & 2 deletions choo-choo-counter.js → 6-choo-choo-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // counter: choo choo

    const choo = require('../../')
    const html = require('../../html')

    2 changes: 0 additions & 2 deletions elm-counter.elm
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    -- counter: elm implementation

    import Html exposing (Html, button, div, text)
    import Html.App as App
    import Html.Events exposing (onClick)
    2 changes: 0 additions & 2 deletions redux-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // counter: redux implementation

    import React, { Component, PropTypes } from 'react'
    import ReactDOM from 'react-dom'
    import { createStore } from 'redux'
  14. @ssrdjan-fadv ssrdjan-fadv revised this gist Feb 11, 2017. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions hyperapp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    app({
    model: 0,
    update: {
    add: model => model + 1,
    sub: model => model - 1
    },
    view: (model, actions) => html`
    <div>
    <button onclick=${actions.add}>+</button>
    <h1>${model}</h1>
    <button onclick=${actions.sub} disabled=${model <= 0}>-</button>
    </div>`
    })
  15. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 25, 2016. 1 changed file with 12 additions and 21 deletions.
    33 changes: 12 additions & 21 deletions mobx-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,19 @@
    // counter: mobx implementation
    / counter: mobx-react implementation

    import { observable, autorun } from 'mobx';
    import { diff, patch } from 'virtual-dom';
    import { observable } from 'mobx';
    import { observer } from 'mobx-react';
    import React from 'react'
    import {render} from 'react-dom'

    let mount = function mount(element, view, state) {
    let tree = <noop/>;
    function render() {
    const newTree = view(state);
    const patches = diff(tree, newTree);
    element = patch(element, patches);
    tree = newTree;
    }
    autorun(() => render());
    }

    const state = { @observable n: 0 };
    const state = { n: 0 };
    const onclick = () => state.n++;

    const view = (state) => <div>
    const view = observer(() => <div>
    <h1>clicked {state.n} times</h1>
    <button onclick={onclick}>click me!</button>
    </div>
    </div>)

    mount(
    document.querySelector('#app'),
    view, state
    );
    render(
    <view />,
    document.querySelector('#app')
    );
  16. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 25, 2016. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions counter-pux.purs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    data Action = Increment | Decrement

    type State = Int

    update :: Action -> State -> State
    update Increment count = count + 1
    update Decrement count = count - 1

    view :: State -> Html Action
    view count =
    div
    []
    [ button [ onClick (const Increment) ] [ text "Increment" ]
    , span [] [ text (show count) ]
    , button [ onClick (const Decrement) ] [ text "Decrement" ]
    ]

  17. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 15, 2016. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions elmar-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import React from 'react'

    export const init = count => count

    const Action = {
    Increment: x => x + 1,
    Decrement: x => x - 1
    }

    export const update = (action, model) => action(model)

    export const view = (signal, model) => (
    <div>
    <button onClick={signal(Action.Decrement)}>-</button>
    <div>{model}</div>
    <button onClick={signal(Action.Increment)}>+</button>
    </div>
    )

    export default {init, update, view}
  18. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 2 changed files with 6 additions and 4 deletions.
    7 changes: 5 additions & 2 deletions calmm-js-counter.js
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,14 @@

    import * as R from "ramda"
    import * as U from "karet.util"
    import React from "karet"
    import React from "karet"
    import ReactDOM from "react-dom"

    export default ({value = U.atom(0)}) =>
    <div>
    <div>Count: {value}</div>
    <button onClick={() => value.modify(R.add(+1))}>+</button>
    <button onClick={() => value.modify(R.add(-1))}>-</button>
    </div>
    </div>

    ReactDOM.render(<Main/>, document.getElementById("app"))
    3 changes: 1 addition & 2 deletions mobx-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    // counter: mobx implementation

    import { observable } from 'mobx';
    import { observable, autorun } from 'mobx';
    import { diff, patch } from 'virtual-dom';
    import { autorun } from 'mobx';

    let mount = function mount(element, view, state) {
    let tree = <noop/>;
  19. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions choo-choo-counter.js
    Original file line number Diff line number Diff line change
    @@ -15,19 +15,16 @@ app.model({
    })

    const mainView = (state, prev, send) => {
    const count = state.counter

    return html`
    <main class="app">
    <button onclick=${() => send('increment')}>Increment</button>
    <button onclick=${() => send('decrement')}>Decrement</button>
    <p>count</p>
    <p>state.counter</p>
    </main>
    }

    app.router((route) => [
    route('/', mainView)
    ])

    const tree = app.start()
    document.body.appendChild(tree)
    document.body.appendChild(app.start())
  20. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions choo-choo-counter.js
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ const mainView = (state, prev, send) => {
    <main class="app">
    <button onclick=${() => send('increment')}>Increment</button>
    <button onclick=${() => send('decrement')}>Decrement</button>
    <p>count</p>
    </main>
    }

  21. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions calmm-js-counter.elm
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    -- counter: calmm-js way ported to elm

    module Counter exposing (..)

    import Dom exposing (..)
    import Lens exposing (Lens)

  22. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions elm-counter.elm
    Original file line number Diff line number Diff line change
    @@ -15,18 +15,14 @@ type alias Model = Int
    model : Model
    model = 0

    type Msg
    = Increment
    | Decrement
    type Msg = Increment | Decrement

    update : Msg -> Model -> Model
    update msg model =
    case msg of
    Increment ->
    model + 1
    Decrement ->
    model - 1

    Increment -> model + 1
    Decrement -> model - 1

    view : Model -> Html Msg
    view model =
    div []
  23. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. No changes.
  24. @ssrdjan-fadv ssrdjan-fadv revised this gist Nov 13, 2016. 6 changed files with 10 additions and 30 deletions.
    2 changes: 2 additions & 0 deletions calmm-eml-counter.elm → calmm-js-counter.elm
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    -- counter: calmm-js way ported to elm

    module Counter exposing (..)

    import Dom exposing (..)
    2 changes: 2 additions & 0 deletions calmm-js-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // counter: calmm-js

    import * as R from "ramda"
    import * as U from "karet.util"
    import React from "karet"
    2 changes: 2 additions & 0 deletions choo-choo-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // counter: choo choo

    const choo = require('../../')
    const html = require('../../html')

    2 changes: 2 additions & 0 deletions cyclejs-counter.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // counter: cyclejs

    import xs from 'xstream';
    import Cycle from '@cycle/xstream-run';
    import {div, button, p, makeDOMDriver} from '@cycle/dom';
    3 changes: 0 additions & 3 deletions elm-counter.elm
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,10 @@ main =
    , update = update
    }

    -- MODEL
    type alias Model = Int
    model : Model
    model = 0

    -- UPDATE
    type Msg
    = Increment
    | Decrement
    @@ -29,7 +27,6 @@ update msg model =
    Decrement ->
    model - 1

    -- VIEW
    view : Model -> Html Msg
    view model =
    div []
    29 changes: 2 additions & 27 deletions redux-counter.js
    Original file line number Diff line number Diff line change
    @@ -11,37 +11,12 @@ class Counter extends Component {
    onDecrement: PropTypes.func.isRequired
    }

    incrementIfOdd = () => {
    if (this.props.value % 2 !== 0) {
    this.props.onIncrement()
    }
    }

    incrementAsync = () => {
    setTimeout(this.props.onIncrement, 1000)
    }

    render() {
    const { value, onIncrement, onDecrement } = this.props
    return (
    <p>
    Clicked: {value} times
    {' '}
    <button onClick={onIncrement}>
    +
    </button>
    {' '}
    <button onClick={onDecrement}>
    -
    </button>
    {' '}
    <button onClick={this.incrementIfOdd}>
    Increment if odd
    </button>
    {' '}
    <button onClick={this.incrementAsync}>
    Increment async
    </button>
    <button onClick={onIncrement}>+</button>
    <button onClick={onDecrement}>-</button>
    </p>
    )
    }
  25. @ssrdjan-fadv ssrdjan-fadv created this gist Nov 13, 2016.
    12 changes: 12 additions & 0 deletions calmm-eml-counter.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    module Counter exposing (..)

    import Dom exposing (..)
    import Lens exposing (Lens)

    counter : Lens m Int -> Html m
    counter value =
    div []
    [ button [onClick (Lens.modify value ((+) -1))] [text "-"]
    , textAs toString value
    , button [onClick (Lens.modify value ((+) 1))] [text "+"]
    ]
    10 changes: 10 additions & 0 deletions calmm-js-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import * as R from "ramda"
    import * as U from "karet.util"
    import React from "karet"

    export default ({value = U.atom(0)}) =>
    <div>
    <div>Count: {value}</div>
    <button onClick={() => value.modify(R.add(+1))}>+</button>
    <button onClick={() => value.modify(R.add(-1))}>-</button>
    </div>
    30 changes: 30 additions & 0 deletions choo-choo-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    const choo = require('../../')
    const html = require('../../html')

    const app = choo()
    app.model({
    state: {
    counter: 0
    },
    reducers: {
    increment: (data, state) => ({ counter: state.counter + 1 }),
    decrement: (data, state) => ({ counter: state.counter - 1 })
    }
    })

    const mainView = (state, prev, send) => {
    const count = state.counter

    return html`
    <main class="app">
    <button onclick=${() => send('increment')}>Increment</button>
    <button onclick=${() => send('decrement')}>Decrement</button>
    </main>
    }

    app.router((route) => [
    route('/', mainView)
    ])

    const tree = app.start()
    document.body.appendChild(tree)
    26 changes: 26 additions & 0 deletions cyclejs-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import xs from 'xstream';
    import Cycle from '@cycle/xstream-run';
    import {div, button, p, makeDOMDriver} from '@cycle/dom';

    function main(sources) {
    let action$ = xs.merge(
    sources.DOM.select('.decrement').events('click').map(ev => -1),
    sources.DOM.select('.increment').events('click').map(ev => +1)
    );

    let count$ = action$.fold((x,y) => x + y, 0);

    return {
    DOM: count$.map(count =>
    div([
    button('.decrement', 'Decrement'),
    button('.increment', 'Increment'),
    p('Counter: ' + count)
    ])
    )
    };
    }

    Cycle.run(main, {
    DOM: makeDOMDriver('#main-container')
    });
    39 changes: 39 additions & 0 deletions elm-counter.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    -- counter: elm implementation

    import Html exposing (Html, button, div, text)
    import Html.App as App
    import Html.Events exposing (onClick)

    main =
    App.beginnerProgram
    { model = model
    , view = view
    , update = update
    }

    -- MODEL
    type alias Model = Int
    model : Model
    model = 0

    -- UPDATE
    type Msg
    = Increment
    | Decrement

    update : Msg -> Model -> Model
    update msg model =
    case msg of
    Increment ->
    model + 1
    Decrement ->
    model - 1

    -- VIEW
    view : Model -> Html Msg
    view model =
    div []
    [ button [ onClick Decrement ] [ text "-" ]
    , div [] [ text (toString model) ]
    , button [ onClick Increment ] [ text "+" ]
    ]
    29 changes: 29 additions & 0 deletions mobx-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // counter: mobx implementation

    import { observable } from 'mobx';
    import { diff, patch } from 'virtual-dom';
    import { autorun } from 'mobx';

    let mount = function mount(element, view, state) {
    let tree = <noop/>;
    function render() {
    const newTree = view(state);
    const patches = diff(tree, newTree);
    element = patch(element, patches);
    tree = newTree;
    }
    autorun(() => render());
    }

    const state = { @observable n: 0 };
    const onclick = () => state.n++;

    const view = (state) => <div>
    <h1>clicked {state.n} times</h1>
    <button onclick={onclick}>click me!</button>
    </div>

    mount(
    document.querySelector('#app'),
    view, state
    );
    74 changes: 74 additions & 0 deletions redux-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    // counter: redux implementation

    import React, { Component, PropTypes } from 'react'
    import ReactDOM from 'react-dom'
    import { createStore } from 'redux'

    class Counter extends Component {
    static propTypes = {
    value: PropTypes.number.isRequired,
    onIncrement: PropTypes.func.isRequired,
    onDecrement: PropTypes.func.isRequired
    }

    incrementIfOdd = () => {
    if (this.props.value % 2 !== 0) {
    this.props.onIncrement()
    }
    }

    incrementAsync = () => {
    setTimeout(this.props.onIncrement, 1000)
    }

    render() {
    const { value, onIncrement, onDecrement } = this.props
    return (
    <p>
    Clicked: {value} times
    {' '}
    <button onClick={onIncrement}>
    +
    </button>
    {' '}
    <button onClick={onDecrement}>
    -
    </button>
    {' '}
    <button onClick={this.incrementIfOdd}>
    Increment if odd
    </button>
    {' '}
    <button onClick={this.incrementAsync}>
    Increment async
    </button>
    </p>
    )
    }
    }

    const counter = (state = 0, action) => {
    switch (action.type) {
    case 'INCREMENT':
    return state + 1
    case 'DECREMENT':
    return state - 1
    default:
    return state
    }
    }

    const store = createStore(counter)
    const rootEl = document.getElementById('root')

    const render = () => ReactDOM.render(
    <Counter
    value={store.getState()}
    onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
    onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
    />,
    rootEl
    )

    render()
    store.subscribe(render)