In the imperative Java world, we are used to writing loops. For example:
int[] arr = new int[10]; for (int i = 0; i < 10; ++i) { arr[i] = arr[i] + 1; } System.out.println(Arrays.toString(arr));
This changes the array in place (that is, the original data structure is being mutated). The Scala way is:
scala> val list = List.fill(10)(0) list: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) scala> list map (a => a + 1) foreach { a => print(" " + a) } 1 1 1 1 1 1 1 1 1 1 scala> list res3: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Instead of coding the loop, we pass in a function to a construct, to building blocks called combinators. These building blocks aka combinators allow themselves to be composed into a higher-level combinator. These combinators allow you to think in terms of higher-level operations and their interactions, instead of low-level details. This is an example of the power of higher order functions.
Also note that the original...