The document describes a hands-on guide for functional programming with Scala located at github.com/vinhdangphuc/hands-on/scala.md. It introduces concepts of functional programming like pure functions, avoiding side effects and shared state, immutability, and declarative programming. It also covers Scala-specific features like higher order functions, pattern matching, and implicit conversions that are useful for functional programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
230 views
Functional Programming With Scala
The document describes a hands-on guide for functional programming with Scala located at github.com/vinhdangphuc/hands-on/scala.md. It introduces concepts of functional programming like pure functions, avoiding side effects and shared state, immutability, and declarative programming. It also covers Scala-specific features like higher order functions, pattern matching, and implicit conversions that are useful for functional programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23
https://github.
com/vinhdangphuc /hands-on Follow the instruction on hands-on/scala.md
Functional Programming with Scala 1
Functional Programming with Scala VINH DANG Data Engineer
FPT Telecom - 2019.11.02
Functional Programming with Scala 2
Functional Programming
The process of building software by composing pure functions,
avoiding shared state, mutable data, and side-effects declarative rather than imperative
Functional Programming with Scala 3
Function ● A function is a collection of statements that perform a certain task ● Function is a object which can be stored in a variable ● Method always belongs to a class which has a name, signature bytecode
Functional Programming with Scala 4
Pure functions ● Given the same inputs, always returns the same output ● Has no side-effects
Functional Programming with Scala 5
Side Effects ● Any application state change that is observable outside the called function other than its return value ○ Modifying any external variable or object property ○ Writing to the screen/console ○ Triggering any external process
Functional Programming with Scala 6
Shared state ● Shared state is any variable, object, or memory space that exists in a shared scope ● Or as the property of an object being passed between scopes ● A shared scope can include global scope or closure scopes
Functional Programming with Scala 7
Immutability ● An immutable object is an object that can’t be modified after it’s created ● In concurrent situation: access a mutable object required locking -> reduces throughput, more difficult to maintain
Functional Programming with Scala 8
Declarative vs Imperative ● Imperative programs spend lines of code describing the specific steps used to achieve the desired results — the flow control: How to do things ● Declarative programs abstract the flow control process, and instead spend lines of code describing the data flow: What to do. The how gets abstracted away
Functional Programming with Scala 9
Why scala? ● Multi-Paradigm Language: OOP, FP ● Interoperability with Java: be able to use all Java libraries ● Expressiveness: you can write beautiful and clean code ● Statically Typed: compile time error
Functional Programming with Scala 10
Higher Order Functions ● Take other functions as parameters ● Or result is a function ● Tends to reuse a common set of functional utilities to process data
Functional Programming with Scala 11
Higher Order Functions ● Map applies a function on all elements of a collection
Functional Programming with Scala 12
Higher Order Functions ● Filter creates a list of elements for which a function returns true
Functional Programming with Scala 13
Higher Order Functions ● Return a function
Functional Programming with Scala 14
Pattern Matching ● More powerful version of switch statement in Java
Functional Programming with Scala 15
Pattern Matching ● Matching on types
Functional Programming with Scala 16
Pattern Matching ● More powerful case
Functional Programming with Scala 17
Pattern Matching ● The most powerful case: matching on case classes
Functional Programming with Scala 18
Implicit Conversion ● Set of methods that are apply when an object of wrong type is used ● Compiler to automatically convert of one type to another
Functional Programming with Scala 19
Implicit Conversion ● Implicit conversions are applied in two conditions ○ First, if an expression of type A and S does not match to the expected expression type B ○ Second, in a selection e.m of expression e of type A, if the selector m does not represent a member of A