Iterator Unique
for Stage 1
Michael Ficarra • January 2024
problem: only get distinct values from an iterator
Iterator.from("Mississippi").distinct();
// ⇒ "M", "i", "s", "p"
let states = ["Alabama", "Alaska", "Arizona", ...];
Iterator.from(states).distinctBy(state => state[0]);
// ⇒ "Alabama", "California", "Delaware", "Florida", ...
design considerations
prior art: other languages
language | library | simple API | with comparator | with mapping |
Clojure | core | distinct | -- | -- |
Elm | List.Extra | unique | -- | uniqueBy |
Haskell | Data.List | nub | nubBy | -- |
Java | Stream | distinct | -- | -- |
Kotlin | Sequence | distinct | -- | distinctBy |
.NET | System.Linq | Distinct | Distinct, DistinctBy | DistinctBy |
PHP | array | array_unique | -- | -- |
Python | more-itertools | unique_everseen | -- | unique_everseen |
Ruby | Enumerable | uniq | -- | uniq |
Scala | Seq | distinct | -- | distinctBy |
Shell | GNU coreutils | uniq | -- | -- |
notably absent: Rust Iterators, Swift Sequences
prior art: JS libraries
library | simple API | with comparator | with mapping |
extra-iterable | unique | unique | unique |
iter-ops | distinct | -- | distinct |
iter-tools | distinct | -- | distinct |
itertools-ts | distinct | -- | distinct |
Lodash / Underscore | uniq | uniqWith | uniqBy |
Ramda | uniq | uniqWith | uniqBy |
sequency | distinct | -- | distinctBy |
wu | unique | -- | -- |
champion's preferences
Stage 1?