Skip to content

Commit

Permalink
fix(transducers): fix #310, update flatten/flattenWith
Browse files Browse the repository at this point in the history
- fix `flatten()`/`flattenWith()` return types
- update generics to allow specifying explicit result type, but
  use new `DeepArrayValue<A>` mapped type as default
- update `flattenWith()` predicate arg type to `any` since current
  restriction on top-level input type was (potentially) wrong for
  deeper levels. Also lift restriction and update pred's return type
  to `MaybeIterable<any>` (for same reason).
  • Loading branch information
postspectacular committed Aug 27, 2021
1 parent a309fac commit bfbd726
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
24 changes: 12 additions & 12 deletions packages/transducers/src/xform/flatten-with.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn, Nullable } from "@thi.ng/api";
import type { DeepArrayValue, Fn, Nullable } from "@thi.ng/api";
import { isIterable, isString } from "@thi.ng/checks";
import type { Reducer, Transducer } from "../api";
import { compR } from "../func/compr";
Expand Down Expand Up @@ -36,20 +36,20 @@ type MaybeIterable<T> = Nullable<Iterable<T>>;
* @param fn -
* @param src -
*/
export function flattenWith<T>(
fn: Fn<T, MaybeIterable<T>>
): Transducer<T | Iterable<T>, T>;
export function flattenWith<T>(
fn: Fn<T, MaybeIterable<T>>,
src: Iterable<T | Iterable<T>>
): IterableIterator<T>;
export function flattenWith<T>(
fn: Fn<T, MaybeIterable<T>>,
src?: Iterable<T | Iterable<T>>
export function flattenWith<A, B = DeepArrayValue<A>>(
fn: Fn<any, MaybeIterable<any>>
): Transducer<A, B>;
export function flattenWith<A, B = DeepArrayValue<A>>(
fn: Fn<any, MaybeIterable<any>>,
src: Iterable<A>
): IterableIterator<B>;
export function flattenWith<A>(
fn: Fn<any, MaybeIterable<any>>,
src?: Iterable<A>
): any {
return isIterable(src)
? iterator(flattenWith(fn), isString(src) ? <any>[src] : src)
: (rfn: Reducer<any, T>) => {
: (rfn: Reducer<any, A>) => {
const reduce = rfn[2];
const flatten = (acc: any, x: any) => {
const xx = fn(x);
Expand Down
11 changes: 7 additions & 4 deletions packages/transducers/src/xform/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DeepArrayValue } from "@thi.ng/api";
import { isNotStringAndIterable } from "@thi.ng/checks";
import type { Transducer } from "../api";
import { flattenWith } from "./flatten-with";
Expand All @@ -21,10 +22,12 @@ import { flattenWith } from "./flatten-with";
*
* @param src -
*/
export function flatten<T>(): Transducer<T | Iterable<T>, T>;
export function flatten<T>(src: Iterable<T | Iterable<T>>): IterableIterator<T>;
export function flatten<T>(src?: Iterable<T | Iterable<T>>): any {
return flattenWith(
export function flatten<A, B = DeepArrayValue<A>>(): Transducer<A, B>;
export function flatten<A, B = DeepArrayValue<A>>(
src: Iterable<A>
): IterableIterator<B>;
export function flatten(src?: Iterable<any>): any {
return flattenWith<any, any>(
(x) => (isNotStringAndIterable(x) ? x : undefined),
src!
);
Expand Down

0 comments on commit bfbd726

Please sign in to comment.