Skip to content

Instantly share code, notes, and snippets.

View .js
const create = (() => {
const handler = {
get: (obj, property, reciever) => {
if (!Reflect.has(obj, property)) {
throw new ReferenceError(`Object has no property ${property}`);
}
return Reflect.get(obj, property);
}
};
View fromKeys.js
function fromKeys(iterable, value) {
let obj = {};
for (let i of iterable) {
if (typeof value === 'function') {
obj[i] = value(i)
}
else {
obj[i] = value;
}
@abozhilov
abozhilov / .js
Created Dec 11, 2017
Merge sorted iterators
View .js
'use strict';
function* merge(iterable1, iterable2) {
let iter1 = iterable1[Symbol.iterator]();
let iter2 = iterable2[Symbol.iterator]();
let a = iter1.next();
let b = iter2.next();
let isFirst;
while(!a.done && !b.done) {
if (a.value <= b.value) {
View gist:7aa2b0bf4682ea6909823d2d04b9d64b
| | 1 2 3
| 1 | 2 3
| 1 2 | 3
| 1 2 3 |
1 | | 2 3
1 | 2 | 3
1 | 2 3 |
1 2 | | 3
1 2 | 3 |
1 2 3 | |
@abozhilov
abozhilov / .js
Last active Sep 29, 2016
scroll.js
View .js
var pos = 0;
window.addEventListener('scroll', function () {
pos = document.body.scrollTop * (100 / document.body.scrollHeight)
})
window.addEventListener('resize', function () {
var scrollTop = pos * (document.body.scrollHeight / 100)
window.scrollTo(0, scrollTop)
})
View sort.js
var sort = (function () {
function defCompare(a, b) {
var aStr = String(a),
bStr = String(b);
if (aStr > bStr) {
return 1;
}
else if (aStr < bStr) {
return -1;
@abozhilov
abozhilov / .js
Created Jun 8, 2016
case + break
View .js
switch(x) {
case 1:
//do something
break;
case 2:
//do something
break;
}
View bridges.js
var north = [2, 5, 3, 3, 3, 1, 8, 2, 6, 7, 6];
var south = [1, 2, 5, 3, 4, 1, 7, 8, 2, 5, 6];
var nLen = north.length;
var sLen = south.length;
var matrix = [];
for (var i = 0; i <= nLen; i++) {
matrix[i] = [];
for (var j = 0; j <= sLen; j++) {
@abozhilov
abozhilov / flatten.js
Last active Dec 6, 2015
Tail call flatten
View flatten.js
function flatten(arr) {
var res = [];
(function dfs(arr, i) {
if (i >= arr.length) {
return;
}
if (Array.isArray(arr[i])) {
return dfs(arr[i], 0);
View .js
var arr1 = [1, 2, 3, 4],
arr2 = [5, 6, 7, 8, 9];
function* values(arr) {
for (var i = 0, len = arr.length; i < len; i++) {
yield arr[i];
}
}
function* zip(...iters) {