Skip to content

Instantly share code, notes, and snippets.

View Beraliv's full-sized avatar
📺
Contributing to OSS 7-9am, 7-9pm UK time (respond within a week)

Alexey Berezin Beraliv

📺
Contributing to OSS 7-9am, 7-9pm UK time (respond within a week)
View GitHub Profile
@Beraliv
Beraliv / allMultiplierPairsOfArr.js
Last active October 21, 2017 10:29
Get all number pairs which [[a, b], a * b = value], for a given value
/**
* ES6 and featured code
*/
let combineGetters = (...getters) => state => getters.reduce(({ payload }, getter) => ({ payload: getter(state, payload) }), { payload: state });
let findPairsInSorted = arr => value => {
let results = [];
let len = (arr && arr.length) || 0;
if (len < 1) {
return results;
@Beraliv
Beraliv / allMultiplierPairsOfArr2.js
Last active October 21, 2017 10:29
Get all number pairs which [[a, b], a * b = value], for a given value
/**
* mostly ES5 without featured code
* except for let and arrow functions which are better for reading and understanding
*
* FYI
* 1) a => a > 0 equals function (a) { return a > 0; } and is just a syntax sugar
* 2) let is used as an example of best practices
* 3) map is used to change the element to the result
* [-3, 1, 15].map(a => a < 0 ? [a, 0] : [0, a]) => [[-3, 0], [0, 1], [0, 15]]
* 4) filter is used to get only those elements which function is true for the condition
@Beraliv
Beraliv / allAddentPairsInArr.js
Created October 21, 2017 10:36
Get all number pairs which [[a, b], a + b = value], for a given value
function findPairsInSorted(arr, value) {
let results = [];
let len = arr ? arr.length : 0;
if (len < 2) {
return results;
}
for (let i = 0, j = len - 1; i < j;) {
let diff = value - arr[i];
if (diff < arr[j]) {
j--;
@Beraliv
Beraliv / max.js
Last active October 21, 2017 19:49
Find the max of 2 numbers without any condition: if-else-clause, ternary operator, third-party library or core implementation.
/**
* The only difference here is that sign(0) = 1
*/
let sign = a => {
let signBit = a >>> 31;
return signBit * -1 + (1 - signBit) * 1;
};
let abs = a => a * sign(a);
@Beraliv
Beraliv / debounceAndThrottle.js
Last active November 10, 2017 01:09
Debounce and throttle lodash analogues
/**
* For clafirying go to https://css-tricks.com/the-difference-between-throttling-and-debouncing/
*
*/
count DEFAULT_DEBOUNCE_TIME = 400;
const DEFAULT_THROTTLE_TIME = 400;
let debounce = (fn, time = DEFAULT_DEBOUNCE_TIME) => {
let timeoutId;
return (...args) => {
@Beraliv
Beraliv / lazyArray.js
Last active November 20, 2017 19:46
Lazy Array operations: map, filter, reduce. Not completed.
let LazyArray = (() => {
let _arr = [];
let _ops = [];
const isEmpty = v => v === null || v === undefined;
const combine = (v, ...fns) => {
let result = v;
for (let { type, fn } of fns) {
@Beraliv
Beraliv / kaspersky.lab.task1.js
Created December 22, 2017 20:40
Car drive history
const CarFactory = ({ name }) => {
let distancePassed = 0;
let driveHistory = [];
let car = {
name,
beep({ message = '' } = {}) {
console.log(`${name}: Beeeeep! ${message}`);
},
@Beraliv
Beraliv / advance-your-js-skills.functions.js
Last active January 3, 2018 01:52
Douglas Crockford. The Good Parts of JavaScript and the Web. Fun with functions.
const unary = {
identity: x => x,
inc: x => x + 1,
dec: x => x - 1,
double: x => 2 * x,
sqr: x => x * x,
identityf: x => () => unary.identity(x),
};
@Beraliv
Beraliv / words.js
Last active January 6, 2018 20:42
Word Games and methods to work with combinations: contains, starts and ends.
// TODO:
// 1) optimise search by sorting strings
/*
* words: Factory for finding words
*
* letters: String or Array,
* length: Number,
*/
const words = (() => {
@Beraliv
Beraliv / patterns.less
Last active January 11, 2018 08:54
Less patterns
/* different parameters based on number of elements */
.selector:nth-last-child(2):first-child {
&, & ~ .selector {
// setting parameter, i.e.: flex-basis: 50%;
}
}
.selector:nth-last-child(3):first-child {
&, & ~ .selector {
// setting parameter, i.e.: flex-basis: 33.33%;