scala - Pattern matching on tail of arrays -
i trying implement distinct function on arrays this:
def distinct(a: array[int]): array[int] = match { case array() => case array(head, tail @ _*) => head +: distinct(tail.toarray).filter(_ != head) }
i don't like, have transform tail toarray every time. otherwise compiler complains tail sequence , not array. possible pattern match better in case?
it isn't sexy enough pattern matching, go way:
def distinct(a: array[int]): array[int] = match { case array() => case htail => htail.head +: distinct(htail.tail).filter(_ != htail.head) }
Comments
Post a Comment