Skip to content

Commit

Permalink
fix(transducers): add generics for compR(), fix types in mapNth()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 18, 2018
1 parent 4c014b1 commit 3b7c9d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ itself. Returns nothing.

#### `mapKeys(keys: IObjectOf<(x: any) => any>, copy?: boolean): Transducer<any, any>`

#### `mapNth<A, B>(n: number, offset: number, fn: (x: A) => B): Transducer<A, B>`
#### `mapNth<A, B>(n: number, offset?: number, fn: (x: A) => B): Transducer<A, A | B>`

#### `mapVals<A, B>(fn: (v: A) => B, copy = true): Transducer<IObjectOf<A>, IObjectOf<B>>`

Expand Down
26 changes: 24 additions & 2 deletions packages/transducers/src/func/compr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { Reducer } from "../api";
import { Reduced } from "../reduced";

export function compR(rfn: Reducer<any, any>, fn: (acc: any, x: any) => any) {
return <Reducer<any, any>>[rfn[0], rfn[1], fn];
/**
* Reducer composition helper. Takes existing reducer `rfn` (a 3-tuple)
* and a reducing function `fn`. Returns a new reducer tuple of this
* form:
*
* ```
* [rfn[0], rfn[1], fn]
* ```
*
* `rfn[2]` reduces values of type `B` into an accumulator of type `A`.
* `fn` accepts values of type `C` and produces interim results of type
* `B`, which are then (possibly) passed to the "inner" `rfn[2]`
* function. Therefore the resulting reducer takes inputs of `C` and an
* accumulator of type `A`.
*
* It is assumed that `fn` internally calls `rfn[2]` to pass its own
* results for further processing by the nested reducer `rfn`.
*
* @param rfn
* @param fn
*/
export function compR<A, B, C>(rfn: Reducer<A, B>, fn: (acc: A, x: C) => A | Reduced<A>) {
return <Reducer<A, C>>[rfn[0], rfn[1], fn];
}
8 changes: 4 additions & 4 deletions packages/transducers/src/xform/map-nth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Reducer, Transducer } from "../api";
import { compR } from "../func/compr";

export function mapNth<A, B>(n: number, fn: (x: A) => B): Transducer<A, B>;
export function mapNth<A, B>(n: number, offset: number, fn: (x: A) => B): Transducer<A, B>;
export function mapNth<A, B>(...args: any[]): Transducer<A, B> {
export function mapNth<A, B>(n: number, fn: (x: A) => B): Transducer<A, A | B>;
export function mapNth<A, B>(n: number, offset: number, fn: (x: A) => B): Transducer<A, A | B>;
export function mapNth<A, B>(...args: any[]): Transducer<A, A | B> {
let n = args[0] - 1, offset, fn;
if (typeof args[1] === "number") {
offset = args[1];
Expand All @@ -12,7 +12,7 @@ export function mapNth<A, B>(...args: any[]): Transducer<A, B> {
fn = args[1];
offset = 0;
}
return (rfn: Reducer<any, B>) => {
return (rfn: Reducer<any, A | B>) => {
const r = rfn[2];
let skip = 0, off = offset;
return compR(rfn,
Expand Down

0 comments on commit 3b7c9d9

Please sign in to comment.