Skip to content

Commit

Permalink
#57 - 'list/utils' module curried functions converted to idiomatic cu…
Browse files Browse the repository at this point in the history
…rrying.
  • Loading branch information
elycruz committed Jul 30, 2022
1 parent 89d1262 commit 5b1a6d0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
6 changes: 4 additions & 2 deletions packages/fjl/src/list/utils/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {$reduceUntil, reduceUntil} from "./reduceUntil";
import {reduceUntil} from "./reduceUntil";
import {alwaysFalse} from "../../boolean/alwaysFalse";
import {ReduceOp, Indexable} from "../../types";

Expand All @@ -17,6 +17,8 @@ export const
/**
* Curried `reduce` combinator.
*/
$reduce = $reduceUntil(alwaysFalse)
$reduce = <T, RetT>(op: ReduceOp<T, Indexable<T>, RetT>) =>
(agg: RetT) =>
(xs: Indexable<T>): RetT => reduceUntil(alwaysFalse, op, agg, xs)

;
7 changes: 5 additions & 2 deletions packages/fjl/src/list/utils/reduceRight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {$reduceUntilRight, reduceUntilRight} from "./reduceUntilRight";
import {reduceUntilRight} from "./reduceUntilRight";
import {alwaysFalse} from "../../boolean/alwaysFalse";
import {ReduceOp, Indexable} from "../../types";

Expand All @@ -12,6 +12,9 @@ export const
agg: RetT, xs: Indexable<T>): RetT =>
reduceUntilRight(alwaysFalse, op, agg, xs),

$reduceRight = $reduceUntilRight(alwaysFalse)
$reduceRight = <T, RetT>(op: ReduceOp<T, Indexable<T>, RetT>) =>
(agg: RetT) =>
(xs: Indexable<T>): RetT =>
reduceUntilRight(alwaysFalse, op, agg, xs)

;
10 changes: 4 additions & 6 deletions packages/fjl/src/list/utils/reduceUntil.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {curry, CurryOf4} from "../../function/curry";
import {ReduceOp, PredForIndexable, Indexable, Lengthable} from "../../types";
import {length} from "../length";

export type ReduceUntil<T1, RetT> = CurryOf4<PredForIndexable<T1>,
ReduceOp<T1, Indexable<T1>, RetT>, // @todo Refactor `ReduceOp`
RetT, Indexable<T1>, RetT>;

export const

/**
Expand Down Expand Up @@ -33,6 +28,9 @@ export const
/**
* Reduces a slice until predicate returns `true`.
*/
$reduceUntil = curry(reduceUntil) as ReduceUntil<any, any>
$reduceUntil = <T, RetT>(pred: PredForIndexable<T>) =>
(op: ReduceOp<T, Indexable<T>, RetT>) =>
(agg: RetT) =>
(xs: Indexable<T>): RetT => reduceUntil(pred, op, agg, xs)

;
10 changes: 6 additions & 4 deletions packages/fjl/src/list/utils/reduceUntilRight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry, CurryOf4} from "../../function/curry";
import {length} from "../length";
import {Indexable, ReduceOp, PredForIndexable} from "../../types";

Expand Down Expand Up @@ -26,9 +25,12 @@ export const
return result;
};

export type ReduceUntilRight = typeof reduceUntilRight;

/**
* Curried version of `$reduceUntilRight`.
*/
export const $reduceUntilRight = curry(reduceUntilRight) as CurryOf4;
export const $reduceUntilRight = <T = any, RetT = any>(pred: PredForIndexable<T>) =>
(op: ReduceOp<T, Indexable<T>, RetT>) =>
(agg: RetT) =>
(arr: Indexable<T>): RetT =>
reduceUntilRight(pred, op, agg, arr)
;
4 changes: 2 additions & 2 deletions packages/fjl/src/list/utils/sliceFrom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry, CurryOf2} from "../../function/curry";
import {slice} from "../../platform/slice";
import {Slice} from "../../types";

Expand All @@ -12,5 +11,6 @@ export const
/**
* Curried version of `sliceFrom`.
*/
$sliceFrom = curry(sliceFrom) as CurryOf2<number, Slice, Slice>
$sliceFrom = <T = any, T2 extends Slice<T> = Slice<T>>(startInd: number) =>
(xs: T2): T2 => sliceFrom(startInd, xs)
;
4 changes: 2 additions & 2 deletions packages/fjl/src/list/utils/sliceTo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry, CurryOf2} from "../../function/curry";
import {slice} from "../../platform/slice";
import {Slice} from "../../types";

Expand All @@ -12,6 +11,7 @@ export const
/**
* Curried version of `sliceTo`.
*/
$sliceTo = curry(sliceTo) as CurryOf2<number, Slice, Slice>
$sliceTo = <T = any, T2 extends Slice<T> = Slice<T>>(toInd: number) =>
(xs: T2): T2 => sliceTo(toInd, xs)

;
5 changes: 3 additions & 2 deletions packages/fjl/src/list/utils/swap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry, CurryOf3} from "../../function";
import {sliceCopy} from "./sliceCopy";

export const
Expand All @@ -17,6 +16,8 @@ export const
/**
* Returns an array with the given indices swapped.
*/
$swap = curry(swap) as CurryOf3<number, number, any[], any[]>
$swap = <T>(ind1: number) =>
(ind2: number) =>
(list: T[]): T[] => swap(ind1, ind2, list)

;
6 changes: 3 additions & 3 deletions packages/fjl/src/list/utils/toShortest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {curry2, CurryOf2} from "../../function/curry";
import {lengths} from "./lengths";
import {map} from "../map";
import {sliceTo} from "./sliceTo";
Expand All @@ -21,8 +20,9 @@ export const
* Returns a list of lists trimmed to the shortest length in given list of lists.
* @background This method is used by the `zip*` functions to achieve their
* 'slice to smallest' functionality.
* @curried At two or more.
* @curried Upto two.
*/
$toShortest = curry2(toShortest) as CurryOf2<any, any, any[][]>
$toShortest = <T = any, T2 extends Slice<T> = Slice<T>>(list1: T2) =>
(list2: T2, ...lists: T2[]): T2[] => toShortest(list1, list2, ...lists)

;

0 comments on commit 5b1a6d0

Please sign in to comment.