This file contains 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 Subject = function () { | |
const observersList = []; | |
const addObserver = observer => { | |
observersList.push(observer); | |
} | |
const removeObserver = toRemove => { | |
const i = observersList.findIndex(observer => observer == toRemove); |
This file contains 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 pubsub = (function () { | |
const topics = {}; | |
let subUid = -1; | |
function publish (topic, args) { | |
if (!topics[topic]) | |
return false; | |
const subscribers = topics[topic]; |
This file contains 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
/** | |
* @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); |
This file contains 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
'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; |
This file contains 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* 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 | |
} |
This file contains 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 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)] | |
This file contains 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 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 = [] |
This file contains 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 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] |
This file contains 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
#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(); |
This file contains 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 getAbortable () { | |
let controller | |
return { | |
fetch: (url, opts) => { | |
controller = new AbortController() | |
return fetch(url, Object.assign(opts, { | |
signal: controller.signal | |
})) |
OlderNewer