I am currently doing this:
val count = sightings.map(_.shape).distinct.length
map
distinct
You can use an iterator to not create the intermediate collection and then accrue the shapes in a Set
to get the distinct ones:
val count = sightings.iterator.map(_.shape).toSet.size
Alternatively, you can use collection.breakOut
to accrue the items in a Set
without creating the intermediate collection (another answer suggested using breakOut
, but in a different way):
val distinctShapes: Set[Shape] = sightings.map(_.shape)(collection.breakOut)
val count = distinctShapes.size