Skip to content

Instantly share code, notes, and snippets.

View c4x1's full-sized avatar
๐ŸŒ€
home->work->home->work;

CSpornov c4x1

๐ŸŒ€
home->work->home->work;
  • Russian
View GitHub Profile
@c4x1
c4x1 / btree.js
Created May 20, 2019 11:07 — forked from marcin-chwedczuk/btree.js
BTree implementation in JavaScript
#!/usr/bin/env node
const NKEYS = 4;
function arrayOfSize(size) {
var a = Array(size);
for (var i = 0; i < size; i += 1)
a[i] = null;
@c4x1
c4x1 / bst.es6
Created May 20, 2019 11:10 — forked from jochasinga/bst.es6
Binary search tree implemented in ES6
'use strict';
class Node {
constructor(data) {
this.data = data;
this.left = undefined;
this.right = undefined;
}
'use strict';
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype.insertNode = function (val) {
var node = {
data : val,
'use strict';
/*
* Binary search tree with in-order iteration.
* http://greim.github.io/gen/dist/00-intro.html
*/
class Tree {
add(value) {
if (this.root) this.root.add(value);
@c4x1
c4x1 / compose.js
Created May 20, 2019 11:25 — forked from WaldoJeffers/compose.js
JavaScript one-line compose (ES6)
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
@c4x1
c4x1 / createStore-no-comments.js
Created June 11, 2019 04:29 — forked from markerikson/createStore-no-comments.js
Redux mini createStore example
function createStore(reducer) {
var state;
var listeners = []
function getState() {
return state
}
function subscribe(listener) {
listeners.push(listener)
@c4x1
c4x1 / observer.js
Created June 12, 2019 08:00 — forked from Komock/observer.js
Simple ES6 Observer Class
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 = [];
@c4x1
c4x1 / Trie.js
Created June 15, 2019 19:42 — forked from deadlocked247/Trie.js
Trie implementation in ES6, good for string autocomplete
/* Class representing a Trie data structure */
export default class Trie {
/**
* Creates a Trie
* @return {Object} Trie
*/
constructor() {
this.words = 0;
this.prefixes = 0;
@c4x1
c4x1 / searchtrie.js
Created June 15, 2019 19:44 — forked from achadha235/searchtrie.js
Simple Search Tries in ES6
/*
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).
@c4x1
c4x1 / es7-async-await.js
Created June 27, 2019 10:37
Javascript fetch JSON with ES7 Async Await
// 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