Skip to content

Instantly share code, notes, and snippets.

View blazecolour's full-sized avatar
🔥

Sergei Shitikov blazecolour

🔥
View GitHub Profile
@blazecolour
blazecolour / linkedList.js
Created September 15, 2017 20:22
Linked list
function LinkedListToArr(list) {
var result = [list.value],
obj = list;
while (obj.next) {
result.push(obj.next.value)
obj = obj.next;
}
return result.join(', ');
}
let instance = null;
class Cashe {
constructor() {
if (!instance) {
instance = this;
}
this.time = new Date();
return instance;
}
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log("Index: " + i + ", element: " + arr[i]);
}, 3000);
}
// -> Index: 4, element: undefined(printed 4 times)
// type 𝔹 := ∀ α, α → α → α
// T : 𝔹
const T = a => b => a;
// F : 𝔹
const F = a => b => b;
// iff : ∀ α, 𝔹 → α → α → α
const iff = c => a => b => c(a)(b);
const average = (...arg) =>
arg.reduce((total, amount, index) => {
total += amount;
return index === arg.length - 1 ? total / arg.length : total;
});
@blazecolour
blazecolour / count-items-arr.js
Last active January 28, 2019 23:01
how many of each item are in the array (this gives us an object with all the items as keys)
const count = arr => arr.reduce((acc, item) =>
({ ...acc, [item]: acc[item] ? acc[item] + 1 : 1 }), {});
// const count = arr =>
// arr.reduce((acc, item) => ({ ...acc, [item]: (acc[item] || 0) + 1 }), {});
const flatten = arr =>
Array.isArray(arr)
? arr.reduce((done, curr) => done.concat(flatten(curr)), [])
: arr;
const clone = x => [...x];
const push = x => y => [...x, y];
const pop = x => x.slice(0, -1);
const unshift = y => x => [y, ...x];
const shift = x => x.slice(1);
const map = (fn, arr) =>
arr.reduce((acc, item, index, arr) => {
return acc.concat(fn(item, index, arr));
}, []);
const filter = (fn, arr) =>
arr.reduce((newArr, item) => {
return fn(item) ? newArr.concat([item]) : newArr;
}, []);