Skip to content

Instantly share code, notes, and snippets.

Avatar

David Chambers davidchambers

View GitHub Profile
View composable-callbacks.js
const after = t => x => cont => {
const id = setTimeout (cont, t, x)
return () => clearTimeout (id)
}
const map = f => run => cont => run(x => cont (f (x)))
const chain = f => run => cont => run(x => f (x) (cont))
const run = chain (x => after (2000) (`${x}C`))
(map (x => `${x}B`)
@Avaq
Avaq / dangerous-promises.js
Last active Oct 12, 2021
Dangerous Promises
View dangerous-promises.js
//
// utils
//
const thrush = x => f => f(x);
const indexBy = k => xs => xs.reduce((index, x) => {
if(index[x[k]] != null) index[x[k]].push(x);
else index[x[k]] = [x];
return index;
@safareli
safareli / Array.chainRec.js
Last active Dec 27, 2016
Implementation of ChainRec from Fantasy Land specification for Array
View Array.chainRec.js
var flatten = Function.prototype.apply.bind([].concat, [])
Array.prototype.chain = function(f) {
return flatten(this.map(f))
}
var stepNext = function (x) { return {value: x, done: false }; };
var stepDone = function (x) { return {value: x, done: true }; };
Array.chainRec = function _chainRec(f, i) {
var todo = [i];
var res = [];
View dropWhereRepeated.hs
--dropWhereRepeated [1, 2, 2, 3, 3, 5, 7, 10]
--[1,5,7,10]
dropWhereRepeated :: (Eq a) => [a] -> [a]
dropWhereRepeated [] = []
dropWhereRepeated [x] = [x]
dropWhereRepeated (x : y : xs)
| x == y = dropWhereRepeated (dropWhile (== x) (xs))
| otherwise = x : dropWhereRepeated (y : xs)
@Avaq
Avaq / placeholder.md
Last active Apr 15, 2019
Exploring an alternative placeholder implementation
View placeholder.md

I published a library which implements this idea; furry.

# From To Normal Alternative
1 a → b → c a → c f(_, b)(a) f(_, b)(a)
2 a → b → c b → a → c nope f(_)(b, a)
3 a → b → c → d b → d f(a, _, c)(b) f(a, _, c)(b)
4 a → b → c → d a → b → d f(_, _, c)(a, b) f(_, _, c)(a, b)
5 a → b → c → d a → c → d f(_, b)(a, c) f(_, b, _)(a, c)
@Avaq
Avaq / combinators.js
Last active Sep 23, 2022
Common combinators in JavaScript
View combinators.js
const I = x => x
const K = x => y => x
const A = f => x => f (x)
const T = x => f => f (x)
const W = f => x => f (x) (x)
const C = f => y => x => f (x) (y)
const B = f => g => x => f (g (x))
const S = f => g => x => f (x) (g (x))
const S_ = f => g => x => f (g (x)) (x)
const S2 = f => g => h => x => f (g (x)) (h (x))
View index.js
const { Future, IO } = require('ramda-fantasy');
const join2 = curryN(2, require('path').join);
const npa = require('npm-package-arg');
const ALIAS = /:([^!]+)/;
const EXTEND = /!$/;
const parseAlias = pipe(
S.match(ALIAS), chain(nth(1)));
const parseExtend = pipe(
S.match(EXTEND), map(T));
@jethrolarson
jethrolarson / _usage.js
Last active Apr 25, 2016
xface - Functional Interface library for typed common apis
View _usage.js
//Some fantasy implementations
const Just = require('./just')
const Cant = require('./cant')
const List = require('./list')
//Spec file that defines what arguments are used for type identification
const fantasy = require('./fantasy')
//The magic.
const {chain, map, index} = require('../../src/xface')(fantasy, [Just, Cant])
@jethrolarson
jethrolarson / exampleUsage.js
Last active Jun 26, 2016
AGeneric interface fantasy-land objects
View exampleUsage.js
var Maybe = require('./maybe.js')
var List = require('./list.js')
//Pass fantasy implementations to it
var {concat, map} = require('fantasy')({Maybe, List})
mapDouble = map(a => 2 * a)
mapDouble([1, 2])
//[2, 4]
@tel
tel / ProfunctorLens.js
Last active Feb 4, 2021
Pure Profunctor Lenses in Javascript (redux)
View ProfunctorLens.js
/* eslint-disable new-cap */
/**
* Lens types.
* ===========
*
* a * b = {fst: a, snd: b}
* a + b = {index: Boolean, value: a | b}
*
* Iso s t a b = forall (~>) . Profunctor (~>) => (a ~> b) -> (s ~> t)