Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Unit from List

Tags:

scala

Given a List, I'd like to filter out any elements that equal Unit - ().

Is there a better way to filter than through this code?

scala> List( () ).filter( x => x != () )
<console>:8: warning: comparing values of types Unit and Unit using `!=' will 
always yield false
                  List( () ).filter( x => x != () )
                                            ^
    res10: List[Unit] = List()
like image 949
Kevin Meredith Avatar asked Mar 26 '26 16:03

Kevin Meredith


1 Answers

I'd go with this:

List(1, (), 4, (), 9, (), 16) filter (_ != ())
res0: List[AnyVal] = List(1, 4, 9, 16)
like image 195
Randall Schulz Avatar answered Mar 29 '26 08:03

Randall Schulz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!