N (Number)
Aggregation
Section titled “Aggregation”sum(nums: number[]): numbermean(nums: number[]): Option<number>median(nums: number[]): Option<number>variance(nums: number[]): Option<number>standardDeviation(nums: number[]): Option<number>min(nums: number[]): Option<number>max(nums: number[]): Option<number>minMax(nums: number[]): Option<readonly [number, number]>Every partial aggregate also has an explicit *OrUndefined variant. Use the
*NonEmpty variants when a readonly non-empty tuple proves a result exists,
and the sample-statistic *AtLeastTwo variants when two inputs are required.
Curried
Section titled “Curried”percentile(p: number): (nums: number[]) => Option<number>percentileOrUndefined(p: number): (nums: number[]) => number | undefinedpercentileNonEmpty(p: number): (nums: readonly [number, ...number[]]) => numberclamp(min: number, max: number): (value: number) => numberdotProduct(b: number[]): (a: number[]) => numberPredicates
Section titled “Predicates”isEven(n: number): booleanisOdd(n: number): booleanExamples
Section titled “Examples”import { pipe } from '@stopcock/fp'import * as A from '@stopcock/fp/array'import * as N from '@stopcock/fp/number'import * as O from '@stopcock/fp/option'
// stats on response timesconst times = [120, 95, 200, 88, 150, 300, 110] as constpipe(N.mean(times), O.getOrElse(() => 0)) // ~151.9N.medianNonEmpty(times) // 120N.standardDeviationNonEmpty(times) // ~68.6N.percentileNonEmpty(95)(times) // 300
// clamp user inputpipe(userAge, N.clamp(0, 150))
// average score, ignoring zerospipe( scores, A.filter((s) => s > 0), N.mean,)
// feature vector similarityN.dotProduct([0, 1, 1])([1, 0, 1]) // 1