Published on 2018 less than a minute
Functional programming in scala
The cheat sheet recaps the course.
→ List
→ More functions on lists
→ Pairs and Tuples
pairs allows pattern matching:
(x, y) match {
case (Nil, y) => y
case (x, Nil) => x
_ =>
}
→ Implicit parameters
It's a good idea to specify functions arguments in the additional parameters, because the compiler can infer the types of the functions parameters from the regular arguments:
// no necessary to specify w and z types
def function(x:Int, y:Int)(ord: Ordering):Boolean = {
ord(x,y) < 0
}
Then it is possible to chose an implicit parameter because the compiler is able to infer the kind of implicit function to look up.
// no necessary to specify w and z types
def function(x:Int, y:Int)(implicit ord: Ordering):Boolean = {
ord(x,y) < 0
}
→ Higher order functions
A high order function takes a other function as argument.
→ Reduction of lists
(x, y)) => x * y
// can be rewritten
_ * _
// however
(x, x)) => x * x
// cannot because each _ represents a new parameter
→ Reasoning about concat
This page was last modified: