Skip to content

Instantly share code, notes, and snippets.

View francisrstokes's full-sized avatar
🎥
Low Byte Productions on YouTube

Francis Stokes francisrstokes

🎥
Low Byte Productions on YouTube
View GitHub Profile
@francisrstokes
francisrstokes / length.js
Created April 16, 2018 06:49
Fluent API for performing length conversions
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) => {
@francisrstokes
francisrstokes / promise-cache.js
Created April 18, 2018 09:09
Caching Promises in a 7 line pure function
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()));
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);
@francisrstokes
francisrstokes / Either.js
Last active October 18, 2018 14:30
Monads
const daggy = require('daggy');
const {identity} = require('ramda');
const Either = daggy.taggedSum('Either', {
Left: ['__value'],
Right: ['__value'],
});
Either.of = Either.Right;
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
@francisrstokes
francisrstokes / CSVParser.js
Last active December 27, 2018 10:05
Arcsecond Article
const {
parse,
char,
many,
regex,
anythingExcept,
sepBy
} = require('arcsecond');
const joinedMany = parser => many (parser) .map(x => x.join(''));
@francisrstokes
francisrstokes / StateDispatcher.js
Last active May 28, 2019 02:31
Redux without redux - sharing state and one way data flow using only standard react
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) {

Keybase proof

I hereby claim:

  • I am francisrstokes on github.
  • I am francisstokes (https://keybase.io/francisstokes) on keybase.
  • I have a public key ASDAQWguSwFKdWeKeEvEeTyzezi0na5Pmhdva4wN2Cwj4wo

To claim this, I am signing this object:

@francisrstokes
francisrstokes / AsyncAwaitGenerators.js
Created December 28, 2018 23:50
Async/Await in 5 lines
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 => {
@francisrstokes
francisrstokes / PipeDriveForm.js
Created July 12, 2019 08:47
PipeDrive Web Forms React Component
import React from 'react';
class PipeDriveForm extends React.Component {
constructor(props) {
super(props);
this.state = {
randomId: 'id' + Math.random().toString(36).substring(7)
};
}