import { PositiveExponent } from "../exponent" ; import { GenericMeasure } from "./genericMeasure" ; import { NthRootUnit , RadicandUnit , Unit } from "./unitTypeArithmetic" ; import { nthRootUnit } from "./unitValueArithmetic" ; /** A function which applies a symbol prefix and multiplier to a given measure. */ export type PrefixFn < N = number > = { < U extends Unit > ( measure ) : GenericMeasure < N , U > ; } ; /** A function which transforms a single measure into another measure with the same unit. */ export type UnaryFn < N = number > = { < U extends Unit > ( x ) : GenericMeasure < N , U > ; } ; /** A function which takes the Rth root of a measure's value and unit. */ export type NthRootFn < R extends PositiveExponent , N = number > = { < U extends RadicandUnit < R > > ( x ) : GenericMeasure < N , NthRootUnit < U , R > > ; } ; /** A function which transforms two measures with same unit into a single measure with the same unit. */ export type BinaryFn < N = number > = { < U extends Unit > ( left , right ) : GenericMeasure < N , U > ; } ; /** A function which transforms one or more measure with the same unit into a single measure with the same unit. */ export type SpreadFn < N = number > = { < U extends Unit > ( first , ... rest ) : GenericMeasure < N , U > ; } ; param fn returns export function wrapUnaryFn < N > ( fn ) { return x => x . unsafeMap ( fn ) ; } param nthRoot param n returns export function wrapRootFn < N , R extends PositiveExponent > ( nthRoot , n ) { return x => x . unsafeMap ( nthRoot , unit => nthRootUnit ( unit , n ) ) ; } param fn returns export function wrapBinaryFn < N > ( fn ) { return ( left , right ) => left . unsafeMap ( lValue => fn ( lValue , right . value ) ) ; } param fn returns export function wrapSpreadFn < N > ( fn ) { return ( first , ... rest ) => { const measureValues = [ first , ... rest ] . map ( m => m . value ) ; const newValue = fn ( ... measureValues ) ; return first . unsafeMap ( ( ) => newValue ) ; } ; } param fn returns export function wrapReducerFn < N > ( fn ) { return ( first , ... rest ) => { const values = rest . map ( m => m . value ) ; return first . unsafeMap ( ( ) => values . reduce ( fn , first . value ) ) ; } ; }