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
var arr = [
[10, 10, 10, 20, 60],
[10, 10, 50, 10, 10],
[20, 10, 30, 20, 10]
];
var dest = arr.reduce((acc, cur) =>
acc.map((el, i) => Math.max(el, cur[i]))
);
@YozhEzhi
YozhEzhi / dom-helper.js
Created April 20, 2017 19:38 — forked from SitePointEditors/dom-helper.js
Mini jQuery, sort of.
/**
* A collection of helper prototype for everyday DOM traversal, manipulation,
* and event binding. Sort of a minimalist jQuery, mainly for demonstration
* purposes. MIT @ m3g4p0p
*/
window.$ = (function (undefined) {
/**
* Duration constants
* @type {Object}
@YozhEzhi
YozhEzhi / observer-design-pattern.js
Created April 21, 2017 23:39
Observer Design Pattern
const Subject = function() {
let observers = [];
return {
subscribeObserver(observer) {
observers.push(observer);
},
unsubscribeObserver(observer) {
const index = observers.indexOf(observer);
if(index > -1) {
@YozhEzhi
YozhEzhi / bubbleSort.js
Last active April 23, 2017 13:20
Bubble Sort
/**
* Helper function to swap items in array
*/
function swap(arr, firstIndex, secondIndex) {
const temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
/**
@YozhEzhi
YozhEzhi / mergeSort.js
Last active April 23, 2017 14:16
Merge sort
const arr = [34, 203, 3, 746, 200, 984, 198, 764, 9];
const merge = (xs, ys) => {
if (xs.length === 0) return ys;
if (ys.length === 0) return xs;
const x = xs[0];
const y = ys[0];
return (x < y)
? [x, ...merge(xs.slice(1), ys)]
@YozhEzhi
YozhEzhi / pub-sub-pattern.js
Last active April 27, 2017 18:30
Pub/Sub JavaScript Object
var events = (function(){
var topics = {};
var hOP = topics.hasOwnProperty;
return {
subscribe: function(topic, listener) {
// Create the topic's object if not yet created
if(!hOP.call(topics, topic)) topics[topic] = [];
// Add the listener to queue
@YozhEzhi
YozhEzhi / pipeline.js
Last active April 30, 2017 12:55
Pipeline function execution
const pipeline = (seed, ...fns) => fns.reduce((val, fn) => fn(val), seed);
console.log(pipeline()); // undefined
console.log(pipeline(42)); // 42
console.log(pipeline(42, n => n*2, n => -n)); // -84
@YozhEzhi
YozhEzhi / compose.js
Last active April 30, 2017 12:24
Compose functions
const compose = (...fns) =>
(seed) =>
fns.reduceRight((val, fn) => fn(val), seed);
const greet = name => `Hi, ${name}`;
const exclaim = statement => `${statement.toUpperCase()}!`;
const welcome = compose(greet, exclaim);
console.log(welcome('Yozh')); // 'Hi, YOZH!'
@YozhEzhi
YozhEzhi / factory-constructor-proto-patterns.js
Created May 6, 2017 15:57
Factory, Constructor & Proto creational patterns.
const peopleFactory = function(name, age, sex) {
return {
name,
age,
sex,
};
};
const human01 = peopleFactory('Alex', 28, 'male');
@YozhEzhi
YozhEzhi / handlers-delegation.js
Created May 6, 2017 16:11
Handlers delegation.
window.onload = function() {
paintPonit(document.body, 'click', function(event) {
if (event.target.nodeName === 'I') {
event.target.className = 'clicked';
}
});
function paintPonit(obj, eventName, handler) {
const handlerWrapper = event => handler.call(obj, event);
obj.addEventListener(eventName, handlerWrapper, false);