This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const daggy = require('daggy'); | |
| const {identity} = require('ramda'); | |
| const Either = daggy.taggedSum('Either', { | |
| Left: ['__value'], | |
| Right: ['__value'], | |
| }); | |
| Either.of = Either.Right; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const validUnits = ['km', 'm', 'cm', 'mm', 'in', 'ft', 'mile']; | |
| const isLengthObj = (lo) => | |
| '_val' in lo && | |
| '_unit' in lo && | |
| typeof lo._val === 'number' && | |
| typeof lo._unit === 'string' && | |
| validUnits.includes(lo._unit); | |
| const formatUnit = (unit, val) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const promiseCache = (promFn) => { | |
| let cachedPromise = promFn(); | |
| return (refreshCache = false) => { | |
| if (refreshCache) cachedPromise = promFn(); | |
| return cachedPromise; | |
| } | |
| } | |
| // Pass a function that generates a promise instead of the promise, so it can be refreshed | |
| const cachedPromise = promiseCache(() => Promise.resolve(Math.random())); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HashTable { | |
| constructor(bucketSize = 1024) { | |
| this._bucketSize = bucketSize; | |
| this._data = new Array(bucketSize); | |
| } | |
| hashKey(key) { | |
| const h = JSON.stringify(key, Object.keys(key).sort()) | |
| .split('') | |
| .reduce((acc, cur, i) => acc + cur.charCodeAt(0) * (i+1), 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| export class StateDispatcher extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = props.state || {}; | |
| this._dispatch = this.dispatch.bind(this); | |
| } | |
| dispatch(action) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ArrayMonad = xs => { | |
| const map = fn => ArrayMonad(xs.map(x => fn(x))) | |
| const chain = fn => ArrayMonad(xs.reduce((ys, x) => [...ys, ...fn(x).xs], [])) | |
| const ap = mys => chain(f => mys['fantasy-land/map'](y => f(y))); | |
| return { | |
| xs, | |
| 'fantasy-land/map': map, | |
| 'fantasy-land/chain': chain, | |
| 'fantasy-land/ap': ap, | |
| 'constructor': ArrayMonad |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { parse, char, str, sequenceOf, choice } = require('arcsecond'); | |
| // parse creates a function we can use for parsing text | |
| const parseText = parse (combinedParser); | |
| console.log( | |
| parseText ('hello world') | |
| ) | |
| // -> [ 'hello', ' ', 'world' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { | |
| between, | |
| many, | |
| choice, | |
| sequenceOf, | |
| char, | |
| whitespace, | |
| anythingExcept, | |
| possibly, | |
| regex, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { | |
| parse, | |
| char, | |
| many, | |
| regex, | |
| anythingExcept, | |
| sepBy | |
| } = require('arcsecond'); | |
| const joinedMany = parser => many (parser) .map(x => x.join('')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const interpret = iterator => last => { | |
| const {value, done} = iterator.next(last); | |
| return (done) ? Promise.resolve(value) : value.then(interpret(iterator)); | |
| }; | |
| const asyncAwait = g => interpret(g())(); | |
| // ... Usage ... | |
| const addOneSoon = (x, t) => new Promise(resolve => { |
OlderNewer