Skip to content

Instantly share code, notes, and snippets.

View e-mihaylin's full-sized avatar
🏠
Working from home

Eugene Mihaylin e-mihaylin

🏠
Working from home
View GitHub Profile
const once = fn => ((...a) => {
let executed = false;
return (...a) => {
if (!executed) {
executed = true;
return fn(...a);
}
};
})();
const gcd = (a, b) => a ? gcd(b % a, a) : b;
const lcm = (a, b) => a * b / gcd(a, b);
[1, 2, 3, 4, 5].reduce(lcm); // Returns 60
const add = n => {
const f = x => add(n + x);
f.valueOf = () => n;
return f;
}
// Other Variants:
function add(n) {
var f = function(x) { return add(n+x); };
f.valueOf = function() { return n; };
const longestSlideDown = p => p.reduceRight((x, e) => e.map((v, i) => v + Math.max(x[i], x[i + 1])))[0];
// https://www.codewars.com/kata/551f23362ff852e2ab000037
const factorial = n => {
if (!n) return '1'
let i, next, carret;
const result = n.toString().split``.reverse().map(Number);
while (--n) {
i = carret = 0;
while ((next = result[i++]) !== undefined || carret) {
carret += n * (next || 0);
result[i - 1] = carret % 10;
const multiply = (a, b) => {
const stack = [];
a = a.split``.reverse();
b = b.split``.reverse();
for (let i = 0, la = a.length; i < la; i++) {
for (let j = 0, lb = b.length; j < lb; j++) {
const m = a[i] * b[j];
const s = stack[i + j];
stack[i + j] = s ? s + m : m;
const getPascalsTriangleRow = n => {
const a = [];
let x = 0;
for (let i = 0; i < n; i++) {
x = a.length - i;
for (let j = 0; j < i + 1; j++)
(j === 0 || j === i) ? a.push(1) :
a.push(a[x + j] + a[x + j - 1]);
}
const count = n => Math.floor((ln(2 * Math.PI * n) / 2 + n * (ln(n) - 1)) / ln(10)) + 1;
const ln = x => Math.log(x) / Math.log(Math.E);
const calc = s => run(s.replace(/\s+/g, '').split``);
const run = s => {
let result = term(s);
while (peek(s) == '+' || peek(s) == '-') {
if (get(s) == '+') result += term(s);
else result -= term(s);
}
return result;
};
const justify = (s, length) => {
s = s.replace(/\n/g, ' ')
s = s.split` `;
const res = [];
let line = [];
let words = 0;
for (let i = 0; i < s.length; i++) {
if (words + line.length - 1 + s[i].length < length) {
line.push(s[i]);