Upload
kunal-cholera
View
685
Download
0
Embed Size (px)
Citation preview
Scala Functions Short link to this slides
http://bit.ly/s-scala-functions
Things we will cover:1. Functions as methods on objects2. Local or nested functions vs private methods3. Function literals and function values4. Placeholder syntax5. Partial functions, partially applied functions & currying6. Tail recursive functions7. Function parameters types - repeated, named & default8. Function closure9. Functions vs methods - signature, type params & in-
depth.
Functions as methods on objects
Local or nested functions
Function literals and function values
Placeholder syntax for function parameters
Partial function
Partially applied functions
Currying
Tail recursive functions
Tail Recursion
Recursion
Tail recursive functions are functions that call themselves as their last action. Scala compiler will do tail call optimizations by not allocating a stack frame per function invocation and it would replace it with the function call to jump back to the beginning of the function after updating the function parameters with new values.
Function parameter types
Function closureThe name “closure” arises from the act of “closing” the function literal by “capturing” the bindings of its free variables.
A free variable is variable which the function literal does not define or is not passed into a function.
Take a look at e.g. on the side, abc is a free variable which is closed into incAbc function definition.
Closure sees the change to the variable every time a function invocation is done.
Function vs methods - signature
Function vs methods - type params
Function vs methods - in depthA function is simply an object that wraps a method, viz. apply, and Scala provides convenience syntax for invoking the apply method of a function without having to actually name it.
scalac test.scala
1
2
3
4
0