This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
So, let's play with some Semigroups and (maybe?) Monoids for combining lists in interesting ways | |
*/ | |
//This one is pretty straightforward | |
//Union (keep all values from both lists, but no repeats) | |
const Union = function(xs){ | |
if (!(this instanceof Union)) { | |
return new Union(xs); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's make it possible to create pure functions even when we're | |
// dealing with impure operations that would have side effects! | |
// First we'll need a "Type" that can contain a (sometimes impure) function | |
function IO(fn) { | |
if (!(this instanceof IO)) {//make it simpler for end users to create a type without "new" | |
return new IO(fn); | |
} | |
this.runIO = fn;//IO now provides an extra control layer that allows the composition of unexecuted effects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Native Arrays are not great structures for cellular automata. | |
Non-empty, circular, doubly-linked lists would be ideal... | |
but all we really need to do is write a comonadic-like interface | |
such that it _pretends_ that the array is circular, and can thus | |
pass the exfn below a sort of "local" slice of an Array as if it were circular. | |
So you can mostly ignore the implementation mess below | |
*/ | |
Array.prototype.extendNear = function(exfn){ | |
const len = this.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's make it possible to create pure functions even when we're | |
// dealing with impure operations that would have side effects! | |
// First we'll need a "Type" that can contain a (sometimes impure) function | |
function IO(fn) { | |
if (!(this instanceof IO)) {//make it simpler for end users to create a type without "new" | |
return new IO(fn); | |
} | |
this.runIO = fn;//IO now provides an extra control layer that allows the composition of unexecuted effects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const IO = fn =>({ | |
runIO: fn,//run the effect | |
map: f => IO( a => f(fn(a)) ),//transform the inner value | |
chain: f => IO( _ => f(fn()).runIO() ),//transform the inner value into another pure operation | |
fork: _ => IO(x => new Promise(r => window.setTimeout(_ => r(fn(x)), 0) ))//fork the process! | |
}); | |
IO.of = x => IO(_ => x); | |
IO.$ = selectors => IO(_=>Array.from(document.querySelectorAll(selectors))); | |
//Examples: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Nothing = new (class Nothing { | |
constructor() {} | |
ap() { return this; } | |
map() { return this; } | |
chain() { return this; } | |
reduce(f, x){ return x; } | |
getOrElse(x){ return x; } | |
concat(x){ return x; } | |
cata({Nothing}){ return Nothing(); }//this is NOT the Nothing type constructor here, btw, but a string method! | |
cataTap({Nothing}){ if(Nothing){ Nothing(); } return this; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//main function for creating an inline worker: | |
//inlineWorker:: Function -> a -> Promise b | |
const inlineWorker = fn => msg => { | |
const scriptString = `const func = ${fn.toString()}; | |
addEventListener('message', function(e) { | |
Promise.resolve(e.data) | |
.then(func) | |
.then(postMessage); | |
}, false); | |
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//actual function (must be a named function declaration for use w/ toString here) | |
function addOne(x){ return x+1 }; | |
//the actual worker code can be written as a string | |
//template literals make that convienient because they support multiline | |
//note that the addOne function above can be used in this worker code, since we'll be importing it | |
const workerScript = ` | |
self.addEventListener('message', function(e) { | |
var data = e.data; | |
console.log('worker recieved: ',data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const e = {};//define a store of possible props we want to add | |
(function(e){ | |
const empty = Symbol('empty'); | |
const _empty = ()=>[]; | |
const flatten = Symbol('flatten'); | |
function _flatten(){ | |
return this.reduce( (a,b) => a.concat(b), []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//always include a polyfill that falls back to setTimeout(cb,1) https://gist.github.com/paullewis/55efe5d6f05434a96c36 | |
//or possibly something better https://github.com/PixelsCommander/requestIdleCallback-polyfill | |
//calling this function creates and returns a new stack of idle functions, | |
//and exposes an addTasks method that will add more to the stack as needed and return a cancelIdleCallback cancelable id | |
function createIdleStack(){ | |
var bgtasks = [];//array of all tasks | |
function backgroundTask(deadline){ | |
//does each task in order, checking to see if there's time left |