Skip to content

Instantly share code, notes, and snippets.

💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
Block or report user

Report or block ericelliott

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ericelliott
ericelliott / promise-monad-proof.js
Created Jun 28, 2017 — forked from briancavalier/promise-monad-proof.js
A proof that Promises/A is a Monad
View promise-monad-proof.js
//-------------------------------------------------------------
//
// Hypothesis:
//
// Promises/A is a Monad
//
// To be a Monad, it must provide at least:
// - A unit (aka return or mreturn) operation that creates a corresponding
// monadic value from a non-monadic value.
// - A bind operation that applies a function to a monadic value
@ericelliott
ericelliott / class-inheritance-demo-2.js
Last active Sep 2, 2019
Class inheritance demo 2 (not recommended)
View class-inheritance-demo-2.js
class Teacher extends User {
constructor (options) {
super(options);
this.lessons = [];
}
createLesson (lesson) {
const { name } = lesson;
this.lessons.push(lesson);
console.log(`${ this.name } created lesson: ${ name }`);
}
@ericelliott
ericelliott / class-inheritance-demo.js
Last active Jan 30, 2017
Class inheritance demo (not recommended)
View class-inheritance-demo.js
class User {
constructor ({ name = 'Anonymous' }) {
this.name = name;
}
login () {
console.log(`${ this.name } logged in.`);
}
};
class Student extends User {
@ericelliott
ericelliott / filter.js
Last active Oct 8, 2019
Filter implemented with reduce
View filter.js
const filter = (fn, arr) => arr.reduce((newArr, item) => {
return fn(item) ? newArr.concat([item]) : newArr;
}, []);
View autocurry.js
const curry = fn => (...args1) => {
if (args1.length === fn.length) {
return fn(...args1);
}
return (...args2) => {
const args = [...args1, ...args2];
if (args.length >= fn.length) {
return fn(...args);
View wait-speculation.js
import speculation from 'speculation';
const wait = (
time,
cancel = Promise.reject() // By default, don't cancel
) => speculation((resolve, reject, onCancel) => {
const timer = setTimeout(resolve, time);
// Use onCancel to clean up any lingering resources
// and then call reject(). You can pass a custom reason.
@ericelliott
ericelliott / speculation.js
Created Jan 23, 2017
Speculation: Cancellable promise implementation. Maintained production version here: https://github.com/ericelliott/speculation
View speculation.js
// HOF Wraps the native Promise API
// to add take a shouldCancel promise and add
// an onCancel() callback.
const speculation = (
fn,
cancel = Promise.reject() // Don't cancel by default
) => new Promise((resolve, reject) => {
const noop = () => {};
const onCancel = (
@ericelliott
ericelliott / promise-chaining.js
Last active Oct 8, 2019
Promise chaining behaviors
View promise-chaining.js
const wait = time => new Promise(
res => setTimeout(() => res(), time)
);
wait(200)
// onFulfilled() can return a new promise, `x`
.then(() => new Promise(res => res('foo')))
// the next promise will assume the state of `x`
.then(a => a)
// Above we returned the unwrapped value of `x`
@ericelliott
ericelliott / cancellable-wait.js
Last active Oct 8, 2019
Cancellable wait -- an ES6 promise example
View cancellable-wait.js
const wait = (
time,
cancel = Promise.reject()
) => new Promise((resolve, reject) => {
const timer = setTimeout(resolve, time);
const noop = () => {};
cancel.then(() => {
clearTimeout(timer);
reject(new Error('Cancelled'));
@ericelliott
ericelliott / wait.js
Created Jan 19, 2017
Wait -- an ES6 promise example
View wait.js
const wait = time => new Promise((resolve) => setTimeout(resolve, time));
wait(3000).then(() => console.log('Hello!')); // 'Hello!'
You can’t perform that action at this time.