Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 2635. Apply Transform Over Each Element in Array

[View Problem on LeetCode](https://leetcode.com/problems/apply-transform-over-each-element-in-array/)

We're about to get into a bunch of problems that essentially ask us to implement some built-in JavaScript function. This problem is asking us to implement mapping without using [`.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map), so of course the first thing we should do is get accepted using `.map`!

For a more serious solution, we can keep it simple with some kind of loop that builds the result.

One more thing worth practicing is implementing `.map` using [`.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce). Since pretty much anything can be done through `.reduce`, it was at one point popular in interviews to ask candidates to build some other operation without explicit loops, instead using only `.reduce`.

It's also possible to consider other built-in functions. For example, LeetCode didn't ask us not to use [`.flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)!

Once you've worked on the problem, check out [the full write-up and solution](solution.md)!

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] {
const res: TOut[] = [];

for (let i = 0; i < arr.length; ++i) {
res.push(fn(arr[i], i));
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] {
const res: TOut[] = [];

arr.forEach((element, index) => {
res.push(fn(element, index));
});

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] {
const res: TOut[] = [];

for (const [index, element] of arr.entries()) {
res.push(fn(element, index));
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function* lazyMap<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): Generator<TOut, void, void> {
for (const [index, element] of arr.entries()) {
yield fn(element, index);
}
}

const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] => Array.from(lazyMap(arr, fn));
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] {
const res: TOut[] = [];

const doMap = (index: number) => {
if (index === arr.length) {
return;
}

res.push(fn(arr[index], index));
doMap(index + 1);
};

doMap(0);

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] {
const res: TOut[] = [];

const doMap = () => {
if (res.length === arr.length) {
return;
}

res.push(fn(arr[res.length], res.length));
doMap();
};

doMap();

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function map<TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
res: TOut[] = [],
): TOut[] {
if (res.length === arr.length) {
return res;
}

res.push(fn(arr[res.length], res.length));
return map(arr, fn, res);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
const map = Function.prototype.call.bind(Array.prototype.map);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] => arr.map(fn);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
const map = Array.from;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] => Array.from(arr, fn);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] => arr.flatMap((element, index) => [fn(element, index)]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] => arr.flatMap(fn);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] =>
arr.reduce((res: TOut[], element, index) => [...res, fn(element, index)], []);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] =>
arr
.reduceRight((res: TOut[], element, index) => {
res.push(fn(element, index));
return res;
}, [])
.reverse();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] =>
arr.reduce(
(res: TOut[], element, index) => (res.push(fn(element, index)), res),
[],
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const map = <TIn, TOut>(
arr: readonly TIn[],
fn: (element: TIn, index: number) => TOut,
): TOut[] =>
arr.reduce((res: TOut[], element, index) => {
res.push(fn(element, index));
return res;
}, []);