This file contains hidden or 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 bind = (target, context, ...bindedArgs) => | |
(...args) => target.call(context, ...bindedArgs, ...args); | |
// Example 1. | |
const sumFn = function(...args) { | |
return [...args].reduce((prev, next) => prev + next, this.value); | |
}; | |
const bindedSum = bind(sumFn, {value: 10}, 20, 30); | |
console.log(bindedSum(40, 50, 60)); // 210 |
This file contains hidden or 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
var mySingleton = (function () { | |
var instance; | |
function init() { | |
function privateMethod() { | |
console.log( "I am private" ); | |
} | |
var privateVariable = "Im also private"; | |
This file contains hidden or 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 instance; | |
class Singleton { | |
constructor() { | |
if (!instance) { | |
instance = this; | |
} | |
this._type = 'Singleton'; | |
return instance; |
This file contains hidden or 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 singleton = Symbol(); | |
const singletonEnforcer = Symbol(); | |
class Singleton { | |
constructor(enforcer) { | |
if (enforcer !== singletonEnforcer) { | |
throw new Error('Cannot construct singleton'); | |
} | |
this._type = 'Singleton'; |
This file contains hidden or 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
/** | |
* @author https://github.com/vasanthk/js-bits/blob/master/js/currying.js | |
* Currying refers to the process of transforming a function with multiple arity (# or args a fn accepts) | |
* into the same function with less arity. | |
* | |
* Briefly, currying is a way of constructing functions that allows partial application of a function’s arguments. | |
* What this means is that you can pass all of the arguments a function is expecting and get the result, | |
* or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments. | |
* | |
* Currying vs Partial Application |
This file contains hidden or 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
/* ES5 version */ | |
function pipeline(seed) { | |
var fns = [].slice.call(arguments, 1); | |
return fns.reduce(function(result, fn) { | |
return fn(result); | |
}, seed); | |
} | |
function compose() { |
This file contains hidden or 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
function binarySearch(list, item) { | |
let low = 0; | |
let high = list.length - 1; | |
while(low <= high) { | |
let mid = Math.floor((low + high) / 2); | |
let guess = list[mid]; | |
if (guess === item) return guess; | |
if (guess > item) { |
This file contains hidden or 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 myArr = [1, 2, 5, 4, 7]; | |
// Recursive sum: | |
function sum(arr) { | |
if (arr.length === 0) return 0; | |
return arr[0] + sum(arr.slice(1)); | |
} | |
console.log(sum(myArr)); // 19 | |
This file contains hidden or 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
function quicksort(arr) { | |
if (arr.length < 2) return arr; | |
const pivot = arr[0]; | |
const less = arr.filter(item => item < pivot && item !== pivot); | |
const greater = arr.filter(item => item > pivot && item !== pivot); | |
return [...quicksort(less), pivot, ...quicksort(greater)]; | |
} |
This file contains hidden or 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
var arr = [1,2,3,3,NaN,null,false,false,undefined,2,undefined,'str','str',NaN,null, [1,2,3],[1,2,3],[3,1,2],{1:1},{1:1},{1:2}]; | |
function u(arr) { | |
var unique=[], | |
hashes={}; | |
for(var i = 0; i < arr.length; i++) { | |
var currentHash = JSON.stringify(!!arr[i] ? arr[i] : ''+arr[i]); | |
if(!hashes[currentHash]){ | |
unique.push(arr[i]); | |
hashes[currentHash]=true; |