Skip to content

Instantly share code, notes, and snippets.

View jayrbolton's full-sized avatar
🍕

Jay R Bolton jayrbolton

🍕
View GitHub Profile
const R = require('ramda')
const stream = require('../stream')
const h = require('../html')
const multiCounters = initial => actions =>
stream.scanMerge([
[actions.addCounter, R.append(0)]
, [actions.remCounter, R.init]
, [actions.increment, (counters, idx) => R.update(idx, counters[idx] + 1, counters)]
, [actions.decrement, (counters, idx) => R.update(idx, counters[idx] - 1, counters)]
interface Functor (F) {
map: (a => b) => F(a) => F(b)
}
interface Applicative(M) > Monad (M) {
andThen: M(a) => (a => M(b)) => M(b)
}
type Maybe (a) {
function model({ fahrenInput, celsiusInput }) {
const fahrenChange = fahrenInput.map((ev) => ev.currentTarget.value);
const celsiusChange = celsiusInput.map((ev) => ev.currentTarget.value);
const fahrenToCelsius =
fahrenChange.map(parseFloat).filter((n) => !isNaN(n)).map((f) => (f - 32) / 1.8);
const celsiusToFahren =
celsiusChange.map(parseFloat).filter((n) => !isNaN(n)).map((c) => c * 9 / 5 + 32);
const celsius = stepper(0, combine(celsiusChange, fahrenToCelsius));
const fahren = stepper(0, combine(fahrenChange, celsiusToFahren));
return Now.of([{ fahren, celsius }, []]);
@jayrbolton
jayrbolton / callb-heaven.js
Last active April 10, 2017 20:00
callback helpers library sketch
// With asynchronous functions that take callbacks, it'd be easy to compose them if they were all curried
// This could create an interface very similar to Promises, but with plain, easy-to-use callback functions
const ch = require('callback-heaven')
const fs = require('fs')
// Contrived example function to open a json file, merge the data with an object, write the result back to the file, and read the file again
// (of course you could also just use writeFileSync and readFileSync, but this is just an example)
function mergeJson(path, obj, cb) {
fs.readFile(path, function(err, data) {
@jayrbolton
jayrbolton / snabbdom-transformer.js
Last active February 3, 2017 16:59
Snabbdom transformer idea
// this is how you could create composable functions that modify and extend VNodes
// - append or prepend children
// - add surrounding VNode wrappers
// - compose hook and event functions
// - merge props, attrs, style, and class
// snabbdom-transform objects can have these properties
// - wrapper: function that takes the vnode and allows you to add surrounding elements
// - class, style, and attrs: these will merge with existing data using R.merge
// - on, hook: these functions will merge and get composed with existing on/hook functions without overwriting any
@jayrbolton
jayrbolton / temperature-converter.js
Last active September 13, 2016 09:49
flyd + snabbdom component example (fahrenheit/celsius converter example)
import flyd from 'flyd'
import h from 'snabbdom/h'
import snabbdom from 'snabbdom'
import render from 'ff-core/render'
// Initialize the state object
const init = ()=> {
// Initialize input change event streams
// Think of streams as values that change over time.
// These two streams are input values that change over time, but start empty.
// project euler #9
// jay r bolton 1/5/2016
//
//
// A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
//
// a2 + b2 = c2
// For example, 32 + 42 = 9 + 16 = 25 = 52.
//
// There exists exactly one Pythagorean triplet for which a + b + c = 1000.
// project euler 8
// jay r bolton 12/26
//
// The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
//
// 73167176531330624919225119674426574742355349194934
// 96983520312774506326239578318016984801869478851843
// 85861560789112949495459501737958331952853208805511
// 12540698747158523863050715693290963295227443043557
// 66896648950445244523161731856403098711121722383113
// project euler #7
//
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
//
// What is the 10,001st prime number?
// Approach I came up with after a while:
// - I remembered Sieve of Eratosthenes but couldn't figure out the exact idea without lazy lists
// - So instead I just keep track of all the previous primes and test the modulo on each prime in every loop
// - Only iterate on odd numbers, easy elimination of half the set
@jayrbolton
jayrbolton / virtual-dom-convenience-wrapper.js
Last active September 8, 2015 06:27
Virtual-dom quick setup wrapper
// This is a minimalistic way to easily use virtual-dom and avoid repeating any boilerplate code
// It's a simple object wrapper with a small api that you can use to set new data and re-render quickly and easily.
// Pass in a parentNode (like document.body), a rootComponent (a function that takes a state and returns a VTree), and an initial state object.
function view(parentNode, rootComponent, initialState) {
this.state = initialState
this.tree = rootComponent(initialState)
this.rootNode = createElement(this.tree)
parentNode.appendChild(this.rootNode)
return this