type KeyResolver < R , K > = ( record : R ) => K ; type Reducer < R , T > = ( groupTotal : T , record : R , index : number , list : R [ ] ) => T ; export const groupBy = < K , R , T > ( getKey , reducer ) => ( records ) : T [ ] => Array . from ( records . reduce ( ( groups , record , index , list ) => { const key = getKey ( record ) ; return groups . set ( key , reducer ( groups . get ( key ) , record , index , list ) ) ; } , new Map ( ) ) . values ( ) ) ;