each-until.js
js
const eachUntil = (f, arr) => {
arr.some((val, ...args) => {
return f(val, ...args)
})
}
eachUntil(a => {
console.log(a);
return a;
}, [0, false, undefined, null, 1, 2, 3])
// 0
// false
// undefined
// null
// 1
excute-sequantal-task.js
js
const excuteSequantalTask = (arr, delay, index = 0) => {
setTimeout(() => {
const cb = arr[index];
const nextIndex = index + 1;
cb();
if (arr[nextIndex]) {
excuteSequantalTask(arr, delay, nextIndex);
}
}, delay);
};
excuteSequantalTask([
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
() => console.log(Date.now()),
], 1000);
replace-item-at-index.js
js
const replaceItemAtIndex = (arr, index, newValue) => {
return [...arr.slice(0, index), newValue, ...arr.slice(index + 1)];
};
remove-item-at-index.js
js
const removeItemAtIndex = (arr, index) => {
return [...arr.slice(0, index), ...arr.slice(index + 1)];
};
chunk.js
js
const chunk = (arr, size) => {
if (size === 0) return arr;
return Array.from({length: Math.ceil(arr.length / size)}, (v, i) =>
arr.slice(i * size, i * size + size)
);
};
console.log(chunk([0, 1, 2, 3], 0)) // [0, 1, 2, 3]
console.log(chunk([0, 1, 2, 3], 1)) // [[0], [1], [2], [3]]
console.log(chunk([0, 1, 2, 3], 2)) // [[0, 1], [2, 3]]
console.log(chunk([0, 1, 2, 3], 3)) // [[0, 1, 2], [3]]
console.log(chunk([0, 1, 2, 3], 4)) // [[0, 1, 2, 3]]
console.log(chunk([0, 1, 2, 3], 5)) // [[0, 1, 2, 3]]
first.js
js
const first = arr => arr.slice(0, 1)
first([]) // []
first([1, 2, 3]) // [1]
last.js
js
const last = arr => {
const len = arr.length
return arr.slice(len - 1, len)
}
last([]) // []
last([1, 2, 3]) // [3]
range.js
js
const range = length => Array.from({length}, (_, i) => i)
range(10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
remove-item.js
js
const arr = [0, 1, 2]
arr.splice(1, 2)
console.log(arr) // [0]
repeat.js
js
// 지정한 숫자만큼 반복하며, 주어진 함수의 값이 각 요소에 할당되어
// 새로운 배열을 반환한다.
const repeat = (times, fn) => Array
.from({length: times}, (_, v) => v)
.map(fn)
repeat(3, i => i * 10)
sort.js
js
const arr = [0, 2, 3, 1]
arr.sort((a, b) => a - b)
console.log('오름차순', ...arr)
arr.sort((a, b) => b - a)
console.log('내름차순', ...arr)
take.js
js
const take = (n, arr) => arr.slice(0, n)
take(0, [1, 2, 3]) // []
take(2, [1, 2, 3]) // [1, 2]
console.log(take(4, [1, 2])) // [1, 2, 3]
take-until.js
js
const takeUntil = (f, arr) => {
const newArr = []
arr.some((val, ...args) => {
newArr.push(val)
return f(val, ...args)
})
return newArr
}
takeUntil(a => a, [0, false, undefined, null, 1, 2, 3])
// [0, false, undefined, null, 1]
take-while.js
js
const takeWhile = (f, arr) => {
const newArr = []
arr.some((val, ...args) => {
const result = !f(val, ...args)
result || newArr.push(val)
return result
})
return newArr
}
takeWhile(a => a, [1, 2, 3, 0, 4, 5])
/// [1, 2, 3]
array-booter.js
js
const range = length => Array.from({length})
const list = range(10000000)
// 1 Phase
{
const f = list => list
.map(a => a + 1)
.map(a => a + 1)
.filter(a => a % 2)
console.time('map+filter')
f(list)
console.timeEnd('map+filter')
// map+filter: 5832.8291015625ms
}
// 2 Phase
{
const magic = f => list => list.flatMap(x => f([x]))
const f = list => list
.map(a => a + 1)
.map(a => a + 1)
.filter(a => a % 2)
const f2 = magic(f)
console.time('map+filter+flatMap')
f2(list)
console.timeEnd('map+filter+flatMap')
// map+filter+flatMap: 3426.182861328125ms
}
// 3 Phase
{
const magic = f => list => list.flatMap(x => f([x]))
const f = list => list
.map(a => a + 1)
.map(a => a + 1)
.filter(a => a % 2)
const f2 = magic(f)
Array.prototype.flatMap = function (f) {
var i = -1, tl = this.length, res = [], b, j, bl
while (++i < tl) {
if (Array.isArray(b = f(this[i], i, this))) {
j = -1, bl = b.length
while (++j < bl) res.push(b[j])
} else res.push(b)
}
return res
}
console.time('map+filter+custom flatMap')
f2(list)
console.timeEnd('map+filter+custom flatMap')
// map+filter+custom flatMap: 2705.951904296875ms
}