JSFiddle - React, Tailwind, and code Playground

by hallettj

HTML

<script src="https://github.com/jivesoftware/tAMD/blob/master/define.js"></script>
<script src="https://raw.github.com/gist/4043719/745baf3ce22df321a622e3d34b534da7f9772ea6/state.js"></script>
<ul id="log"></ul>

JavaScript

require(['state'], function(state) {

    function fooCounter(array) {
        if (array.length === 0) {
            return state.get().bind(function(counts) {
                return state.pure(counts['foo']);
            });
        }

        var head = array[0];
        var tail = array.slice(1);

        return state.get().bind(function(counts) {
            var count = counts[head] || 0;
            counts[head] = count + 1;

            return state.put(counts).bind(function(_) {
                return fooCounter(tail);
            });
        });
    }

    function getFooCount(array) {
        return fooCounter(array).evalState({});
    }

    $('#log').append('<li>'+ getFooCount(['foo', 'foo', 'foo', 'bar', 'foo']) + '</li>');
});