- scala> val list = List(1,2,3,4)
- list: List[Int] = List(1, 2, 3, 4)
- scala> list.mkString(",")
- res0: String = 1,2,3,4
- scala> list.mkString("(",",",")")
- res1: String = (1,2,3,4)
- scala> list.reduceLeft ( (next,cumulative) => cumulative + next )
- res2: Int = 10
- scala> for( e <- list ) println ( e )
- 1 2 3 4
- scala> for( e <- list ) {
- | println ( e )
- | }
- 1 2 3 4
- scala> for( e <- list ) yield e+1
- res5: List[Int] = List(2, 3, 4, 5)
- scala> for ( e <- list; if( e % 2 == 0 ) ) yield e
- res6: List[Int] = List(2, 4)
Monday, August 3, 2009
Basic Collection Operations
In this example I show some common operations that can be performed on all collections. The examples use list but any Iterable can be used.
Labels:
beginner,
collections,
for,
for-comprehension,
list,
Scala
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment