Skip to content

Instantly share code, notes, and snippets.

💭
Just coding

Kyle Simpson getify

💭
Just coding
Block or report user

Report or block getify

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@getify
getify / ex1-prototype-style.js
Last active Jun 15, 2019
OLOO (objects linked to other objects) pattern explored (with comparison to the prototype style of the same code)
View ex1-prototype-style.js
function Foo(who) {
this.me = who;
}
Foo.prototype.identify = function() {
return "I am " + this.me;
};
function Bar(who) {
Foo.call(this,"Bar:" + who);
@getify
getify / 1.js
Last active Jun 8, 2019
race-promises-pool
View 1.js
async function racePromisesPool([ ...prs ] = []) {
var raceWon = false;
var prListeners = prs.map(function listen(pr,idx){
return pr.then(function t(v){
if (!raceWon) {
raceWon = true;
prs.splice(idx,1); // remove the promise from the pool since it won the race
return v;
}
});
@getify
getify / 1.js
Created Jun 7, 2019
merging defaults/settings objects with destructuring
View 1.js
ajax(
ajaxOptions({
url: "https://my.other.tld/api",
headers: {
"Cache-Control": "no-cache"
},
cb: resp => console.log(resp)
})
);
@getify
getify / 1.js
Last active Jun 3, 2019
is Maybe a "monad?
View 1.js
// is Just(..) a monad? Well, it's a monad constructor.
// Its instances are certainly monads.
function Just(v) {
return { map, chain, ap };
function map(fn) {
return Just(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / 1.js
Last active May 31, 2019
Force Monad #MayThe4thBeWithYou
View 1.js
var Force = { Skywalker, RegularFolk, of: Skywalker };
function Skywalker(v) {
return { map, chain, ap };
function map(fn) {
return Skywalker(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / gist:7325764
Last active May 28, 2019
converting between Uint8Arrays and binary-encoded strings and word-arrays (for crypto purposes, with CryptoJS and NaCL)
View gist:7325764
/*
wordArray: { words: [..], sigBytes: words.length * 4 }
*/
// assumes wordArray is Big-Endian (because it comes from CryptoJS which is all BE)
// From: https://gist.github.com/creationix/07856504cf4d5cede5f9#file-encode-js
function convertWordArrayToUint8Array(wordArray) {
var len = wordArray.words.length,
u8_array = new Uint8Array(len << 2),
offset = 0, word, i
@getify
getify / ex1.js
Last active May 25, 2019
class vs OLOO
View ex1.js
class Foo {
constructor(x,y,z) {
Object.assign(this,{ x, y, z });
}
hello() {
console.log(this.x + this.y + this.z);
}
}
@getify
getify / gist:1b26accb1a09aa53ad25
Last active May 23, 2019
first draft sketch of a "Worker" polyfill
View gist:1b26accb1a09aa53ad25
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Worker Polyfill</title>
<script src="polyfill.worker.js"></script>
</head>
<body>
<h1>Worker Polyfill</h1>
<script>
@getify
getify / gist:3667624
Created Sep 7, 2012
escape all double-quote chars in a string
View gist:3667624
// NOTE: only escapes a " if it's not already escaped
function escapeDoubleQuotes(str) {
return str.replace(/\\([\s\S])|(")/g,"\\$1$2"); // thanks @slevithan!
}
escapeDoubleQuotes("ab"); // ab => ab
escapeDoubleQuotes("a\"b"); // a"b => a\"b
escapeDoubleQuotes("a\\\"b"); // a\"b => a\"b
escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b
View pi-hex-digits.js
// This is a port of the C-language reference implementation, which can be
// found here: http://www.experimentalmath.info/bbp-codes/piqpr8.c
/*
This program implements the BBP algorithm to generate a few hexadecimal
digits beginning immediately after a given position id, or in other words
beginning at position id + 1. On most systems using IEEE 64-bit floating-
point arithmetic, this code works correctly so long as d is less than
approximately 1.18 x 10^7. If 80-bit arithmetic can be employed, this limit
You can’t perform that action at this time.