Code Examples for

Programming in Scala, Second Edition

Return to chapter index

1 A Scalable Language

  • 1.1 A language that grows on you
  • 1.2 What makes Scala scalable?
  • 1.3 Why Scala?
  • 1.4 Scala's roots
  • 1.5 Conclusion
  • 1.1 A language that grows on you


    var capital = Map("US" -> "Washington", "France" -> "Paris") capital += ("Japan" -> "Tokyo") println(capital("France"))
    def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1)
    265252859812191058636308480000000
    import java.math.BigInteger def factorial(x: BigInteger): BigInteger = if (x == BigInteger.ZERO) BigInteger.ONE else x.multiply(factorial(x.subtract(BigInteger.ONE)))
    recipient ! msg
    receive { case Msg1 => ... // handle Msg1 case Msg2 => ... // handle Msg2 // ... }
    actor { var sum = 0 loop { receive { case Data(bytes) => sum += hash(bytes) case GetSum(requester) => requester ! sum } } }

    1.2 What makes Scala scalable?

    1.3 Why Scala?


    // this is Java class MyClass { private int index; private String name; public MyClass(int index, String name) { this.index = index; this.name = name; } }
    class MyClass(index: Int, name: String)
    // this is Java boolean nameHasUpperCase = false; for (int i = 0; i < name.length(); ++i) { if (Character.isUpperCase(name.charAt(i))) { nameHasUpperCase = true; break; } }
    val nameHasUpperCase = name.exists(_.isUpper)
    // this is Java interface CharacterProperty { boolean hasProperty(char ch); }
    // this is Java exists(name, new CharacterProperty() { public boolean hasProperty(char ch) { return Character.isUpperCase(ch); } });
    s.exists(p) || s.exists(q) == s.exists(x => p(x) || q(x))
    def f(x: String) = ...
    val x: HashMap[Int, String] = new HashMap[Int, String]()
    val x = new HashMap[Int, String]() val x: Map[Int, String] = new HashMap()

    1.4 Scala's roots

    1.5 Conclusion

    For more information about Programming in Scala, Second Edition (the "Stairway Book"), please visit:

    http://www.artima.com/shop/programming_in_scala_2ed

    and:

    http://booksites.artima.com/programming_in_scala_2ed

    Copyright © 2007-2010 Artima, Inc. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.