Skip to content

Instantly share code, notes, and snippets.

Avatar

Drew dtipson

View GitHub Profile
@dtipson
dtipson / send-postMessage-events.js
Created Sep 25, 2018
Sending postMessage events
View send-postMessage-events.js
function throttle(fn, threshhold = 250, scope) {
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
@dtipson
dtipson / quick-stream-self.js
Last active Jul 26, 2017
Can run in console on any https site on Chrome/Firefox
View quick-stream-self.js
document.body.innerHTML = '';
const delay = milliseconds => x => new Promise(resolve => setTimeout(resolve, milliseconds, x));
//requestRecord :: Object (optional) -> Promise Stream
const requestRecord = (config={video:true, audio:true}) => {
return navigator.mediaDevices && navigator.mediaDevices.getUserMedia ?
navigator.mediaDevices.getUserMedia(config).then(delay(1400)) : // delay avoid startup flash
Promise.reject('no support for getUserMedia');
View Promise.all.js
// mapping over an array with an async function
// would return an Array of Promises which isn't
// super useful to work with on its own
async function fooPromise(x){
return Promise.resolve(x*3);
}
const arr = [6,7,8,9];// -> [Promise[18],Promise[21],Promise[24],Promise[27]]]
@dtipson
dtipson / KeyedCollection.js
Created Apr 24, 2017
Experimenting with a type that contains objects that are listed in insertion order but are also unique by a single chosen prop
View KeyedCollection.js
const getProp = prop => object => object[prop]
function KeyedCollection (Iterable, prop){
if (!(this instanceof KeyedCollection)) {
return new KeyedCollection(Iterable, prop);
}
if(!prop){
throw new Error('must supply a prop')
}
const it_target = []
const selectPropFrom = getProp(prop)
View simple.Task.js
// Finally wrapped your head around Promises? Time to toss out all that knowledge and learn the functional alternative!
// Here's a super simple implementation of a Task "type"
const __Task = fork => ({fork})
// Absurdly simple! All we're doing is using a function that returns some unknown value, by name, in an object.
// At this point "fork" is just a cute name though: what matters is how we use it.
// What makes Task a "Task" is that is that the "fork" value here... will be a higher-order function.
// Here's a usage example, already fully functional, already nearly as powerful as Promise!
View Symbol.toPrimitive.uses.js
var object = {
value:5,
increment(){ return ++this.value; }
};
/*
But of course, even though that value is central to what the object is,
it's not really a _primitive_ value in the sense that we can directly coerce it into a number or string:
*/
View Semigroup List comparisons.js
/*
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);
}
View IO plus Array helpers.js
// 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
@dtipson
dtipson / cellular automata with quasi-comonads.js
Last active Jun 19, 2018
Using a comonad(ish) pattern to create cellular automata in a more functional way. Inspired by this episode of funfunfunction: https://www.youtube.com/watch?v=bc-fVdbjAwk
View cellular automata with quasi-comonads.js
/*
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;
@dtipson
dtipson / IO plus Array & Promise helpers.js
Last active Oct 1, 2019
Bare bones FP type utility lib so we can play around with functions that capture the composition of DOM read/writes, but in a pure way
View IO plus Array & Promise helpers.js
// 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
You can’t perform that action at this time.