Skip to content

Instantly share code, notes, and snippets.

@rkatic
Last active March 9, 2023 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkatic/c522871f336f84e81da3ee5a440c8c34 to your computer and use it in GitHub Desktop.
Save rkatic/c522871f336f84e81da3ee5a440c8c34 to your computer and use it in GitHub Desktop.
const identity = x => x
/**
* Creates a predicate function to check if a value is not seen before
*
* @param {function} [by = x=>x]
* @returns {function} predicate function
*
* @example
*
* const uniqueUsers = users.filter(distinct(it => it.id))
*/
function distinct (fn = identity) {
const seen = new Set()
return x => seen.size < seen.add(fn(x)).size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment