import { List , ascend , slice , sort } from '@typed/list' ; import { Maybe , Nothing } from '@typed/maybe' ; import { mean } from '../mean' ; name export function median ( numbers ) { const length = numbers . length ; if ( length === 0 ) return Nothing ; const width = 2 - ( length % 2 ) ; const index = ( length - width ) / 2 ; const medianNumbers = slice ( index , Maybe . of ( index + width ) , sort < number > ( ascend ( x => x ) , numbers ) ) ; return numberToMaybe ( mean ( medianNumbers ) ) ; } function numberToMaybe ( num ) { return Number . isNaN ( num ) ? Nothing : Maybe . of ( num ) ; }