export class Permutation < X > { sorted : X [ ] ; permutation : number [ ] ; constructor ( sorted , permutation ) { this . sorted = sorted ; this . permutation = permutation ; } static fromFunction < Y > ( original , sort ) { const range = original . map ( ( _v , i ) => i ) ; const permutation = range . sort ( ( a , b ) => sort ( original [ a ] , original [ b ] ) ) ; const res = new Permutation < Y > ( [ ] , permutation ) ; res . forEach ( ( originalIx , newIx ) => { res . sorted [ newIx ] = original [ originalIx ] ; } ) ; return res ; } forEach ( fn ) { this . permutation . forEach ( fn ) ; } map < Y > ( fn ) { const original = this . sorted . map ( fn ) ; const res = new Permutation ( original , this . permutation ) ; return res ; } }