Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -454,6 +454,20 @@ function map<TIn, TOut>(
}
```

Also, since the output is the same size as the input, you could modify the array to save space:

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

```typescript []
function map<TIn>(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] {
for (let i = 0; i < arr.length; ++i) {
arr[i] = fn(arr[i], i);
}

return arr;
}
```

## Answers to Bonus Questions

1. **What is the significance of the `void`s in `Generator<number, void, void>`?**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function map<TIn>(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] {
for (let i = 0; i < arr.length; ++i) {
arr[i] = fn(arr[i], i);
}

return arr;
}
Loading