7. MAGIC!
Scala is a Functional Programming language.
And Functional Programming is in essence MATH….
MATH!!
8. ● From Wiki:
In computer science, functional programming is a programming paradigm—a style of
building the structure and elements of computer programs—that treats computation as the
evaluation of mathematical functions and avoids changing-state and mutable data.
● Everything is MATH, even the simplest form of presentation:
○ Type-theory
2: natural means value: type
○ Infix operator
E op E’ means operate between E and E’,
● 1 + 2,
● a max b (dot notation: a.max(b)),
● x defined_function y
Functional Programming
infix notation
11. How to present computation or program with
mathematical functions?
E op E’
Expression!!
12. Expression-Oriented Language
● All functional programming languages are expression-oriented.
An expression-oriented programming language is a programming language where every
(or nearly every) construction is an expression and thus yields a value.
● Statement v.s Expression
● Benefits to EOP: easier to test, reason, and express
● Scala expression return a value
○ If/else expression: val greater = if (a > b) a else b
○ Try/catch block:
○ A function call return value
14. Functional Language
● First-class citizen:
an entity which supports all the operations generally available to other entities.
● First-class function:
A PL treat function as first-class citizen, supports
● Passing functions as function arguments
● Return functions as value from other functions
● Assign function to variable or store in data structure → function literal/value
● Higher-order function
In mathematics and computer science, a higher-order function (also functional, functional form
or functor) is a function that does at least one of the following:
1. takes one or more functions as arguments (i.e., procedural parameters),
2. returns a function as its result.
Func1 Func2
First-class function
Higher-order
function
15. How do we express function?
- Functional programming languages implement the lambda calculus. -
Lambda Calculus!
16. Lambda calculus
● Lambda calculus (λ-calculus) is somehow a tool that provides provides a simple
semantic to express function (computation)
Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and
application using variable binding and substitution, which is developed in the 1930s to investigate computability.
● Two simplifications to make semantic simple
1. Anonymous function
Anonymous functions are sometimes called lambda expressions.
2. uses functions of a single input
An ordinary function that requires two inputs, ex. f(x, y), can be reworked into an equivalent
function that accepts a single input, and as output returns another function, that in turn accepts
a single input.
17. Anonymous Function
● Nested function
a function which is defined within another function, the enclosing function.
● Anonymous function: syntactically lighter than a named function
In computer programming, an anonymous function (function literal, lambda abstraction) is a
function definition that is not bound to an identifier.
Anonymous functions are a form of nested function, in allowing access to variables in the
scope of the containing function (non-local variables). This means anonymous functions need
to be implemented using closures.
● Scala anonymous function syntax: fat arrow => to separate params and body
18. Closure
● Closure
Operationally, a closure is a record storing a function[a] together with an environment
The referencing environment binds the non-local names to the corresponding variables in the
lexical environment at the time the closure is created, additionally extending their lifetime to at
least as long as the lifetime of the closure itself.
● Free variable and bound variable
After doSome(), x is still alive in
the following two println...
19. Closure
Every time doSome got called,
f and execution context for f will
returned.
doSome
(execution context for f)
f
(function to execute)
x
(Bound variable for
doSome, free variable
for f)
Two different execution context
20. ● So
○ Function is a first-class citizen, so we can have function literal
○ Function variable is constant since function is literal:
val function_name = …
○ filter is an operator takes two expressions: xs and an anonymous function
○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take
function as argument or return function
○ No closure here
E op E’
21. ● So
○ Function is a first-class citizen, so we can have function literal
○ Function variable is constant since function is literal:
val function_name = …
○ filter is an operator takes two expressions: xs and an anonymous function
○ In sort(xs filter (pivot >)) , sort and filter are higher-order functions since they take
function as argument or return function
○ No closure here
A function call/expression is an
argument for the *filter* operator…
Should be (x => pivot > x)
(pivot >) … WTH?
E op E’
22. Simple Semantic for Lambda
● Concise syntax
● Closure and syntax
■ Dynamic programming language (ex. python, js)
■ Static programming language (ex. java, scala)
→ end up writing code with tons of types
→ Type inference in Scala
● Arity
○ Arity-0: no arguments. omission of parentheses on methods of arity-0
○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should
only be used for
● purely-functional methods (methods with no side-effects) - such as mkString, or
● methods which take functions as parameters - such as foreach:
23. Simple Semantic for Lambda
● Concise syntax
● Closure and syntax
■ Dynamic programming language (ex. python, js)
■ Static programming language (ex. java, scala)
→ end up writing code with tons of types
→ Type inference in Scala
● Arity (wiki)
○ Arity-0: no arguments. omission of parentheses on methods of arity-0
○ Arity-1: one arguments. This syntax is formally known as “infix notation”. It should
only be used for
● purely-functional methods (methods with no side-effects) - such as mkString, or
● methods which take functions as parameters - such as foreach
● Issue
○ Infix v.s dot (stackoverflow discussion)
○ Complicated Underscore (stackoverflow discussion)
35. Partially applied function
● Apply (wiki)
In mathematics and computer science, apply is a
function that applies functions to arguments.
● In functional programming,
○ Fully applied function
when you call a function with all parameters
○ Partially applied function
when you call a function with a subset of parameters
Programexecution
Func2’(?=3)
Func2’(?=9)
Func2’(?=13)
Func2’ Data ?
Func2 arguments
● You can not only replace one parameter with the placeholder syntax “_” myNumbers.foreach(println(_))
but also an entire parameter list: myNumbers.foreach(println _)
37. ● Anonymous function
○ Initially (x => pivot > x)
○ Shorthand partially applied function (pivot > _)
○ Drop underscore (pivot > )
NOTE: Leaving out the underscore is only allowed when a function is expected:
● val c = t2 → Not OK!
● val c = t2 _ → OK!
38. ● Anonymous function
○ Initially (x => pivot > x)
○ Shorthand partially applied function (pivot > _)
○ Drop underscore (pivot > )
NOTE: Leaving out the underscore is only allowed when a function is expected.
● These two are different: funcA _ v.s. funcA(_)
39. Currying
● Currying is the technique of translating the evaluation of a function that takes multiple
arguments (or a tuple of arguments) into evaluating a sequence of functions, each with
a single argument.
F(x, y, z) → F(x)(y)(z) Func 31 Func 32 Func 33Func 3
● Currying is useful in both practical and theoretical settings.
○ Practical
provides a way of automatically managing how arguments are passed to functions and exceptions in functional
programming.
○ Theoretical
provides a way to study functions with multiple arguments in simpler theoretical models with only one argument
41. Polymorphic Methods
Method dup is parameterized with type T and
with the value parameters x: T and n:
Int.
In the first call to dup, the programmer
provides the required parameters, but as
the following line shows, the programmer is
not required to give actual type parameters
explicitly.
The type system of Scala can infer such
types. This is done by looking at the types of
the given value parameters and at the
context where the method is called.
45. By-name Parameter
● Only function body got passed
● By-name parameter is NOT a function object, just a name, cannot be called
46. Syntax Abstraction
● Function abstraction
○ lambda calculus provide flexibility to define function/formula, control flow, etc.
● Syntactic abstraction
○ Build its own syntax → Domain specific language
By-name parameter + Curry:
Unless and until seems to be a syntax
47. Recap
● Functional, expression-oriented language
○ Expression
○ First-class function
● Lambda calculus
○ Anonymous function
○ Closure
○ Simple semantic
● World view: OOP and FP
● Abstraction
○ Function
■ Partially applied function
■ Currying
■ Function composition
■ Polymorphic methods
○ Syntax
■ By-value, by-name parameter