Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
quaquaquaqua

buzzdecafe

💭
quaquaquaqua
View GitHub Profile
View glz.js
const NIL = Symbol('~~NIL~~');
const isNil = x => typeof x === 'symbol' && x.toString() === NIL.toString();
const pair = (a, b) => ({
fst: function* () { yield a; },
snd: function* () { yield b; },
[Symbol.iterator]: function* () { yield a; (b[Symbol.iterator] ? yield* b : yield b); }
});
View store-comonad.js
/*
data Store s a = Store (s -> a) s
instance Functor (Store s) where
fmap f (Store g s) = Store (f . g) s
instance Extend (Store s) where
duplicate (Store f s) = Store (Store f) s
instance Comonad (Store s) where
View s2.js
const S = {
fromArray: xs => sink => {
for (let i=0; i<xs.length; ++i)
if (sink(xs[i]))
return true
},
map: fn => stream => sink => stream(x => sink(fn(x))),
@buzzdecafe
buzzdecafe / curry.js
Last active Nov 19, 2017
decorated curry
View curry.js
const meta = Symbol('@@ramda-meta');
const initMeta = f => ({ func: f, arity: f.length, seenArgs: [] });
const getMeta = f => f[meta] || initMeta(f);
const setMeta = (arg, f) => {
const fMeta = getMeta(f);
f[meta] = {
func: fMeta.func,
@buzzdecafe
buzzdecafe / _stream.js
Last active Apr 23, 2020
Union types are beautiful
View _stream.js
import Type from 'union-type';
// `tail` function must be return a Stream type, but no way to enforce that without evaluating it :-(
const Stream = Type({
Empty: [],
Cons: [() => true, Function]
});
Stream.prototype.chain = function(f) {
@buzzdecafe
buzzdecafe / Lazy.js
Created May 27, 2017 — forked from i-am-tom/Lazy.js
A Fantasy Land-compliant type for lazy computation.
View Lazy.js
const fl = require('fantasy-land')
//- Lazy holds a vaue in a thunk, effectively delaying
//- evaluation until required. This is useful when we're
//- in a situation with either very large data set or
//- cyclical data.
//@ make stack-safe.
//> type Lazy a = Unit -> a
function Lazy(run) {
@buzzdecafe
buzzdecafe / ct_notes.txt
Last active Jan 24, 2022
Category Theory for Programmers: Notes
View ct_notes.txt
CATEGORY THEORY FOR PROGRAMMERS
Category Theory 1.1: Motivation and Philosophy
https://www.youtube.com/watch?v=I8LbkfSSR58&index=1&list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_
Composability, Abstraction, Reusability
Category Theory is a higher-level language.
Not a practical, executable language.
View klotski.ml
(*
#1
loop : ('a -> bool) -> ('a -> 'a) -> 'a -> 'a
such that loop p f x = x when p x = true and loop p f x = loop p f (f x) otherwise.
*)
let rec loop p f x = if p x then x else loop p f (f x);;
(*
#2
exists : ('a -> bool) -> 'a list -> bool
@buzzdecafe
buzzdecafe / compose.js
Last active Mar 27, 2017
curried binary compose
View compose.js
// first try:
R.cc = R.curry(function cc(f, g) {
return R.curryN(g.length, function() {
var gargs = Array.prototype.slice.call(arguments, 0, g.length);
var fargs = Array.prototype.slice.call(arguments, g.length);
return f.apply(this, [g.apply(this, gargs)].concat(fargs));
});
});
// so this works:
View fizzbuzz.js
// fizzbuzz ramda-style
var R = require('ramda');
R.forEach(R.cond(
[R.pipe(R.modulo(R.__, 15), R.eq(0)), function() { console.log('FizzBuzz'); }],
[R.pipe(R.modulo(R.__, 3), R.eq(0)), function() { console.log('Fizz'); }],
[R.pipe(R.modulo(R.__, 5), R.eq(0)), function() { console.log('Buzz'); }],
[R.alwaysTrue, function(){}]
), R.range(1, 101));