# Extracts of Programming in Scala

CHAPTER 1 - 3

## Chapter 1

• We believe learning about Scala, and the ideas behind it, can help you become a better programmer in general.
• The commended way to read this book is in chapter order, from front to back.
• Once you have read enough, take on a programming project of your own. [...] You can go so far by reading.
• Scala is a blend of object-oriented and functional programming concepts in a statically typed language.
• You could for example specify a particular implementation, such as a HashMap or a TreeMap, or invoke the par method to obtain a ParMap that executes operations in parallel. You could specify a default value for the map, or you could override any other method of the map you create.
• BigInt looks like a built-in type because you can use integer literals and operators such as * and - with values of that type.
• Even though ! Looks like a built-in operator, it too is just a method defined in the Akka actors library.
• Many languages admit values that are not objects, such as primitive values in Java.
• When you say 1+2 in Scala, you are actually invoking a method named + defined in class Int.
• The first idea [of functional programming] is that functions are first-class values. In a functional language, a function is a value of the same status as, say, an integer or a String.
• Methods like replace are called referentially transparent, which means that for any given input the method call could be replaced by its result without affecting the program's semantics.
• [...] So you can write str.toInt instead of Integer.parseInt(str). How can this be achieved without breaking interoperability? Java's String class certainly has no toInt method! In fact, Scala has a very general solution to solve this tension between advanced library design and interoperability. Scala lets you define implicit conversions, which are always applied when types would not normally match up, or when non-existing members are selected. [...] When looking for a toInt method on a String, Scala compiler will find no such member of class String, but it will find an implicit conversion that converts a Java String to an instance of Scala class StringOps, which does define such a member. The conversion will then be applied implicitly before performing the toInt operation.

## Chapter 2

• A val is similar to a final variable in Java. Once initialized, a val can never be reassigned. A var, by contrast, is similar to a non-final variable in Java.
• In Scala you specify a variable's type after its name, separated by a colon.
• def max(x: Int, y: Int): Int = { ... }
• In the case of max, however, you may leave the result type off and the compiler will infer it.
• A result type of Unit indicates the function returns no interesting value.
• If you wish to exit the interpreter, you can do so by entering :quit or :q.
• Command line arguments to a Scala script are available via a Scala array named args.
• Note that Java's ++i and i++ don't work in Scala.
• Another similarity to Java is that if a block has only one statement, you can optionally leave off the curly braces.
• In fact, one of the main aims of this book is to help you become as comfortable with the functional style as you are with imperative style.
• args.foreach(arg => println(arg))
• If you'd prefer to be more explicit, you can mention the type name. But when you do, you'll need to wrap the argument portion in parentheses (which is the normal form of the syntax anyway): args.foreach((arg: String) => println(arg))
• If you're in the mood for more conciseness instead of more explicitness, you can take advantage of a special shorthand in Scala. If a function literal consist of one statement that takes a single argument, you need not explicitly name and specify the argument. Thus, the following code also works: args.foreach(println)
• Only a functional relative of the imperative for (called a for expression) is available in Scala.
for (arg <- args)  println(arg)
• Although arg may seem to be a var, because it will get a new value on each iteration, it really is a val: arg can't be reassigned inside the body of the for expression.

## Chapter 3

• val greetStrings = new Array[String](3)
• Arrays in Scala are accessed by placing the index inside parentheses, not square brackets as in Java. Thus the zeroth element of the array is greetStrings(0), not greetStrings[0].
• When you define a variable with val, the variable can't be reassigned, but the object to which it refers could potentially still be changed.
• If a method takes only one parameter, you can call it without a dot or parentheses.
• Note that this syntax only works if you explicitly specify the receiver of the method call. You cannot write println 10, but you can write Console println 10.
• greetStrings(i) gets transformed into greetStrings.apply(i).
• greetStrings(0) = "Hello" will be transformed into greetStrings.update(0, "Hello")
• val numNames = Array("zero", "one", "two"). This is calling a factory method, named apply, which creates and returns the new array.
• Although you can't change the length of an array after it is instantiated, you can change its element values. Thus, arrays are mutable objects.
• Scala Lists are always immutable (whereas Java Lists can be mutable). More generally, Scala's List is designed to enable a functional style of programming.
• val oneTwoThreeFour = oneTwo ::: threeFour
• Perhaps the most common operator you'll use with lists is ::, which is pronounced "cons". Cons prepends a new element to the beginning of an existing list and returns the resulting list.
• In the expression 1::twoThree, :: is a method of its right operand, the list twoThree.
• If a method is used in operator notation, such as a * b, the method is invoked on the left operand, as in a.*(b) -- unless the method name ends in a colon.
• val oneTwoThree = 1 :: 2 :: 3 :: Nil
• Class List does not offer an "append" operation -- it's written :+ [...] -- but this operation is rarely used, because the time it takes to append to a list grows linearly with the size of the list, whereas prepending with :: takes constant time.
• Tuples can contain different types of elements.
• To instantiate a new tuple that holds some objects, just place the objects in parentheses, separated by commas. Once you have a tuple instantiated, you can access its elements individually with a dot, underscore, and the one-based index of the element.
• If you want a mutable set, you'll need to use an import:
​ximport scala.collection.mutable​val movieSet = mutable.Set("Hitch", "Poltergeist")movieSet += "Shrek"println(movieSet)
• val hashSet = HashSet("Tomatoes", "Chillies")
• val treasureMap = mutable.map[Int, String]()
• treasureMap += (1 -> "Go to island.") This -> method, which you can invoke on any object in a Scala program, returns a two-element tuple containing the key and value. You then pass this tuple to the += method of the map object to which treasureMap refers.
• assert(res == "zero\none\ntwo")
• Once you've integrated through an iterator, it is spent. By transforming it into a list via the toList call, you gain the ability to iterate as many times as you wish, at the cost of storing all lines from the file in memory at once.
• maxWidth = maxWidth.max(widthOfLength(line))
• The reduceLeft method applies the passed function to the first two elements in lines, then applies it to the result of the first application and the next element in lines, and so on, all the way through the list.
• val padding = " " * numSpaces