- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Using map to switch List[Boolean] values
Sun, 2012-02-19, 05:46
I'm trying to write a short function to take a List[Boolean] and
simply switch the values from true to false or vice versa.
Something like this:
def boolNot(myList: List[Boolean]): List[Boolean] =
myList.map ( myList: List[Boolean] => !myList)
Scala doesn't like the !myList at the end. Can anybody suggest how I
can write this to have the function simply negate each element in the
list?
Thanks.
So it's just
myList.map(b: Boolean => !b)
which doesn't really need the type annotation (there's only one thing it can be)
myList.map(b => !b)
or if you use the underscore abbreviation for a completely unambiguous parameter,
myList.map(!_)
--Rex
On Sat, Feb 18, 2012 at 11:46 PM, John <john.public66@gmail.com> wrote: