Skip to content

Instantly share code, notes, and snippets.

View alexnoz's full-sized avatar
💡

Alex Nozdriukhin alexnoz

💡
  • Third Stone from the Sun
View GitHub Profile
@alexnoz
alexnoz / observer.js
Created July 17, 2017 15:36
The "Observer" pattern
const Subject = function () {
const observersList = [];
const addObserver = observer => {
observersList.push(observer);
}
const removeObserver = toRemove => {
const i = observersList.findIndex(observer => observer == toRemove);
@alexnoz
alexnoz / pubsub.js
Created July 19, 2017 13:29
The "Publish-subscribe" pattern
const pubsub = (function () {
const topics = {};
let subUid = -1;
function publish (topic, args) {
if (!topics[topic])
return false;
const subscribers = topics[topic];
@alexnoz
alexnoz / get-custom-event.js
Created July 23, 2017 06:53
Create custom event in a cross-browser manner. Works in IE >= 9
/**
* @param {String} name - The event's name
* @param {Object} [options] - Options to pass to the event constructor
* @returns Custom event
*/
function getCustomEvent (name, options = {}) {
let event;
if (typeof window.CustomEvent === 'function') {
event = new CustomEvent(name, options);
@alexnoz
alexnoz / .js
Last active December 12, 2017 05:38 — forked from abozhilov/.js
Merge sorted iterators
'use strict';
function* merge(iterable1, iterable2) {
let iter1 = iterable1[Symbol.iterator]();
let iter2 = iterable2[Symbol.iterator]();
let a = iter1.next();
let b = iter2.next();
while(!a.done && !b.done) {
if (a.value <= b.value) {
yield a.value;
@alexnoz
alexnoz / roman-sequence.js
Created December 12, 2017 17:14
A generator function that yields roman numerals in the specified diapason
function* romanSequence (from, to) {
switch (arguments.length) {
case 0:
console.warn('[romanNumerals]: the function expects at least one argument')
return
case 1:
to = from
from = 1
}
@alexnoz
alexnoz / quicksort.js
Last active December 14, 2017 08:47
The 'quicksort' algorithm implemented using Hoare's partition scheme
function swap(arr, i, j) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
function quickSort (arr) {
function partition (lo, hi) {
const p = arr[Math.floor((lo + hi) / 2)]
@alexnoz
alexnoz / mergesort-in-place.js
Last active December 14, 2017 08:47
The merge sorting algorithm (in place version)
function mergeSort (arr) {
function merge (l, m, r) {
let i = 0, j = 0, k = l
const leftL = m - l + 1
const rightL = r - m
const tempL = []
const tempR = []
@alexnoz
alexnoz / shellsort.js
Created December 15, 2017 15:16
The Shell's sorting algorithm
function shellsort (arr) {
const getGap = l => Math.floor(l / 2)
let gap = getGap(arr.length)
while ((gap = getGap(gap)) > 0) {
for (let i = gap; i < arr.length; i++) {
const temp = arr[i]
for (var j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap]
@alexnoz
alexnoz / postfix.cpp
Last active June 7, 2018 21:17
A dead-simple postfix expression evaluator
#include <stack>
#include <string>
#include <cctype>
#include <cstdlib>
using std::stack;
using std::string;
int evaluateExpr(const char *op, stack<int> &tokens) {
int b = tokens.top(); tokens.pop();
@alexnoz
alexnoz / abortable.js
Created June 13, 2018 14:05
Abortable fetch helper
function getAbortable () {
let controller
return {
fetch: (url, opts) => {
controller = new AbortController()
return fetch(url, Object.assign(opts, {
signal: controller.signal
}))