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 () { | |
| 'use strict'; | |
| var cluster = require('cluster'), | |
| http = require('http'), | |
| os = require('os'), | |
| /* | |
| * ClusterServer object |
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 us re-create `Promise.all` | |
| `Promise.all` method returns a promise that resolves when all of the promises in the iterable argument have resolved, | |
| or rejects with the reason of the first passed promise that rejects. | |
| Read more about `Promise.all` on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all | |
| A basic example would be something like this: | |
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
| /** Async version of Array.prototype.reduce() | |
| * await reduce(['/foo', '/bar', '/baz'], async (acc, v) => { | |
| * acc[v] = await (await fetch(v)).json(); | |
| * return acc; | |
| * }, {}); | |
| */ | |
| export async function reduce(arr, fn, val, pure) { | |
| for (let i=0; i<arr.length; i++) { | |
| let v = await fn(val, arr[i], i, arr); | |
| if (pure!==false) val = v; |
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
| 'use restrict'; | |
| /** | |
| * Module dependences. | |
| */ | |
| var mongoose = require('mongoose'); | |
| /** | |
| * load up the material model. | |
| */ |
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
| // Async/Await requirements: Latest Chrome/FF browser or Babel: https://babeljs.io/docs/plugins/transform-async-to-generator/ | |
| // Fetch requirements: Latest Chrome/FF browser or Github fetch polyfill: https://github.com/github/fetch | |
| // async function | |
| async function fetchAsync () { | |
| // await response of fetch call | |
| let response = await fetch('https://api.github.com'); | |
| // only proceed once promise is resolved | |
| let data = await response.json(); | |
| // only proceed once second promise is resolved |
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
| /* | |
| TRIES | |
| A trie (or radix tree) is a simple data structure that represents a set of values stored against a | |
| tokenizable key (usually strings). Each edge in a Trie represents a token in a key. A key is present in the trie if | |
| (1) There exists a path (e1..en) from the root such that the key = <token(e1), token(e2)...token(en)> and | |
| (2) there is some value at the path's terminal vertex vn. | |
| Let k = length(key). Then time complexity for lookup, insertion and deletion for a Trie is O(k). |
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
| /* Class representing a Trie data structure */ | |
| export default class Trie { | |
| /** | |
| * Creates a Trie | |
| * @return {Object} Trie | |
| */ | |
| constructor() { | |
| this.words = 0; | |
| this.prefixes = 0; |
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
| class Observer { | |
| subscribe(subscriber) { | |
| this.subscribers.push(subscriber); | |
| } | |
| publish(event, data) { | |
| let query = this.subscribers.filter(subscriber => subscriber.event === event); | |
| if (query.length) query.forEach(subscriber => subscriber.action(data)); | |
| } | |
| constructor() { | |
| this.subscribers = []; |
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 createStore(reducer) { | |
| var state; | |
| var listeners = [] | |
| function getState() { | |
| return state | |
| } | |
| function subscribe(listener) { | |
| listeners.push(listener) |
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 compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))) | |
| // Usage : compose functions right to left | |
| // compose(minus8, add10, multiply10)(4) === 42 | |
| // | |
| // The resulting function can accept as many arguments as the first function does | |
| // compose(add2, multiply)(4, 10) === 42 |