-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
58 additions
and
49 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 |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import {concat} from "../_platform/slice"; | ||
import {map} from "./map"; | ||
import {MapOp, Slice} from "../types"; | ||
import type {Ternary} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Map a function over all the elements of a container of containers and concatenate the results. | ||
*/ | ||
concatMap = <T = any>(fn: MapOp, arr: Slice<T>[]): any[] => | ||
concatMap = <T=any, TS extends any[]=any[]>( | ||
fn: Ternary<T, number, TS[]>, | ||
arr: TS[] | ||
): ReturnType<typeof fn>[] => | ||
concat(map(fn, arr)), | ||
|
||
$concatMap = <T = any>(fn: MapOp) => | ||
(arr: T[][]): any[] => | ||
/** | ||
* Curried version of `concatMap`. | ||
*/ | ||
$concatMap = <T=any, TS extends any[]=any[]>( | ||
fn: Ternary<T, number, TS[]> | ||
) => | ||
(arr: TS[]): ReturnType<typeof fn>[] => | ||
concatMap(fn, arr) | ||
|
||
; |
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 |
---|---|---|
@@ -1,37 +1,39 @@ | ||
import {MapAccumOp, Slice} from "../types"; | ||
import {Quaternary, Slice} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Performs a map and a reduce, all in one (from right-to-left), on each item, and returns the result of the reduce, | ||
* and the map, as a tuple. | ||
* Performs a map and a reduce, all in one (from right-to-left), on each item in the given array, and | ||
* returns the result of the reduce, and the map, as a tuple, respectively. | ||
*/ | ||
mapAccumR = <A, B, C>( | ||
op: MapAccumOp<A, B, C>, | ||
zero: A, | ||
xs: Slice<B> | ||
): [A, Slice<C>] => { | ||
const limit = xs.length; | ||
mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>( | ||
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>, | ||
zero: AccumVal, | ||
bs: Bs | ||
): [AccumVal, MapOfBs] => { | ||
const limit = bs.length; | ||
|
||
if (!limit) return [zero, xs.slice(0) as unknown as Slice<C>]; | ||
if (!limit) return [zero, bs.slice(0) as unknown as MapOfBs]; | ||
|
||
const mapped = []; | ||
|
||
let agg = zero; | ||
|
||
for (let i = limit - 1; i >= 0; i--) { | ||
const tuple = op(agg, xs[i] as B, i); | ||
const tuple = op(agg, bs[i] as B, i, bs); | ||
agg = tuple[0]; | ||
mapped.push(tuple[1]); | ||
} | ||
|
||
return [agg, mapped]; | ||
return [agg, mapped as unknown as MapOfBs]; | ||
}, | ||
|
||
/** | ||
* Curried version of `mapAccumR`. | ||
*/ | ||
$mapAccumR = <A, B, C>(op: MapAccumOp<A, B, C>) => | ||
(zero: A) => | ||
(xs: Slice<B>): [A, Slice] => | ||
mapAccumR(op, zero, xs); | ||
$mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>( | ||
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>, | ||
) => | ||
(zero: AccumVal) => | ||
(bs: Bs): [AccumVal, MapOfBs] => | ||
mapAccumR(op, zero, bs); |
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