-
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
242 additions
and
36 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
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,52 @@ | ||
--- | ||
title: cartesian | ||
description: cartesian | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
|
||
## Description | ||
The `cartesian` function calculates the Cartesian product of multiple arrays. It takes a variable number of arrays as arguments and returns an array of arrays representing all possible combinations of elements from the input arrays. | ||
|
||
## Parameters | ||
- `...arrays`: Arrays of elements. | ||
|
||
## Returns | ||
An array of arrays representing the Cartesian product of the input arrays. | ||
|
||
## Type | ||
|
||
```ts | ||
type cartesian = (dimensions: [number,]) => <A>(a: A[]) => [A][]; | ||
type cartesian = (dimensions: [number, number,]) => <A>(a: A[]) => [A][][]; | ||
//... | ||
type cartesian = (dimensions: [number, number, number, number, number, number, number, number]) => <A>(a: A[]) => [A][][][][][][][][]; | ||
``` | ||
|
||
|
||
## Example | ||
```typescript | ||
import { cartesian } from './path/to/cartesian'; | ||
|
||
const result = cartesian([1, 2], ['a', 'b', 'c'], [true, false]); | ||
|
||
console.log(result); | ||
// Output: [ | ||
// [ | ||
// [[1,"a",true],[1,"a",false]], | ||
// [[1,"b",true],[1,"b",false]], | ||
// [[1,"c",true],[1,"c",false]] | ||
// ], | ||
// [ | ||
// [[2,"a",true],[2,"a",false]], | ||
// [[2,"b",true],[2,"b",false]], | ||
// [[2,"c",true],[2,"c",false]] | ||
// ] | ||
// ] | ||
``` | ||
|
||
|
||
|
||
## See Also | ||
|
||
- [allCombinations](./allCombinations) | ||
- [reshape](./reshape) |
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,50 @@ | ||
--- | ||
title: reshape | ||
description: reshape | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
|
||
## Description | ||
The `reshape` function is designed to reshape a flat array into a multi-dimensional array according to the specified dimensions. It takes an array of dimensions and returns a function that accepts a flat array as input and returns a multi-dimensional array based on the provided dimensions. | ||
|
||
## Parameters | ||
- `dimensions`: An array of numbers representing the desired dimensions for the output array. | ||
|
||
## Returns | ||
A function that takes an array as input and returns a multi-dimensional array based on the specified dimensions. | ||
|
||
## Throws | ||
- `Error`: If the total size of the dimensions does not match the length of the input array. | ||
|
||
## Type | ||
|
||
```ts | ||
type reshape = (dimensions: [number,]) => <A>(a: A[]) => [A][]; | ||
type reshape = (dimensions: [number, number,]) => <A>(a: A[]) => [A][][]; | ||
//... | ||
type reshape = (dimensions: [number, number, number, number, number, number, number, number]) => <A>(a: A[]) => [A][][][][][][][][]; | ||
``` | ||
|
||
## Example | ||
|
||
```typescript | ||
import { reshape } from 'fnxt/array'; | ||
|
||
const dimensions = [2, 3, 2]; | ||
const fn = reshape(dimensions); | ||
|
||
const flatArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | ||
const multiDimensionalArray = fn(flatArray); | ||
|
||
console.log(multiDimensionalArray); | ||
// Output: [ | ||
// [[1, 2], [3, 4], [5, 6]], | ||
// [[7, 8], [9, 10], [11, 12]] | ||
// ] | ||
``` | ||
|
||
|
||
## See Also | ||
|
||
- [allCombinations](./allCombinations) | ||
- [cartesian](./cartesian) |
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,60 @@ | ||
import {expect} from 'chai'; | ||
import {reshape} from '../../../../src/array/'; | ||
|
||
describe('reshape', () => { | ||
|
||
|
||
it('should reshape []', () => { | ||
|
||
const fn = reshape([]); | ||
expect(fn([])).to.eql([]); | ||
}); | ||
|
||
it('should reshape [1]', () => { | ||
|
||
const fn = reshape([1]); | ||
expect(fn([1])).to.eql([1]); | ||
}); | ||
|
||
it('should reshape [2]', () => { | ||
|
||
const fn = reshape([1, 2]); | ||
expect(fn([1, 2])).to.eql([ | ||
[1, 2], | ||
]); | ||
}); | ||
|
||
it('should reshape [1,2]', () => { | ||
|
||
const fn = reshape([1, 2]); | ||
expect(fn([1, 2])).to.eql([ | ||
[1, 2], | ||
]); | ||
}); | ||
it('should reshape [2,3,2]', () => { | ||
|
||
const fn = reshape([2, 3, 2]); | ||
expect(fn([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])).to.eql( | ||
[ | ||
[[1, 2], [3, 4], [5, 6]], | ||
[[7, 8], [9, 10], [11, 12]] | ||
] | ||
); | ||
}); | ||
it('should reshape [4, 3, 1]', () => { | ||
|
||
const fn = reshape([4, 3, 1]); | ||
expect(fn([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])).to.eql( | ||
[[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]], [[10], [11], [12]]] | ||
); | ||
}); | ||
it('should reshape [4, 3, 1]', () => { | ||
|
||
const fn = reshape([4, 3, 1]); | ||
expect(() => fn([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])).to.throws(); | ||
}); | ||
|
||
|
||
}); | ||
|
||
|
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,31 @@ | ||
import {chunkBySize} from './chunkBySize'; | ||
|
||
export function reshape(dimensions: [number,]): <A>(a: A[]) => [A][]; | ||
export function reshape(dimensions: [number, number,]): <A>(a: A[]) => [A][][]; | ||
export function reshape(dimensions: [number, number, number,]): <A>(a: A[]) => [A][][][]; | ||
export function reshape(dimensions: [number, number, number, number,]): <A>(a: A[]) => [A][][][][]; | ||
export function reshape(dimensions: [number, number, number, number, number,]): <A>(a: A[]) => [A][][][][][]; | ||
export function reshape(dimensions: [number, number, number, number, number, number,]): <A>(a: A[]) => [A][][][][][][]; | ||
export function reshape(dimensions: [number, number, number, number, number, number, number,]): <A>(a: A[]) => [A][][][][][][][]; | ||
export function reshape(dimensions: [number, number, number, number, number, number, number, number]): <A>(a: A[]) => [A][][][][][][][][]; | ||
export function reshape(dimensions: number[]): <A>(a: A[]) => any[]; | ||
|
||
export function reshape(dimensions: number[]) { | ||
const size = dimensions.length ? dimensions.reduce((a, b) => a * b, 1) : 0; | ||
return (arrays: unknown[]): unknown[] => { | ||
if (size !== arrays.length) { | ||
throw new Error(dimensions.join(' * ') + ' must equal array length'); | ||
} | ||
if (size === 0) { | ||
return []; | ||
} | ||
// @ts-ignore | ||
let res: any[] = arrays; | ||
for (let i = dimensions.length - 1; i >= 0; i--) { | ||
res = chunkBySize(dimensions[i])(res); | ||
} | ||
return res[0]; | ||
|
||
}; | ||
} | ||
|