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 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);
// maxOnlyEven(): undefined
// maxOnlyEven(1): undefined
// maxOnlyEven(1,3): undefined
// maxOnlyEven(1,4): 4
// maxOnlyEven(2): 2
// maxOnlyEven(undefined,2): 2
// maxOnlyEven(2,4): 4
function maxOnlyEven(num1,num2) {
var A = num1 % 2 == 0;
@getify
getify / 1.js
Last active October 6, 2016 07:01
// examples...
//
// maxEven(): undefined
// maxEven(1): undefined
// maxEven(1,13): undefined
// maxEven(-Infinity,Infinity): undefined
// maxEven(0): 0
// maxEven(-4,3,-6,0,5,2,1,9): 2
function maxEven(num1,...nums) {