Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / 1.js
Last active October 15, 2017 01:35
generators as object iterator
// ugly way
var obj = {
a: 1,
b: 2,
c: 3,
[Symbol.iterator]() {
var keys = Object.keys(this);
var idx = 0;
return {
@getify
getify / 1.js
Last active February 15, 2017 19:11
comparing FPO
function lowercase(v) {
return v.toLowerCase();
}
function uppercase(v) {
return v.toUpperCase();
}
var words = ["Now","Is","The","Time"];
var moreWords = ["The","Quick","Brown","Fox"];
@getify
getify / 1.js
Last active February 13, 2017 14:35
gatherArgs + gatherArgProps
function gatherArgs(fn) {
return function gatheredFn(...argsArr) {
return fn( argsArr );
};
}
function gatherArgProps(fn,propOrder = []) {
return function gatheredFn(...args) {
return fn(
zip( propOrder, args )
@getify
getify / 1.js
Last active February 13, 2017 06:50
curryProps + partialProps + spreadArgProps
function curryProps(fn,arity = 1) {
return (function nextCurried(prevArgs){
return function curried(nextArg){
if (!nextArg || typeof nextArg != "object") {
nextArg = { value: nextArg };
}
var [key] = Object.keys( nextArg );
var args = Object.assign( {}, prevArgs, { [key]: nextArg[key] } );
@getify
getify / zip-iterators-core.js
Last active January 26, 2017 01:08
Zipping Iterators
function *zipIterators(...its) {
its = its.map(it => (!it.next && it[Symbol.iterator]) ? it[Symbol.iterator]() : it);
while (its.length > 0) {
let entry = [];
let itsCopy = [...its];
let it;
while (it = itsCopy.shift()) {
its.shift();
let res = it.next();
@getify
getify / number-prototype-iterator.js
Created January 25, 2017 23:52
Make JavaScript Great Again
Object.defineProperty(Number.prototype,Symbol.iterator,{
*value({ start = 0, step = 1 } = {}) {
var inc = this > 0 ? step : -step;
for (let i = start; Math.abs(i) <= Math.abs(this); i += inc) {
yield i;
}
},
enumerable: false,
writable: true,
configurable: true
@getify
getify / object-prototype-tostringtag.js
Created January 25, 2017 23:31
Make JavaScript Great Again
Object.defineProperty(Object.prototype,Symbol.toStringTag,{
get() {
return JSON.stringify(this);
},
set(v) {
Object.defineProperty(Object.prototype,Symbol.toStringTag,{
value: v,
enumerable: false,
writable: true,
configurable: true
// Doesn't throw an error, but doesn't return an object
const getObj = () => { a: 1 };
console.log(getObj()); //=> val 'undefined' : void
// Reason: It looks like an arrow function returning an object, but it isn't.
// It's an arrow function with a function body (containing a labeled
// statement) that returns nothing.
const getObj = () =>
{ // <- Start function body
a: // <- A label to use when break/continue; e.g. break a;
@getify
getify / 1.js
Created January 3, 2017 04:36
simple class inheritance vs oloo delegation
class Foo {
one() {
return this.x;
}
}
class Bar extends Foo {
two() {
return this.y;
}
@getify
getify / EAT-1.js
Last active December 2, 2016 01:15
// EAT: Eager Async Thunk, aka "promise"
function gimmeFutureVal(x) {
var fs = [], v;
doSomethingAsync(x,function res(r){
if (fs.length > 0) {
v = r;
while (fs.length > 0) {
fs.shift()(v);