-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unzipWith): add unzipWith (#113)
* feat(array): add unzipWith * docs(array): add unzipWith reference * perf: refactoring unzipWith bench * docs(array): add vitepress * Apply suggestions from code review * Update unzipWith.ts * Update src/array/unzipWith.ts * Update benchmarks/unzipWith.bench.ts * Update benchmarks/unzipWith.bench.ts * Update src/array/unzipWith.spec.ts * Update src/array/unzipWith.spec.ts --------- Co-authored-by: Sojin Park <raon0211@gmail.com> Co-authored-by: Sojin Park <raon0211@toss.im>
- Loading branch information
1 parent
c1d80ff
commit 834d50c
Showing
8 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { unzipWith as unzipWithToolkit } from 'es-toolkit'; | ||
import { unzipWith as unzipWithLodash } from 'lodash'; | ||
import { bench, describe } from 'vitest'; | ||
|
||
describe('unzipWith', () => { | ||
bench('es-toolkit/unzipWith', () => { | ||
unzipWithToolkit( | ||
[ | ||
[1000, 2000], | ||
[3000, 4000], | ||
[50000, 60000], | ||
], | ||
(a, b, c) => a + b + c | ||
); | ||
}); | ||
|
||
bench('lodash/unzipWith', () => { | ||
unzipWithLodash( | ||
[ | ||
[1000, 2000], | ||
[3000, 4000], | ||
[50000, 60000], | ||
], | ||
(a, b, c) => a + b + c | ||
); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# unzipWith | ||
|
||
결합되어 있는 2차원 배열을 1차원 배열로 풀고, `iteratee` 함수로 값을 변환해서 새로운 배열을 반환해요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function unzipWith<T, R>(target: readonly T[][], iteratee: (...args: T[]) => R): R[]; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `target` (`T[][]`): 결합을 풀고 변환할 배열이에요. | ||
- `iteratee` (`(...args: T[]) => R`): 결합이 풀린 배열을 변환할 함수예요. | ||
|
||
### 반환 값 | ||
|
||
(`R[]`): 결합을 풀고 변환된 값들로 만들어진 새로운 배열이에요. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
const nestedArray = [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]; | ||
const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3); | ||
// [9, 12] | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# unzipWith | ||
|
||
Unzips an array of arrays, applying an `iteratee` function to regrouped elements. | ||
|
||
## Signature | ||
|
||
```typescript | ||
function unzipWith<T, R>(target: T[][], iteratee: (...args: T[]) => R): R[]; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `target`(`T[][]`): The nested array to unzip. This is an array of arrays, where each inner array contains elements to be unzipped. | ||
- `iteratee` (`(...args: T[]) => R`): A function to transform the unzipped elements. | ||
|
||
### Returns | ||
|
||
(`R[]`): A new array of unzipped and transformed elements. | ||
|
||
## Examples | ||
|
||
```typescript | ||
const nestedArray = [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]; | ||
const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3); | ||
// [9, 12] | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { unzipWith } from './unzipWith'; | ||
import { zip } from './zip'; | ||
|
||
describe('unzipWith', () => { | ||
it('should unzip arrays correctly with an iteratee', () => { | ||
const zipped = zip([10, 20, 30], [40, 50, 60], [70, 80, 90]); | ||
const result = unzipWith(zipped, (item, item2, item3) => item + item2 + item3); | ||
|
||
expect(result).toEqual([60, 150, 240]); | ||
}); | ||
|
||
it('should handle arrays of different lengths', () => { | ||
const zipped = zip([1, 20, 300, 4000], [100, 200, 300]); | ||
const result = unzipWith(zipped, (item, item2, item3, item4) => item + item2 + item3 + item4); | ||
|
||
expect(result).toEqual([4321, NaN]); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Unzips an array of arrays, applying an `iteratee` function to regrouped elements. | ||
* | ||
* @template T, R | ||
* @param {T[][]} target - The nested array to unzip. This is an array of arrays, | ||
* where each inner array contains elements to be unzipped. | ||
* @param {(...args: T[]) => R} iteratee - A function to transform the unzipped elements. | ||
* @returns {R[]} A new array of unzipped and transformed elements. | ||
* | ||
* @example | ||
* const nestedArray = [[1, 2], [3, 4], [5, 6]]; | ||
* const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3); | ||
* // result will be [9, 12] | ||
*/ | ||
export function unzipWith<T, R>(target: readonly T[][], iteratee: (...args: T[]) => R): R[] { | ||
const maxLength = Math.max(...target.map(innerArray => innerArray.length)); | ||
const result: R[] = new Array(maxLength); | ||
|
||
for (let i = 0; i < maxLength; i++) { | ||
const group = new Array(target.length); | ||
|
||
for (let j = 0; j < target.length; j++) { | ||
group[j] = target[j][i]; | ||
} | ||
|
||
result[i] = iteratee(...group); | ||
} | ||
|
||
return result; | ||
} |