This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Каррирование в JavaScript | |
function curry(fn) { | |
// Запоминаем количество аргументов | |
var arity = fn.length; | |
return function f1(...args) { | |
if (args.length >= arity) { | |
return fn(...args); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function swap(arr, firstIndex, secondIndex) { | |
const buf = arr[firstIndex]; | |
arr[firstIndex] = arr[secondIndex]; | |
arr[secondIndex] = buf; | |
} | |
function partition(arr, left, right) { | |
let pivot = arr[Math.floor((right + left) / 2)], | |
i = left, | |
j = right; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node(data) { | |
this.data = data; | |
this.parent = null; | |
this.children = []; | |
} | |
function Tree(data) { | |
var node = new Node(data); | |
this._root = node; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// В виде функции | |
function bind(func, context) { | |
return function() { | |
return func.apply(context, arguments); | |
}; | |
} | |
// В виде полифилла | |
if (!Function.prototype.bind) { | |
Function.prototype.bind = function(context) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3, 4]; | |
/* | |
Реализовать map через reduce. | |
Внимание! Если initialValue передано, то на первой итерации функция будет вызвана с | |
этим значением и значением первого элемента массива. Если же initialValue не передано, | |
то функция будет вызвана со значениями первого и второго элементов массива. Отсюда также | |
следует, что если начальное значение не передано, то функция вызывается на один раз меньше, | |
иначе ровно столько раз, сколько элементов в массиве. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var unsortedArray = [-10, 7, 29, 30, 5, -10, -70]; | |
computeProduct(unsortedArray); | |
/* | |
Сортирующая функция. Если её не передать методу sort, то элементы | |
сортируются путём преобразования их в строки и сравнения строк. | |
*/ | |
function sortIntegers(a, b) { | |
return a - b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Кастомный оператор new. Принимает функцию-конструктор и её аргументы. | |
Возвращает инстанс класса. | |
*/ | |
function newOperator(constr, args) { | |
// Создаём объект через Object.create (он наследуется от прототипа конструктора) | |
// Конструктор не вызывается! | |
var thisValue = Object.create(constr.prototype); | |
// Вызывает конструктор, указав в качестве this созданный объект |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function processTemp(tempString, dataObject) { | |
for(var key in dataObject) { | |
if (dataObject.hasOwnProperty(key)) { | |
tempString = tempString.replace(new RegExp('{' + key + '}', 'g'), dataObject[key]); | |
} | |
} | |
return tempString; | |
} | |
var str = 'Hello {name}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = []; | |
function flatten(val) { | |
if (Array.isArray(val)) { | |
for (var i = 0; i < val.length; i++) { | |
flatten(val[i]); | |
} | |
} else { | |
result.push(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var magicSort = function(arr) { | |
var temp = 0; | |
for (var i = 0; i < arr.length; i++) { | |
for (var j = 0; j < arr.length - 2; j++) { | |
if (j % 2 === 0 && arr[j] < arr[j + 2]) { | |
temp = arr[j]; | |
arr[j] = arr[j + 2]; | |
arr[j + 2] = temp; | |
} else if (j % 2 === 1 && arr[j] > arr[j + 2]) { |
OlderNewer