I want to know if a List
is homogeneous.
Here is my code :
def isHomogeneous(ls: List[Any]) = ls.map(_.getClass).toSet.size == 1
Is there a better way ?
def allEqual(xs: Traversable[_]) =
xs.headOption.forall(head => xs.forall(_ == head))
def isHomogeneous(xs: Traversable[_]) =
allEqual(xs.view.map(_.getClass))
getClass
business separate from the traversal.Traversable
instead of List
.Nil
.