Programming in Scala Gist 8

Vinu Charanya
vTechNotes
Published in
5 min readMay 26, 2016

These are the key points from the Programming in Scala, Second Edition by Martin Odersky, Lex Spoon, Bill Venners. I wanted to make note of some key points. They may seem broken and not make much sense, if you have not read the book. This is for personal reference. Feel free to correct me if I have interpreted anything wrong in this post.

Chapter 8— Functions and Closures

Scala offers several ways to define functions such as functions that are members of some object, functions nested within functions, function literals and function values.

8.1 Methods

  • Most common way — member of some object called method: A def defined as a member of a class, trait or a singleton object is called a method.
An Application that can call the LongLines method above
To run the LongLines method

8.2 Local functions

  • Important design principle of the functional programming style: programs should be decomposed into many small functions that each do a well-defined task. Gives the flexibility to build complex systems with small building blocks.
  • Problem: helper functions can pollute the program namespace. Solution: Scala offers an approach to define functions inside other functions.
  • These are called local functions: A def defined inside a block. These are visible only inside the enclosing block.
  • Local functions can access parameters of the enclosing function.

8.3 First-class functions

  • Scala has first-class functions: Scala supports first-class functions, which means you can express functions in function literal syntax, i.e., (x: Int) => x+1, and that functions can be represented by objects, which are function values.
  • A function literal is compiled into a class that when instantiated at run time is a function value. Thus the distinction between function literals and values is that literals exists in source code, whereas values exist as object at runtime.
Function Value definition
  • Function values can be stored in variables and can be invoked using the usual ()
Function value example with foreach and collection
Function value example with filters

8.4 Short forms of function literals

  • Make function literal more brief: Leave off the parameter type. This is called target typing.
  • Second, leave out parenthesis around a parameter type whose type is inferred.

8.5 Placeholder syntax

  • Use underscores as placeholders for one or more parameters, when they appear only one time within the function literal.
  • Sometimes compiler cannot infer the type and it is better to specify the type.
  • Multiple underscores mean multiple parameters and not reuse a single parameter repeatedly.

8.6 Partially applied functions

  • Entire parameter list can be replaced with an underscore.
Partial function example
  • The val a refers to a function value object which was generated automatically by the Scala compiler from sum _, the partially applied function expression. The generated class has an apply method that takes 3 arguments.
  • A partial set of arguments can also by passed to the partial function as the example below:
  • A partial applied function expression in which all the parameters are left off, can be expressed more concisely without _ . someNumbers.foreach(println) This is allowed only in places where a function is required.

8.7 Closures

Free variable
Bound variable
  • Closure: A function object that captures free variables, and is said to be “closed” over the variable visible at the time it is created.
  • A function literal with no free variables, such as (x: Int) => x + 1, is called a closed term. But any function literal with free variables, such as (x: Int) => x + more, is an open term.
  • Scala’s closures captures the variables themselves, not the referred variables.
Closure with different values for more

8.8 Special function call forms

  • Scala supports repeated parameters, named arguments and default arguments
  • Repeated parameters — indicate the last param to a function may be repeated by placing * after the type of the param.
  • Inside the function, the type of the repeated param is an Array of the declared type. However, passing an array will throw a type error. To accomplish, append the array argument with a colon and _* symbol. Notation tells the compiler to pass each element as its own argument.
  • Named arguments- allow you to pass argument to a function in a different order. syntax: function(paramName1 = value, paramName2 = value)
  • Default param values-argument for a such a param can be omitted from a function call and the corresponding default value will be used.
Default param values used with named arguments

8.9 Tail recursion

  • Tail recursive-functions that call themselves at the only place as their last action.
  • Tracing tail-recursive functions: All calls will execute in a single frame. A tail-call optimizations when looking at a stack trace can be turned off by passing the following argument: -g:notailcalls
  • Limits of tail recursion: Scala can only optimize directly recursive call back to the same function making the call.
  • Indirect recursion doesn’t have any optimization.
  • No tail-call optimization if the final call goes to a function value.
Not optimized, although the function value is used to call the recursive function.

Conclusion

In addition to methods, Scala provides local function, function literals and function values. In addition to normal function calls, Scala provides partially applied functions and functions with repeated params. Function calls are implemented as optimized tail calls when possible.

--

--