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
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 / 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()));
@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 / 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;