-
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.
- Loading branch information
Showing
70 changed files
with
12,874 additions
and
30,450 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Accumulator, Collector, enumerate } from '../core'; | ||
|
||
/** | ||
* Applies an accumulator function over an array. | ||
* @param accumulator - An accumulator function to be invoked on each element. | ||
* @param seed - The initial accumulator value. | ||
* @returns The transformed final accumulator value. | ||
*/ | ||
export function aggregate<T, TAcc, TRes = any>(accumulator: Accumulator<T, TAcc, TRes>): Collector<T, TRes>; | ||
export function aggregate<T, TAcc, TRes>(seed: TAcc, accumulator: Accumulator<T, TAcc, TRes>): Collector<T, TRes>; | ||
export function aggregate<T, TAcc, TRes>(...args: any[]): Collector<T, TRes> { | ||
return function(source) { | ||
let seed: T|TAcc|TRes; | ||
let accumulator: Accumulator<T, TAcc, TRes>; | ||
let idx = 0; | ||
|
||
const e = enumerate(source); | ||
|
||
if (args.length >= 2) { | ||
[ seed, accumulator ] = args; | ||
} else { | ||
accumulator = args[0]; | ||
|
||
if (e.moveNext()) { | ||
seed = e.current; | ||
|
||
// TODO: This is right? | ||
idx += 1; | ||
} | ||
} | ||
|
||
while(e.moveNext()) { | ||
seed = accumulator(seed as TAcc, e.current, idx++); | ||
} | ||
|
||
return seed as TRes; | ||
}; | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Selector, Collector } from '../core'; | ||
|
||
/** | ||
* Computes the average of an array of numbers that are obtained by invoking | ||
* a transform function on each element of the input array. | ||
* @param selector - A transform function to apply to each element. | ||
* @returns The average of the array. | ||
*/ | ||
export function average<T>(selector?: Selector<T, number>): Collector<T, number> { | ||
if (typeof selector != 'function') { | ||
selector = Number; | ||
} | ||
|
||
return function(source) { | ||
let len = 0; | ||
let acc = 0; | ||
|
||
for (let item of source) { | ||
acc += selector(item, len++); | ||
} | ||
|
||
return acc / len; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Collector, Predicate, enumerate } from '../core'; | ||
import { where } from '../projection'; | ||
|
||
/** | ||
* Returns a number that represents how many elements in the specified array satisfy a condition. | ||
* @param predicate - A function to test each element for a condition. | ||
* @returns A number that represents how many elements in the array satisfy the condition in the predicate function. | ||
*/ | ||
export function count<T>(predicate?: Predicate<T>): Collector<T, number> { | ||
return function(source) { | ||
if (typeof predicate == 'function') { | ||
source = where(predicate)(source); | ||
} | ||
|
||
const e = enumerate(source); | ||
let acc = 0; | ||
|
||
while (e.moveNext()) { | ||
acc += 1; | ||
} | ||
|
||
return acc; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from './aggregate'; | ||
export * from './average'; | ||
export * from './count'; | ||
export * from './max'; | ||
export * from './min'; | ||
export * from './sum'; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Selector, Collector, enumerate } from '../core'; | ||
import { select } from '../projection'; | ||
|
||
/** | ||
* Returns the maximum value in an array of numbers. Optionally with a element transform function. | ||
* @param selector - A transform function to apply to each element. | ||
* @returns A number that corresponds to the maximum value in the array. | ||
*/ | ||
export function max<T>(selector?: Selector<T, number>): Collector<T, number> { | ||
if (typeof selector == 'function') { | ||
selector = Number; | ||
} | ||
|
||
return function(source) { | ||
const e = enumerate<number>(select(selector)(source)); | ||
|
||
e.moveNext(); | ||
let maximum = e.current; | ||
|
||
while (e.moveNext()) { | ||
if (e.current > maximum) { | ||
maximum = e.current; | ||
} | ||
} | ||
|
||
return maximum; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Selector, Collector, enumerate } from '../core'; | ||
import { select } from '../projection'; | ||
|
||
/** | ||
* Returns the minimum value in an array of numbers. Optionally with a element transform function. | ||
* @param selector - A transform function to apply to each element. | ||
* @returns A number that corresponds to the minimum value in the array. | ||
*/ | ||
export function min<T>(selector?: Selector<T, number>): Collector<T, number> { | ||
if (typeof selector == 'function') { | ||
selector = Number; | ||
} | ||
|
||
return function(source) { | ||
const e = enumerate<number>(select(selector)(source)); | ||
|
||
e.moveNext(); | ||
let minimum = e.current; | ||
|
||
while (e.moveNext()) { | ||
if (e.current < minimum) { | ||
minimum = e.current; | ||
} | ||
} | ||
|
||
return minimum; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Selector, Collector } from '../core'; | ||
import { select, pipe } from '../projection'; | ||
|
||
/** | ||
* Computes the sum of the array of number values that are obtained by invoking | ||
* a transform function on each element of the input array. | ||
* @param transform - A transform function to apply to each element. | ||
* @returns The sum of the projected values. | ||
*/ | ||
export function sum<T>(transform?: Selector<T, number>): Collector<T, number> { | ||
if (typeof transform == 'function') { | ||
return pipe(select(transform), sum()); | ||
} | ||
|
||
return function(source) { | ||
let acc = 0; | ||
|
||
for (let item of source) { | ||
acc += Number(item); | ||
} | ||
|
||
return acc; | ||
}; | ||
} |
Oops, something went wrong.