Skip to content

Instantly share code, notes, and snippets.

View YozhEzhi's full-sized avatar

Alexandr Zhidovlenko YozhEzhi

  • Sportmaster Lab
  • Saint-Petersburg, Russia
View GitHub Profile
@YozhEzhi
YozhEzhi / es6-custom-bind.js
Last active January 15, 2018 06:50
ES6 custom bind
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
var mySingleton = (function () {
var instance;
function init() {
function privateMethod() {
console.log( "I am private" );
}
var privateVariable = "Im also private";
@YozhEzhi
YozhEzhi / es6-singleton-module-scoped-instance.js
Last active September 27, 2019 08:28 — forked from dmnsgn/SingletonModuleScopedInstance.js
ES6 singleton pattern: constructor returns an instance scoped to the module. With using the `new` operator.
let instance;
class Singleton {
constructor() {
if (!instance) {
instance = this;
}
this._type = 'Singleton';
return instance;
@YozhEzhi
YozhEzhi / es6-singleton-module-scoped-instance.js
Last active February 25, 2017 14:53 — forked from dmnsgn/SingletonEnforcer.js
ES6 singleton pattern: constructor returns an instance scoped to the module. Without using the `new` operator.
const singleton = Symbol();
const singletonEnforcer = Symbol();
class Singleton {
constructor(enforcer) {
if (enforcer !== singletonEnforcer) {
throw new Error('Cannot construct singleton');
}
this._type = 'Singleton';
/**
* @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
@YozhEzhi
YozhEzhi / pipeline-and-compose.js
Last active March 12, 2017 15:37
Pipeline & compose functions
/* ES5 version */
function pipeline(seed) {
var fns = [].slice.call(arguments, 1);
return fns.reduce(function(result, fn) {
return fn(result);
}, seed);
}
function compose() {
@YozhEzhi
YozhEzhi / binarySearch.js
Created April 8, 2017 21:02
Binary search
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) {
@YozhEzhi
YozhEzhi / recursive-actions-with-array.js
Last active April 10, 2017 17:04
Recursive actions with array
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
@YozhEzhi
YozhEzhi / quicksort.js
Created April 10, 2017 17:02
Quicksort
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)];
}
@YozhEzhi
YozhEzhi / unique-items.js
Created April 19, 2017 06:54
Get unique items from array.
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;