Skip to content

Instantly share code, notes, and snippets.

View DrBoolean's full-sized avatar

Brian Lonsdorf DrBoolean

View GitHub Profile
const Components = [‘alert’, ‘buttons’, ‘data-tables’, /* …etc */]
// build a Map of just {key: key}
const empty = Components.reduce((acc, key) => acc.set(key, key), Immutable.Map())
// load up each component
const ui = empty.map(loadComponent).map(addExample).map(beautifyHtml).map(addSelectorTable)
// access a component
ui.get(‘buttons’) // { "name": "Buttons", "example": Maybe.Just(Task), "html": Maybe.Just(<some html>), "selectors": Maybe.Just(cols,rows) }
const UI = Immutable.Map({
 ‘alerts’: {/* lots of stuff */},
 ‘buttons’:{/* lots of stuff */},
 ‘data-tables’: {/* lots of stuff */}
 // lots more components
})
@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 / extend.js
Last active February 16, 2017 22:14
Sum Product extend example
const {Tuple} = require('fantasy-tuples')
const Either = require('data.either') // missing extend, but could pr
Tuple(current_user, age)
.map(age => age + 1)
.extend(({_1: user, _2: age}) =>
Object.assign({}, user, {age}))
// Tuple(current_user, current_user_with_age)
// ^ notice this only ever changes the _2, but you can see _1 while you work.
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 / reports.js
Created August 23, 2016 16:21
Curry use
// to, from are awkward here. generateReport only needs them to send an email
const generateReport = (to, from, data) =>
mungeData(data, (warnings, munged) =>
renderReport(munged, (report) => emailReport(to, from, report)))
generateReport(“info@reports.com”, “app@test.net”, {some: ‘data’})
// if emailReport was curried, we could pass in the emailer
@DrBoolean
DrBoolean / step3.js
Last active August 5, 2016 14:42
step3.js
function add1(v) { return v + 1; }
function isOdd(v) { return v % 2 == 1; }
function sum(total,v) { return total + v; }
function listReduction(list,v) {
list.push(v);
return list;
}
function mapReducer(fn) {
@DrBoolean
DrBoolean / task_sketch9.js
Created May 31, 2016 14:44
task_sketch9.js
{
"Band": {
"dino_jr": {
"id": 0.5933238949411739,
"name": "Dinosaur Jr",
"members": 3
},
"amanset": {
"id": 0.9872892555359196,
"name": "American Analog Set",
Map({a: 1, b: 2}).map(v => Id.of(v + 1))
// Map { "a": Id(2), "b": Id(3) }
@DrBoolean
DrBoolean / task_sketch8.js
Last active May 30, 2016 16:03
task_sketch8.js
const createRecord = tablename => attrs =>
Id.of(Object.assign({id: Math.random(2)}, attrs))
const makeFixtures = defs =>
Map(defs)
.traverse(Id.of, (instances, tablename) =>
Map(instances).traverse(Id.of, createRecord(tablename)))