Skip to content

Instantly share code, notes, and snippets.

View mvaldesdeleon's full-sized avatar

Martín Valdés de León mvaldesdeleon

View GitHub Profile
function storify(target = {}) {
const observers = {},
observables = {};
return new Proxy(target, {
get: function(target, property, receiver) {
if (property in observables === false) {
observables[property] = Rx.Observable.create(observer => {
observers[property] = observer;
@mvaldesdeleon
mvaldesdeleon / liftP.js
Created August 8, 2017 13:52
Quilombo Driven Development
const wait = n => new Promise(res => setTimeout(res, n));
const log = console.log.bind(console);
const dummy = payload => wait(1000).then(() => payload);
const user = () => (log('user'), dummy({user: 'john'}));
const tweets = () => (log('tweets'), dummy({tweets: ['asdasd', 'dsada']}));
const github = () => (log('github'), dummy({repos: ['asdasd', 'dsada']}));
// buildResult :: (user, tweets, github) -> {user, tweets, github}
@mvaldesdeleon
mvaldesdeleon / Editor.hs
Created August 18, 2017 11:27
Fixed `Editor.hs` file from CIS 194: Homework 7 (Spring '13)
{-# LANGUAGE GeneralizedNewtypeDeriving
, ScopedTypeVariables
#-}
module Editor where
import System.IO
import Buffer
import Control.Exception
@mvaldesdeleon
mvaldesdeleon / reader.js
Created November 12, 2017 23:16
Reader monad
function Reader(x) {
this.x = x;
}
Reader.prototype.run = function(env) {
return this.x(env);
}
Reader.of = function(x) {
return new Reader(() => x);
@mvaldesdeleon
mvaldesdeleon / state.js
Created November 20, 2017 19:49
State monad
function State(fn) {
this._fn = fn;
}
State.state = fn => new State(fn);
State.of = val => State.state(state => [val, state]);
State.get = State.state(state => [state, state]);
@mvaldesdeleon
mvaldesdeleon / transducers.js
Last active November 29, 2017 17:09
Transducers
const reduce = reducer => z => as => as.reduce((z, a) => reducer(z)(a), z);
// reducer :: (z -> a -> z)
// transducer :: (z -> a -> z) -> (z -> b -> z)
// contramapT :: (b -> a) -> (z -> a -> z) -> (z -> b -> z)
const contramapT = fn => reducer => (z => b => reducer(z)(fn(b)));
// filterT :: (a -> Bool) -> (z -> a -> z) -> (z -> a -> z)
@mvaldesdeleon
mvaldesdeleon / stub.js
Last active December 1, 2017 23:01
Sugerencia meetupjs
// newtype Point = {x :: Number, y :: Number}
// newtype EnhancedPoint = {x :: Number, y :: Number, visible :: Boolean, distance :: Number}
/* Library functions */
// getCenter :: Point => Point => Point
const getCenter =
({x: x1, y: y1}) =>
({x: x2, y: y2}) =>
({x: 0.5*(x1+x2), y: 0.5*(y1+y2)})
@mvaldesdeleon
mvaldesdeleon / concurrently.js
Created January 16, 2018 15:04
Run promises concurrently, wait until they are all settled
const runConcurrently = concurrency => factories => {
const queue = [];
const lock = () => new Promise(resolve => {
if (concurrency > 0) {
concurrency--
resolve()
} else {
queue.push(resolve)
}
@mvaldesdeleon
mvaldesdeleon / deep.js
Last active February 8, 2018 21:59
Conditional deep mapping & deep resolving of Objects.
const isObject =
obj =>
obj instanceof Object;
const isPromise =
obj =>
typeof obj.then === 'function';
const mapDeepIf =
(pr, fn, obj) =>
class Estructura {
constructor(datos) {
this.datos = [-Infinity, -Infinity];
}
insertar(dato) {
this.datos[0] = this.datos[0] < dato ? dato : this.datos[0];
this.datos = sortPair(this.datos);
return this;