-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
161 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,49 @@ | ||
--- | ||
title: pairwiseWith | ||
description: pairwiseWith | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
Creates elements by combining adjacent elements in an array. | ||
|
||
## Parameters | ||
|
||
`fn` (Reduction): A reduction function that takes two elements of the array and returns a single value. | ||
|
||
`array` (Array): An array of elements for which pairs of adjacent elements are to be created. | ||
|
||
## Returns | ||
|
||
A new array containing the result of applying the reduction function to pairs of adjacent elements. | ||
|
||
## Example | ||
|
||
```ts | ||
import { pairwiseWith } from './pairwiseWith'; | ||
|
||
const inputArray = [1, 2, 3, 4, 5]; | ||
const sumReducer = (a, b) => a + b; | ||
|
||
const resultArray = pairwiseWith(sumReducer)(inputArray); | ||
// resultArray will be [3, 5, 7, 9] | ||
``` | ||
|
||
## Notes | ||
|
||
- The `pairwiseWith` function applies the provided reduction function to pairs of adjacent elements in the input array. | ||
|
||
- The input array remains unaltered, and a new array is returned. | ||
|
||
- If the input array has less than two elements, an empty array is returned since there are no adjacent elements to pair. | ||
|
||
- The reduction function should take two arguments and return a value. | ||
|
||
- This function does not modify the original array. | ||
|
||
|
||
|
||
## See Also | ||
|
||
- [map](./map) | ||
- [reduce](./reduce) | ||
- [pairwise](./pairwise) | ||
- [zipWith](./zipWith) |
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,41 @@ | ||
--- | ||
title: zipWith | ||
description: zipWith | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
Combines two arrays element-wise using a specified binary function. | ||
|
||
## Signature | ||
|
||
```ts | ||
type zipWith = <E, F, R>(mapper: BinaryFunction<E, F, R>) => (arr1: E[]) => (arr2: F[]) => Array<R> | ||
``` | ||
## Parameters | ||
`mapper` (BinaryFunction): A binary function that takes elements from both arrays and returns a value. | ||
`arr1` (Array): The first array to be zipped. | ||
`arr2` (Array): The second array to be zipped. | ||
## Returns | ||
A new array containing the result of applying the `mapper` function to pairs of elements from `arr1` and `arr2`. | ||
```ts | ||
import { zipWith } from './zipWith'; | ||
|
||
const array1 = [1, 2, 3]; | ||
const array2 = ['a', 'b', 'c']; | ||
const combineElements = (a, b) => `${a}-${b}`; | ||
|
||
const resultArray = zipWith(combineElements)(array1)(array2); | ||
// resultArray will be ['1-a', '2-b', '3-c'] | ||
``` | ||
|
||
## See Also | ||
- [map](./map) | ||
- [reduce](./reduce) | ||
- [zip](./zip) | ||
- [pairwiseWith](./pairwiseWith) |
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,18 @@ | ||
import {expect} from 'chai'; | ||
import {pairwiseWith} from '../../../../src/array'; | ||
import {checkThrow} from '../../../support/checkThrow'; | ||
|
||
describe('pairwiseWith', () => { | ||
it('should pairwiseWith', () => { | ||
const pairwiseSum = pairwiseWith<number>((a:number, b:number) => a + b); | ||
expect(pairwiseSum([1, 2, 3, 4])).to.eql([3, 5, 7]); | ||
expect(pairwiseSum([1,2])).to.eql([3]); | ||
expect(pairwiseSum([1])).to.eql([]); | ||
expect(pairwiseSum([])).to.eql([]); | ||
}); | ||
|
||
|
||
it('should throw if null or undefined', () => { | ||
checkThrow(pairwiseWith<number>((a, b) => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {expect} from 'chai'; | ||
import {checkThrow} from '../../../support/checkThrow'; | ||
import {zipWith} from '../../../../src/array'; | ||
|
||
describe('zipWith', () => { | ||
|
||
|
||
it('should zipWith', () => { | ||
const fn = zipWith((a: number, b: number) => a + b)([1, 2, 3]); | ||
expect(fn([4, 5, 6])).to.eql([5, 7, 9],); | ||
}); | ||
|
||
it('should zipWith empty', () => { | ||
const fn = zipWith((a: number, b: number) => a + b)([]); | ||
expect(fn([])).to.eql([]); | ||
}); | ||
it('should not zipWith if not same size', () => { | ||
const fn = zipWith((a: number, b: number) => a + b)([1, 2]); | ||
expect(() => fn([2])).to.throw(); | ||
}); | ||
|
||
|
||
it('should throw if null or undefined', () => { | ||
const fn = zipWith((a: number, b: number) => a + b)([1]); | ||
checkThrow(fn); | ||
}); | ||
}); |
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,9 @@ | ||
import {Reduction} from 'fnxt/fnxt-types'; | ||
|
||
export const pairwiseWith = <E>(fn: Reduction<E>) => (array: E[]): E[] => { | ||
const result: E[] = []; | ||
for (let i = 1; i < array.length; i++) { | ||
result.push(fn(array[i - 1], array[i])); | ||
} | ||
return result; | ||
}; |
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,12 @@ | ||
import {BinaryFunction} from 'fnxt/fnxt-types'; | ||
|
||
export const zipWith = <E, F, R>(mapper: BinaryFunction<E, F, R>) => (arr1: E[]) => (arr2: F[]): Array<R> => { | ||
if (arr1.length !== arr2.length) { | ||
throw new Error('Input arrays must have equal lengths'); | ||
} | ||
const result: Array<R> = []; | ||
for (let i = 0; i < arr1.length; i++) { | ||
result.push(mapper(arr1[i], arr2[i])); | ||
} | ||
return result; | ||
}; |