Skip to content

Instantly share code, notes, and snippets.

View DrBoolean's full-sized avatar

Brian Lonsdorf DrBoolean

View GitHub Profile
@DrBoolean
DrBoolean / task_sketch7.js
Created May 29, 2016 20:20
task_sketch7.js
const associateRels = fixs => attrs =>
attrs.traverse(Id.of, (attr, name) =>
name.match(/_id$/)
? Id.of(attr.split('.').reduce((acc, p) => acc.get(p), fixs))
: Id.of(attr))
const createRecord = tablename => attrs =>
Id.of(Object.assign({id: Math.random(2)}, attrs))
@DrBoolean
DrBoolean / task_sketch7.js
Last active May 31, 2016 14:40
task_sketch7.js
// record is used in place of the actual db row
Id({
Band: {
dino_jr: record,
amanset: record
},
Event: {
dino_stubbs: record
},
Venue: {
@DrBoolean
DrBoolean / task_sketch6.js
Created May 29, 2016 16:43
task_sketch6.js
makeFixtures(defs)
.fork(console.error, console.log)
// {
// "Band": {
// "dino_jr": {
// "id": 0.5556986515877327,
// "name": "Dinosaur Jr",
// "members": 3
// },
// "amanset": {
@DrBoolean
DrBoolean / task_sketch5.js
Last active May 29, 2016 20:21
task_sketch5.js
const makeFixtures = defs =>
Map(defs)
.traverse(Id.of, (instances, tablename) =>
Map(instances).traverse(Id.of, createRecord(tablename)))
.chain(fixs =>
fixs.traverse(Id.of, (instances) =>
Map(instances).traverse(Id.of, associateRels(fixs))))
@DrBoolean
DrBoolean / task_sketch4.js
Last active May 29, 2016 20:07
task_sketch4.js
Map({a: {one: 1}, b: {two: 2}}).traverse(Id.of, (inner, k) =>
Map(inner).traverse(Id.of, v => Id.of(v + 1)))
// Id (Map { "a": Map { "one": 2 }, "b": Map { "two": 3 } })
@DrBoolean
DrBoolean / task_sketch3.js
Last active May 29, 2016 20:06
task_sketch3.js
Map({a: 1, b: 2}).traverse(Id.of, v => Id.of(v + 1))
// Id (Map { "a": 2, "b": 3 })
@DrBoolean
DrBoolean / task_sketch2.js
Last active June 1, 2016 16:11
task_sketch2.js
makeFixtures(defs).map(fixtures => {
// Get the id of the created model
fixtures.Band.amanset.id
// 3
// Get a relationship
fixtures.Event.dino_stubbs.venue.name
// Stubbs
})
const defs = {
Band: {
dino_jr: {
name: 'Dinosaur Jr',
members: 3,
},
amanset: {
name: 'American Analog Set',
members: 7
}
@DrBoolean
DrBoolean / SumProductList.js
Last active March 27, 2018 20:54
Either and Tuple (the canonical Sum/Product types, respectively) used to make a List
const {Left, Right} = require('data.either')
const Tuple = require('fantasy-tuples').Tuple2
//List :: Either Null (Tuple a List)
const empty = () =>
Left(null)
const cons = (x, l) =>
Right(Tuple(x, l))
@DrBoolean
DrBoolean / free-er2.js
Created February 27, 2016 16:38
Free(er) Monads Pt2
const daggy = require('daggy')
const Task = require('data.task')
const _ = require('lodash')
const kleisli_comp = (f, g) => x => f(x).chain(g)
const compose = (f, g) => x => f(g(x))
const id = x => x
//=============FREE=============
const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']})
const {Impure, Pure} = Free