Last active
March 9, 2023 17:14
-
-
Save rkatic/c522871f336f84e81da3ee5a440c8c34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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