export const identity = elem => elem ; export const toString = elem => elem . toString ( ) ; param elemToInsert param list param keyExtractor export function insertSortedLast < U > ( elemToInsert , list , keyExtractor = toString ) { list . splice ( findSortedInsertionIndexLast ( keyExtractor ( elemToInsert ) , list , keyExtractor ) , 0 , elemToInsert ) ; } param needle param haystack param keyExtractor export function findSortedInsertionIndexLast < U , V > ( needle , haystack , keyExtractor = toString ) { let low = 0 ; let high = haystack . length ; while ( low !== high ) { const cur = Math . floor ( low + ( high - low ) / 2 ) ; if ( needle < keyExtractor ( haystack [ cur ] ) ) { high = cur ; } else { low = cur + 1 ; } } return low ; } /**
 * Find the index of an element in a sorted list. If list contains no matching element, return -1.
 */ export function findSortedIndex < U , V > ( needle , haystack , keyExtractor = toString ) { let low = 0 ; let high = haystack . length ; while ( low !== high ) { const cur = Math . floor ( low + ( high - low ) / 2 ) ; const extractedKey = keyExtractor ( haystack [ cur ] ) ; if ( needle < extractedKey ) { high = cur ; } else if ( needle > extractedKey ) { low = cur + 1 ; } else { return cur ; } } return - 1 ; } /**
 * Like  Array . prototype . findIndex but finds the last index instead.
  export function findLastIndex < T > ( arr , predicate ) { for ( let i = arr . length - 1 ; i >= 0 ; i -- ) { if ( predicate ( arr [ i ] ) ) { return i ; } } return - 1 ; } /**
 * Like  Array . prototype . find but finds the last matching element instead.
  export function findLast < T > ( arr , predicate ) { return arr [ findLastIndex ( arr , predicate ) ] ; } /**
 * Return a new array, where all elements from the original array occur exactly once.
 */ export function removeDuplicates < T > ( arr , eq = ( x , y ) => x === y ) { const results = [ ] ; for ( const arrElement of arr ) { let duplicateFound = false ; for ( const resultElement of results ) { if ( eq ( arrElement , resultElement ) ) { duplicateFound = true ; break ; } } if ( ! duplicateFound ) { results . push ( arrElement ) ; } } return results ; }