Tuesday, 28 May 2013

Map.values() returns an Iterable

Hidden in koan("Map values can be iterated") is a comment which piqued my interest:

// NOTE that the following will not compile, as iterators do not implement "contains"
// mapValues.contains("Illinois") should be (true)
view raw AboutMaps.scala hosted with ❤ by GitHub

I cracked out the SBT console to have a better look:

scala> val myMap = Map("MI" -> "Michigan", "OH" -> "Ohio", "WI" -> "Wisconsin", "MI" -> "Michigan")
myMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(MI -> Michigan, OH -> Ohio, WI -> Wisconsin)
scala> val mapValues = myMap.values
mapValues: Iterable[java.lang.String] = MapLike(Michigan, Ohio, Wisconsin)
view raw AboutMaps.scala hosted with ❤ by GitHub

It seems the comment is a little incorrect (myValues is an Iterable rather than an Iterator) but I get the idea.

No comments:

Post a Comment