Templates by BIGtheme NET

Core – Scala’s Collections Library

Introduction
1) The usages of the collection operation make inputs and output explicit as function
parameters and results.
2) These explicit inputs and outputs are subject to static type checking. The bottom line
is that the large majority of misuses will manifest themselves as type errors.
3) Parallel collections support the same operations as sequential ones, so no new operations
need to be learned and no code needs to be rewritten.
You can turn a sequential collection into a parallel one simply by invoking the par method.

Example: Here’s one line of code that demonstrates many of the advantages of Scala’s collections.

 val (minors, adults) = people partition (_.age < 18)

It’s immediately clear what this operation does: It partitions a collection of people into minors and adults depending on their age.
Because the partition method is defined in the root collection type TraversableLike, this code works for any kind of collection, including arrays. The resulting minors and adults collections will be of the same type as the people collection.

This code is much more concise than the one to three loops required for traditional collection processing (three loops for an array, because the intermediate results need to be buffered somewhere else).
Furthermore, the partition operation is quite fast, and will get even faster on parallel collections on
multi-cores.

Adding two numbers:
AddTwoNumbers.scala

class AddTwoNumbers (num1: Double, num2: Double) {  
  def a1() = num1
  def a2() = num2
}

object AddTwoNumbers {
  def main(args: Array[String]) {
    val num = new AddTwoNumbers(1.2, 3.4)
    println("Adition result is: " + ( num.a1() + num.a2()) )
  }
}

OutPut:

 Adition result is: 4.6

Demo
1

*** Venkat – Happy leaning ****