Skip to content

Instantly share code, notes, and snippets.

View DrBoolean's full-sized avatar

Brian Lonsdorf DrBoolean

View GitHub Profile
@DrBoolean
DrBoolean / free-er.js
Last active March 17, 2024 10:33
Free(er) monads in JS (pt 1)
const daggy = require('daggy')
const compose = (f, g) => x => f(g(x))
const id = x => x
const kleisli_comp = (f, g) => x => f(x).chain(g)
//=============FREE=============
const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']})
const {Impure, Pure} = Free
@DrBoolean
DrBoolean / mcft.js
Created December 30, 2015 04:44
Monoidal Contravariant Functors and Transducers
const daggy = require('daggy');
const {foldMap} = require('pointfree-fantasy')
const {concat, toUpper, prop, identity, range, compose} = require('ramda');
// Contravariant functors usually have this shape F(a -> ConcreteType).
// In other words, some type holding a function which is parametric on its input, but not output.
// They don't always have that shape, but it's a good intuition
// Covariant functors are what we're used to, which are parametric in their output
//================================================================
@DrBoolean
DrBoolean / coyo_uses.js
Created February 26, 2016 15:40
Coyoneda Uses in JS
const daggy = require('daggy')
const compose = (f, g) => x => f(g(x))
const id = x => x
//===============Define Coyoneda=========
const Coyoneda = daggy.tagged('x', 'f')
Coyoneda.prototype.map = function(f) {
return Coyoneda(this.x, compose(f, this.f))
}
@DrBoolean
DrBoolean / falg.js
Created December 15, 2015 18:19
F-algebra es2015
const daggy = require('daggy')
const {compose, curry, map, prop, identity, intersection, union} = require('ramda');
const inspect = (x) => { if(!x) return x; return x.inspect ? x.inspect() : x; }
// F-algebras
// Fix
// ============
// Fx is a simple wrapper that does almost nothing. It's much more useful in typed languages to check that we have a recursive f (Fix f)
@DrBoolean
DrBoolean / three_envs.js
Last active May 24, 2021 02:31
Tail of three envs
const compose = (f, g) => x => f(g(x))
const Id = x =>
({
fold: f => f(x),
map: f => Id(f(x))
})
Id.of = Id
const Tuple = (_1, _2) =>
@DrBoolean
DrBoolean / representable.js
Last active December 24, 2020 00:15
Representable Functors
const Immutable = require('immutable-ext')
const {Just, Nothing} = require('data.maybe')
const Task = require('data.task')
// Setup
const Reader = f =>
({
run: f,
map: g => Reader(x => g(f(x))),
@DrBoolean
DrBoolean / binary_baby_transformer.js
Last active December 1, 2020 03:12
StateT is useful
import {StateT} from 'fantasy-states'
import Task from 'data.task'
import {prop, compose, map, chain, merge, always} from 'ramda'
// Unfortunately Binary Gendered Baby Page
//==========================================
// data App a b c = App State a (Task b c)
const App = StateT(Task)
const {get, put, modify} = App;
@DrBoolean
DrBoolean / fp_arch.js
Last active October 27, 2020 09:57
OO => FP architecture refactor
// Simple example, but the idea holds for more complex objects.
/* 1) Start with OO */
// user.js
class User {
constructor(firstName, lastName, email) {
this.firstName = firstName
this.lastName = lastName
const {Map} = require('immutable-ext')
const Id = require('fantasy-identities')
const Task = require('data.task')
const defs = {
Band: {
dino_jr: {
name: 'Dinosaur Jr',
members: 3,
},
@DrBoolean
DrBoolean / comonads-are-objects.js
Created December 23, 2015 19:55
Comonads are objects js style.
// http://www.haskellforall.com/2013/02/you-could-have-invented-comonads.html
const daggy = require('daggy');
const {toUpper, prop, identity, range, curry, compose} = require('ramda');
const Config = daggy.tagged("opts")
Config.prototype.inspect = function() {
return `Config(${this.opts})`
}