Skip to content

Instantly share code, notes, and snippets.

View djfm's full-sized avatar
💭
Freelance. Won't set foot in office again.

François-Marie de Jouvencel djfm

💭
Freelance. Won't set foot in office again.
View GitHub Profile
const scale = {
c: 0,
'c#': 1,
d: 2,
'd#': 3,
e: 4,
f: 5,
'f#': 6,
g: 7,
'g#': 8,
const {
Cluster,
N1qlQuery,
MutationState,
} = require('couchbase');
const URL = require('url');
const addQueryParams = params => url => {
const parsed = URL.parse(url);
const just = x => Promise.resolve(x);
const nothing = () => Promise.reject();
chain(
() => just(1),
() => nothing(),
(_, x, y) => just(x / y)
)()
.should.be.rejected;
@djfm
djfm / chain-do.js
Last active September 23, 2016 13:24
const chainStep = fn => (argSource, history) => {
if (argSource && (typeof argSource === 'object')) {
const then = argSource.then;
if (typeof then === 'function') {
return Promise.all(history).then(
settledHistory =>
then.call(argSource, arg => fn(arg, ...settledHistory))
);
}
}
do
a <- makeId "dress"
b <- makeId "dress"
c <- makeId "dress"
return checkOrdered a b c
storage.makeId('dress')
.then(
a =>
storage.makeId('dress')
.then(
b =>
storage.makeId('dress')
.then(
c => {
a.should.be.lessThan(b);
chain(
() => storage.makeId('dress'),
() => storage.makeId('dress'),
() => storage.makeId('dress'),
(_, a, b, c) => {
a.should.be.lessThan(b);
a.should.be.lessThan(c);
b.should.be.lessThan(c);
}
)()
const chain = (...fns) =>
initialValue => fns.reduce(
(result, fn) => {
if (result && (typeof result === 'object')) {
const then = result.then;
if (typeof then === 'function') {
return then.call(result, fn);
}
}
return fn(result);
const chain = (...fns) => initialValue =>
fns.reduce((previousOutput, fn) => fn(previousOutput), initialValue);
chain(x => x + 1, x => x / 2)(0).should.equal(0.5);
chain(x => x / 2, x => x + 1)(0).should.equal(1);
// run it: https://jsfiddle.net/s8hwae47/
@djfm
djfm / monad.js
Created July 25, 2016 11:25
incomplete but fun monad implementation attempt
const chai = require('chai');
chai.should();
const listMonad = {
// make :: a -> m a
// make :: a -> [a]
make: (...args) => args,
// bind :: m a -> (a -> m b) -> m b
// bind :: [a] -> (a -> [b]) -> [b]
bind: someList => listReturningFunction =>