-
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.
feat(reduce, reduceright): overloads that don't require seed value
to match JS reduce
- Loading branch information
1 parent
ab7e3a1
commit 594dd7e
Showing
4 changed files
with
95 additions
and
11 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,12 +1,34 @@ | ||
import { _aggregate } from './aggregate' | ||
import { deferP0 } from 'ts-functional-pipe' | ||
import { headTail } from '../helpers/headTail' | ||
|
||
export function _reduce<T, TOut>( | ||
src: Iterable<T>, | ||
aggFunc: (prev: TOut, curr: T, idx: number) => TOut, | ||
seed: TOut | ||
) { | ||
return _aggregate(src, seed, aggFunc) | ||
): TOut | ||
export function _reduce<T>(src: Iterable<T>, aggFunc: (prev: T, curr: T, idx: number) => T): T | ||
export function _reduce<T, TOut, TT extends T | TOut>( | ||
src: Iterable<T>, | ||
aggFunc: (prev: TT, curr: T, idx: number) => TT, | ||
seed?: TT | ||
): TT { | ||
if (typeof seed !== 'undefined') { | ||
return _aggregate(src, seed, aggFunc) | ||
} else { | ||
const [head, tail] = headTail(src) | ||
return _aggregate(tail, head as TT, aggFunc) | ||
} | ||
} | ||
|
||
export const reduce = deferP0(_reduce) | ||
export function reduce<T, TOut>( | ||
aggFunc: (prev: TOut, curr: T, idx: number) => TOut, | ||
seed: TOut | ||
): (source: Iterable<T>) => TOut | ||
export function reduce<T>(aggFunc: (prev: T, curr: T, idx: number) => T): (source: Iterable<T>) => T | ||
export function reduce<T, TOut, TT extends T | TOut>( | ||
aggFunc: (prev: TT, curr: T, idx: number) => TT, | ||
seed?: TT | ||
): (source: Iterable<T>) => TT { | ||
return (source: Iterable<T>) => _reduce(source, aggFunc, seed as TT) | ||
} |
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,11 +1,31 @@ | ||
import { aggregate } from './aggregate' | ||
import { deferP0, pp } from 'ts-functional-pipe' | ||
import { reverse } from './reverse' | ||
import { reduce } from './reduce' | ||
export function _reduceRight<T, TOut>( | ||
src: Iterable<T>, | ||
aggFunc: (prev: TOut, curr: T, idx: number) => TOut, | ||
seed: TOut | ||
) { | ||
return pp(src, reverse(), aggregate(seed, aggFunc)) | ||
): TOut | ||
export function _reduceRight<T>(src: Iterable<T>, aggFunc: (prev: T, curr: T, idx: number) => T): T | ||
export function _reduceRight<T, TOut, TT extends T | TOut>( | ||
src: Iterable<T>, | ||
aggFunc: (prev: TT, curr: T, idx: number) => TT, | ||
seed?: TT | ||
): TT { | ||
return pp(src, reverse(), reduce(aggFunc, seed as TT)) | ||
} | ||
|
||
export function reduceRight<T, TOut>( | ||
aggFunc: (prev: TOut, curr: T, idx: number) => TOut, | ||
seed: TOut | ||
): (source: Iterable<T>) => TOut | ||
export function reduceRight<T>( | ||
aggFunc: (prev: T, curr: T, idx: number) => T | ||
): (source: Iterable<T>) => T | ||
export function reduceRight<T, TOut, TT extends T | TOut>( | ||
aggFunc: (prev: TT, curr: T, idx: number) => TT, | ||
seed?: TT | ||
): (source: Iterable<T>) => TT { | ||
return (source: Iterable<T>) => _reduceRight(source, aggFunc, seed as TT) | ||
} | ||
export const reduceRight = deferP0(_reduceRight) |
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