Skip to content

Instantly share code, notes, and snippets.

View buzzdecafe's full-sized avatar
💭
quaquaquaqua

buzzdecafe

💭
quaquaquaqua
View GitHub Profile
@buzzdecafe
buzzdecafe / S.js
Last active April 18, 2024 11:55
S Combinator
/*
S : \x y z -> x z (y z)
*/
// S :: (z -> (a -> b)) -> (z -> a) -> z -> b
function S(x, y, z) {
return x(z)(y(z));
}
// example:
// add :: a -> a -> a
@buzzdecafe
buzzdecafe / Functor.js
Last active October 18, 2023 07:28 — forked from CrossEye/Functor.js
(function(global) {
global.Functor = function(conf) {
Functor.types[conf.key] = {
obj: conf.obj,
fmap: conf.fmap
};
};
Functor.types = {};
@buzzdecafe
buzzdecafe / recurly.sh
Last active December 15, 2022 15:26
cURL recurly API
# recurly does not document using cURL to access their API, they really want you to use their libraries.
# Sometimes i need to test something quick. I do not want to write a script importing your stupid library.
# So here's how you do it:
APIKEY=<get your API key from your recurly.com site>
curl -H "Accept: application/vnd.recurly.v2021-02-25+json" -u $APIKEY: https://v3.recurly.com/accounts
@buzzdecafe
buzzdecafe / loop-recur.js
Created November 4, 2022 19:29
Scott's loop/recur impl
const Loop = (fn, ... init) => {
let r = fn (... init)
while (r instanceof Recur) r = fn (... r)
return r
}
const Recur = ( ...v) => Object .create (
Recur .prototype,
{[Symbol .iterator]: {value: _ => v .values ()}}
)
@buzzdecafe
buzzdecafe / ct_notes.txt
Last active January 24, 2022 12:17
Category Theory for Programmers: Notes
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.
@buzzdecafe
buzzdecafe / cons, car, cdr
Last active November 17, 2021 10:11
cons car and cdr implemented as javascript functions. cribbed from SICP http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_thm_2.4then implemented a bunch of functions just for fun.
// clean and pure:
function cons(x, y) {
return function(pick) {
return pick(x, y);
}
}
// does more stuff:
function cons(x, y) {
var fn = function(pick) {
@buzzdecafe
buzzdecafe / _stream.js
Last active April 23, 2020 16:44
Union types are beautiful
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) {
(*
#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
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); }
});
/*
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