SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
A Gentle Introduction
to Scala and FP
1/50
Hi, I'm Anton!
Functional
programmer
Scala enthusiast
Applications engineer
at Rakuten
@a7emenov
Find me on ,
and
Twitter
Telegram Github
2/50
Agenda
Introduce Scala as a language
Give examples of universal FP constructs
Show benefits of those constructs
Compare Scala to other languages
3/50
What is Scala as
a language?
4/50
Scala combines object-
oriented and functional
programming in one
concise, high-level
language.
5/50
What lies at the core of FP?
6/50
What lies at the core of FP?
Absence of side-effects in
computations
6/50
What lies at the core of FP?
Absence of side-effects in
computations
Function composition
6/50
What lies at the core of FP?
Absence of side-effects in
computations
Function composition
Immutable data
6/50
What lies at the core of FP?
Absence of side-effects in
computations
Function composition
Immutable data
Dark magic
6/50
What lies at the core of FP?
Absence of side-effects in
computations
Function composition
Immutable data
Dark magic
?
6/50
What lies at the core of FP?
Absence of side-effects in
computations
Function composition
Immutable data
Dark magic
All of it
7/50
Functional programming is a
way of solving problems which
centers around combining
side-effect-free constructs in a
declarative manner
8/50
Declarative vs. imperative
Imperative
for (int i = 1; i 5; i ) {
int temp = i * 2;
if (temp < 6) {
System.out.println(temp);
}
}
Declarative
(1 to 5)
.map(_ * 2)
.filter(_ < 6)
.foreach(println)
9/50
Deep dive
FP & Scala
Immutability
Pure functions
Referential transparency
Algebraic data types
Pattern matching
Recursion
10/50
Value mutation
Variables
Can be changed anytime
Can be changed from
any place that has a
reference
Exposing private state
requires explicit copying
Immutable values
Can't be changed
Create new values
instead of changing
the old ones
No mutable state
11/50
Value mutation
Variables Immutable values
12/50
Immutable values
Benefits
Reduce concurrency
issues
No shared mutable
state
Easier local reasoning
Drawbacks
Extra memory allocations
13/50
Pure functions
Side-effect freeSide-effect free - everything that a function does can be
observed in its return value:
TotalTotal - return values for every possible input
DeterministicDeterministic - given same inputs, return same
outputs
Locally scopedLocally scoped - no effect on the state outside of the
function's scope
14/50
Referential transparency
All calls to a function
can be substituted
with its return value
15/50
Referential transparency
Not transparent
def getInt(l: Int, r: Int): Int =
Random.between(l, r)
def increment(x: Int): Int = {
val res = x + 1
destroyTheWorld()
res
}
Transparent
def fact(n: Int): Int = {
var res = 1
var i = 1
while (i n) {
res = res * i
i = i + 1
}
res
}
16/50
Business value with pure functions
How can programs be
useful without any
side-effects?
17/50
Business value with pure functions
Describe side-effecting computations
instead of instantly running them
Combine all side-effects to produce a
single description
Run the resulting computation in the
main method
18/50
Business value with pure functions
def saveValue(key: String, value: Int): IO[Unit] = ?
def getValue(key: String): IO[Option[Int]] = ???
def rateLimit[A](program: IO[A]): IO[A] = ???
val program: IO[Option[Int]] =
ratelimit(
saveValue("key", 1) >> getValue("key")
)
val result: Option[Int] = program.runSyncUnsafe()
19/50
Business value with pure functions
saveValue("key", 1) >> getValue("key")
def saveValue(key: String, value: Int): IO[Unit] = ???
def getValue(key: String): IO[Option[Int]] = ???
def rateLimit[A](program: IO[A]): IO[A] = ???
val program: IO[Option[Int]] =
ratelimit(
)
val result: Option[Int] = program.runSyncUnsafe()
19/50
Business value with pure functions
def rateLimit[A](program: IO[A]): IO[A] = ???
def saveValue(key: String, value: Int): IO[Unit] = ???
def getValue(key: String): IO[Option[Int]] = ???
val program: IO[Option[Int]] =
ratelimit(
saveValue("key", 1) >> getValue("key")
)
val result: Option[Int] = program.runSyncUnsafe()
19/50
Business value with pure functions
val program: IO[Option[Int]] =
ratelimit(
saveValue("key", 1) >> getValue("key")
)
def saveValue(key: String, value: Int): IO[Unit] = ???
def getValue(key: String): IO[Option[Int]] = ???
def rateLimit[A](program: IO[A]): IO[A] = ???
val result: Option[Int] = program.runSyncUnsafe()
19/50
Business value with pure functions
val result: Option[Int] = program.runSyncUnsafe()
def saveValue(key: String, value: Int): IO[Unit] = ???
def getValue(key: String): IO[Option[Int]] = ???
def rateLimit[A](program: IO[A]): IO[A] = ???
val program: IO[Option[Int]] =
ratelimit(
saveValue("key", 1) >> getValue("key")
)
19/50
Pure functions
Benefits
Ultimate local
reasoning
Parallelizable
execution
Easy testing
Drawbacks
Extra object allocations
Require an effect system
or a library to operate
mutable state
20/50
Algebraic data types
Product typesProduct types - define how to create a value
(“has a field” relationship)
Sum typesSum types - define the range of values
(“is one of” relationship)
Product types + Sum types =
Algebraic data types
21/50
Product types
Traditional classes
public class Person {
// Which is the primary constructor?
// Are the constructors related?
public Person(Age age,
Year birthYear)
{ /* ... */ }
public Person(Year currentYear,
Age age)
{ /* ... */ }
public Person(Year currentYear,
Year birthYear)
{ /* ... */ }
}
22/50
Product types
Traditional classes
public class Person {
// Which is the primary constructor?
// Are the constructors related?
public Person(Age age,
Year birthYear)
{ /* ... */ }
public Person(Year currentYear,
Age age)
{ /* ... */ }
public Person(Year currentYear,
Year birthYear)
{ /* ... */ }
}
Product types
// Primary constructor defines the product
case class Person(age: Age,
birthYear: Year) {
// Supplementary constructors
// must use the primary constructor
def this(currentYear: Year, age: Age)
{ this(age, currentYear - age) }
def this(currentYear: Year, birthYear: Year
{ this(currentYear - birthYear, birthYear
}
22/50
Sum types
Traditional interfaces
public interface User
{ /* Common methods */ }
public class Client implements User
{ /* Client-specific methods */ }
public class Admin implements User
{ /* Admin-specific methods */ }
void process(User user) {
// How can this function reason about a
// possible implementations of User?
}
23/50
Sum types
Traditional interfaces
public interface User
{ /* Common methods */ }
public class Client implements User
{ /* Client-specific methods */ }
public class Admin implements User
{ /* Admin-specific methods */ }
void process(User user) {
// How can this function reason about a
// possible implementations of User?
}
Sum types
// "Sealed" allows the compiler to find
// all descendants of User
sealed trait User
{ /* Common methods */ }
case class Client extends User
{ /* Client-specific methods */ }
case class Admin extends User
{ /* Admin-specific methods */ }
def process(user: User): IO[Unit] = {
// The function now has knowledge that
// there are only 2 options for "user"
}
23/50
Pattern matching
Data construction
sealed trait User
case class Client(email: String
role: String)
extends User
case class Admin(name: String,
scopes: List[String])
extends User
val client: User =
Client("test@email.com", "manager")
val admin: User =
Admin("Jane", List("emails", "orders")
Data deconstruction
def process(u: User): IO[Unit] = u match {
case Client(email, "manager")
logManager(email)
case Admin(name, Nil)
logAdmin(name, "No scopes")
case Admin(name, List("emails"))
logAdmin(name, "Only 'emails' scope")
case Admin(name, scopes)
if scopes.contains("orders")
logAdmin(name, "Has 'orders' scope")
case _
logDefault("Default action")
}
24/50
Pattern matching
Benefits
Compile-time type
checks
Exhaustiveness checks
Enhanced code
readability
Extensible
Drawbacks
Extra memory allocations
25/50
Recursion
Declarative
Native mapping to
many real-world
problems
Can be as efficient as a
loop*
import scala.annotation.tailrec
@tailrec
def foldLeft[R](list: List[Int])
(acc: R)
(f: (R, Int) R): R =
list match {
case Nil
acc
case head :: tail
foldLeft(tail)(f(acc, head))(f)
}
// "12345"
foldLeft(List(1, 2, 3, 4, 5))(acc = "") {
(acc, num) acc + num.toString
}
26/50
FP in Scala - covered points
Immutability
Pure functions
Referential transparency
Algebraic data types
Pattern matching
Recursion over iteration
27/50
Scala
language
features
Higher-kinded types
For-comprehensions
Implicit values
Syntax extension
Macros
28/50
Higher-kinded types
Simple types:Simple types:
Int, String, User
Unary typeUnary type
constructors:constructors:
List[A], Future[A]
Binary typeBinary type
constructors:constructors:
Either[A, B], Tuple[A, B]
Higher-kinded typesHigher-kinded types:
trait Processor[L[_]] {
def run[A](l: L[A]): Unit
}
val listP = new Processor[List] {
def run[A](l: List[A]): Unit =
l.foreach(println)
}
listP.run(List(1, 2, 3))
listP.run(List("a", "b", "c"))
29/50
For comprehensions
def getProfile(userName: String): Option[Profile] = ???
def getComments(profile: Profile): Option[List[Comment]] = ???
def getBonus(profile: Profile): Option[Int] = ???
val updatedComments = for {
profile getUserProfile("Jack")
comments getComments(profile)
bonus getBonus(profile)
} yield comments.take(10)
.map( .increaseRating(bonus))
30/50
For comprehensions
Define coherent syntax for sequential
composition
Syntactic sugar for every type with map,
flatMap and withFilter methods
Can be used with any type supplying these
methods
Take advantage of pattern matching
31/50
Implicit values
Provide automatic
value forwarding
Resolved at compile-
time
Can be derived from
existing context
case class Foo(id: Int)
object Foo {
implicit val o: Ordering[Foo] =
Ordering.by(_.id)
}
def sortFoo(foos: List[Foo])
(implicit o: Ordering[Foo]) =
foos.sorted(o)
sortFoo(
32/50
Syntax extensions
Allow to create ad-hoc
methods for code
reuse and readability
Can be used for types
imported from other
projects/libraries
implicit class StringSyntax
(s: String) {
def filterDigits: String =
s.filter(_.isDigit)
}
// "123"
"1a2b3c".filterDigits
33/50
Macros
Eliminate boilerplate
code
Provide
implementations in
compile time
Allow to extract code
meta data
import io.circe._
import io.circe.generic.semiauto.
case class Foo(a: Int, b: String)
case class Bar(
id: Option[String], foo: Foo
)
val barDecoder: Decoder[Bar] =
deriveDecoder[Foo].map(foo
Bar(id = None, foo = foo)
)
34/50
Scala language - covered points
Higher-kinded types
For-comprehensions
Implicit values
Syntax extensions
Macros
35/50
Scala in
numbers
Development and
adoption rates
Effect on productivity
Performance
36/50
Scala adoption rates
Maintained intense release
schedule for over 15 years
Development of the
language is supported by the
European government and
enterprise investors[1]
As popular as Kotlin for
backend development[2] and
is 20% more widespread
company-wise[3]
1.
2.
3.
bit.ly/2Xr3bnH
bit.ly/2rPlUgW
api.github.com
stackshare.io/scala
37/50
Scala adoption rates
StackOverflow 2019[1] survey claims that
almost 60% of respondents have tried
Scala and are interested in working
with it
Approximately 4% of developers are use
Scala as their main language at work[2]
Source: insights.stackoverflow.com/survey/2019
38/50
Scala and productivity
Source: bit.ly/2qombqM
39/50
Scala’s functional programming style
allows writing 2 times less code[1]
Reading Scala code is 2 times faster on
average[1]
Research data[2] shows that Scala
projects need ~30% less bug fixes
1.
2.
bit.ly/37eiWTH
arxiv.org/pdf/1901.10220.pdf
40/50
Scala's performance
Uses JVM as the main
runtime platform
Can be compiled to a native
binary
Up to 35% performance
increase with GraalVM[1]
Utilizes incremental
compilation for only a few
seconds delay per build
1.
2.
graalvm.org/scala
bit.ly/330Ugue
41/50
Scala is a rapidly developed
language used and supported
by many, that can offer
substantial productivity
benefits while keeping
reasonable performance
42/50
Scala in real
world
Areas of application
Scala vs. Java & Kotlin
Scala vs. Python
Scala vs. Golang
Future prospects
43/50
Areas of application
ML pipelines
Resilient applications
Distributed systems
Small or simple systems
Low-level manipulation
UI-centered applications
44/50
Scala vs. Java & Kotlin
Java & Kotlin
Vast amount of libraries
for different needs
Reasonable performance
for most programming
tasks
Object-oriented
approach
JVM as runtime platform
Scala
Fully compatible with Java
and Kotlin libraries
Performance competitive
with Java/Kotlin
Allows for both FP and OOP
Target JVM, browser and
native environments
45/50
Scala vs. Python
Python
Ad-hoc Spark & Hadoop
integration
Easy to prototype ideas
Great number of math
and visualisation libraries
Relatively slow on big
volumes on data
Scala
Native Spark & Hadoop
integration
Compile-time verification
Mainly ML-oriented libraries
Performs well on terabytes
of data
46/50
Scala vs. Golang
Golang
Fixed imperative style
Blazing fast speed
Concurrency primitives
on the language level
Scala
Extensible declarative style
Reasonable performance
Libraries for scalability
47/50
Future prospects
Next major version of Scala is
coming at the end of 2020.
Take a look at:
dotty.epfl.ch
48/50
49/50
Thank you for
your
attention!
Any
questions?
@a7emenov
Find me on ,
and
Twitter
Telegram Github
50/50

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Multi cluster, multitenant and hierarchical kafka messaging service slideshare
Multi cluster, multitenant and hierarchical kafka messaging service   slideshareMulti cluster, multitenant and hierarchical kafka messaging service   slideshare
Multi cluster, multitenant and hierarchical kafka messaging service slideshare
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
scalar.pdf
scalar.pdfscalar.pdf
scalar.pdf
 
What's new in Scala 2.13?
What's new in Scala 2.13?What's new in Scala 2.13?
What's new in Scala 2.13?
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Towards Functional Programming through Hexagonal Architecture
Towards Functional Programming through Hexagonal ArchitectureTowards Functional Programming through Hexagonal Architecture
Towards Functional Programming through Hexagonal Architecture
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 

Ähnlich wie Gentle Introduction to Scala

Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
Aarti P
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
Satish Verma
 

Ähnlich wie Gentle Introduction to Scala (20)

Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Beauty and/or elegance in functional programming
Beauty and/or elegance in functional programmingBeauty and/or elegance in functional programming
Beauty and/or elegance in functional programming
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Clean Code
Clean CodeClean Code
Clean Code
 
Functional Programming with C#
Functional Programming with C#Functional Programming with C#
Functional Programming with C#
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Compiler Construction for DLX Processor
Compiler Construction for DLX Processor Compiler Construction for DLX Processor
Compiler Construction for DLX Processor
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Simulation Tool - Plugin Development
Simulation Tool - Plugin DevelopmentSimulation Tool - Plugin Development
Simulation Tool - Plugin Development
 
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 

Mehr von Fangda Wang

Mehr von Fangda Wang (11)

[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?[WWCode] How aware are you of your deciding model?
[WWCode] How aware are you of your deciding model?
 
Under the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeedUnder the hood of architecture interviews at indeed
Under the hood of architecture interviews at indeed
 
How Indeed asks coding interview questions
How Indeed asks coding interview questionsHow Indeed asks coding interview questions
How Indeed asks coding interview questions
 
Types are eating the world
Types are eating the worldTypes are eating the world
Types are eating the world
 
From ic to tech lead
From ic to tech leadFrom ic to tech lead
From ic to tech lead
 
Introduction to japanese tokenizer
Introduction to japanese tokenizerIntroduction to japanese tokenizer
Introduction to japanese tokenizer
 
To pair or not to pair
To pair or not to pairTo pair or not to pair
To pair or not to pair
 
Balanced Team
Balanced TeamBalanced Team
Balanced Team
 
Functional programming and Elm
Functional programming and ElmFunctional programming and Elm
Functional programming and Elm
 
Elm at large (companies)
Elm at large (companies)Elm at large (companies)
Elm at large (companies)
 
Data science tools of the trade
Data science tools of the tradeData science tools of the trade
Data science tools of the trade
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Gentle Introduction to Scala

  • 1. A Gentle Introduction to Scala and FP 1/50
  • 2. Hi, I'm Anton! Functional programmer Scala enthusiast Applications engineer at Rakuten @a7emenov Find me on , and Twitter Telegram Github 2/50
  • 3. Agenda Introduce Scala as a language Give examples of universal FP constructs Show benefits of those constructs Compare Scala to other languages 3/50
  • 4. What is Scala as a language? 4/50
  • 5. Scala combines object- oriented and functional programming in one concise, high-level language. 5/50
  • 6. What lies at the core of FP? 6/50
  • 7. What lies at the core of FP? Absence of side-effects in computations 6/50
  • 8. What lies at the core of FP? Absence of side-effects in computations Function composition 6/50
  • 9. What lies at the core of FP? Absence of side-effects in computations Function composition Immutable data 6/50
  • 10. What lies at the core of FP? Absence of side-effects in computations Function composition Immutable data Dark magic 6/50
  • 11. What lies at the core of FP? Absence of side-effects in computations Function composition Immutable data Dark magic ? 6/50
  • 12. What lies at the core of FP? Absence of side-effects in computations Function composition Immutable data Dark magic All of it 7/50
  • 13. Functional programming is a way of solving problems which centers around combining side-effect-free constructs in a declarative manner 8/50
  • 14. Declarative vs. imperative Imperative for (int i = 1; i 5; i ) { int temp = i * 2; if (temp < 6) { System.out.println(temp); } } Declarative (1 to 5) .map(_ * 2) .filter(_ < 6) .foreach(println) 9/50
  • 15. Deep dive FP & Scala Immutability Pure functions Referential transparency Algebraic data types Pattern matching Recursion 10/50
  • 16. Value mutation Variables Can be changed anytime Can be changed from any place that has a reference Exposing private state requires explicit copying Immutable values Can't be changed Create new values instead of changing the old ones No mutable state 11/50
  • 18. Immutable values Benefits Reduce concurrency issues No shared mutable state Easier local reasoning Drawbacks Extra memory allocations 13/50
  • 19. Pure functions Side-effect freeSide-effect free - everything that a function does can be observed in its return value: TotalTotal - return values for every possible input DeterministicDeterministic - given same inputs, return same outputs Locally scopedLocally scoped - no effect on the state outside of the function's scope 14/50
  • 20. Referential transparency All calls to a function can be substituted with its return value 15/50
  • 21. Referential transparency Not transparent def getInt(l: Int, r: Int): Int = Random.between(l, r) def increment(x: Int): Int = { val res = x + 1 destroyTheWorld() res } Transparent def fact(n: Int): Int = { var res = 1 var i = 1 while (i n) { res = res * i i = i + 1 } res } 16/50
  • 22. Business value with pure functions How can programs be useful without any side-effects? 17/50
  • 23. Business value with pure functions Describe side-effecting computations instead of instantly running them Combine all side-effects to produce a single description Run the resulting computation in the main method 18/50
  • 24. Business value with pure functions def saveValue(key: String, value: Int): IO[Unit] = ? def getValue(key: String): IO[Option[Int]] = ??? def rateLimit[A](program: IO[A]): IO[A] = ??? val program: IO[Option[Int]] = ratelimit( saveValue("key", 1) >> getValue("key") ) val result: Option[Int] = program.runSyncUnsafe() 19/50
  • 25. Business value with pure functions saveValue("key", 1) >> getValue("key") def saveValue(key: String, value: Int): IO[Unit] = ??? def getValue(key: String): IO[Option[Int]] = ??? def rateLimit[A](program: IO[A]): IO[A] = ??? val program: IO[Option[Int]] = ratelimit( ) val result: Option[Int] = program.runSyncUnsafe() 19/50
  • 26. Business value with pure functions def rateLimit[A](program: IO[A]): IO[A] = ??? def saveValue(key: String, value: Int): IO[Unit] = ??? def getValue(key: String): IO[Option[Int]] = ??? val program: IO[Option[Int]] = ratelimit( saveValue("key", 1) >> getValue("key") ) val result: Option[Int] = program.runSyncUnsafe() 19/50
  • 27. Business value with pure functions val program: IO[Option[Int]] = ratelimit( saveValue("key", 1) >> getValue("key") ) def saveValue(key: String, value: Int): IO[Unit] = ??? def getValue(key: String): IO[Option[Int]] = ??? def rateLimit[A](program: IO[A]): IO[A] = ??? val result: Option[Int] = program.runSyncUnsafe() 19/50
  • 28. Business value with pure functions val result: Option[Int] = program.runSyncUnsafe() def saveValue(key: String, value: Int): IO[Unit] = ??? def getValue(key: String): IO[Option[Int]] = ??? def rateLimit[A](program: IO[A]): IO[A] = ??? val program: IO[Option[Int]] = ratelimit( saveValue("key", 1) >> getValue("key") ) 19/50
  • 29. Pure functions Benefits Ultimate local reasoning Parallelizable execution Easy testing Drawbacks Extra object allocations Require an effect system or a library to operate mutable state 20/50
  • 30. Algebraic data types Product typesProduct types - define how to create a value (“has a field” relationship) Sum typesSum types - define the range of values (“is one of” relationship) Product types + Sum types = Algebraic data types 21/50
  • 31. Product types Traditional classes public class Person { // Which is the primary constructor? // Are the constructors related? public Person(Age age, Year birthYear) { /* ... */ } public Person(Year currentYear, Age age) { /* ... */ } public Person(Year currentYear, Year birthYear) { /* ... */ } } 22/50
  • 32. Product types Traditional classes public class Person { // Which is the primary constructor? // Are the constructors related? public Person(Age age, Year birthYear) { /* ... */ } public Person(Year currentYear, Age age) { /* ... */ } public Person(Year currentYear, Year birthYear) { /* ... */ } } Product types // Primary constructor defines the product case class Person(age: Age, birthYear: Year) { // Supplementary constructors // must use the primary constructor def this(currentYear: Year, age: Age) { this(age, currentYear - age) } def this(currentYear: Year, birthYear: Year { this(currentYear - birthYear, birthYear } 22/50
  • 33. Sum types Traditional interfaces public interface User { /* Common methods */ } public class Client implements User { /* Client-specific methods */ } public class Admin implements User { /* Admin-specific methods */ } void process(User user) { // How can this function reason about a // possible implementations of User? } 23/50
  • 34. Sum types Traditional interfaces public interface User { /* Common methods */ } public class Client implements User { /* Client-specific methods */ } public class Admin implements User { /* Admin-specific methods */ } void process(User user) { // How can this function reason about a // possible implementations of User? } Sum types // "Sealed" allows the compiler to find // all descendants of User sealed trait User { /* Common methods */ } case class Client extends User { /* Client-specific methods */ } case class Admin extends User { /* Admin-specific methods */ } def process(user: User): IO[Unit] = { // The function now has knowledge that // there are only 2 options for "user" } 23/50
  • 35. Pattern matching Data construction sealed trait User case class Client(email: String role: String) extends User case class Admin(name: String, scopes: List[String]) extends User val client: User = Client("test@email.com", "manager") val admin: User = Admin("Jane", List("emails", "orders") Data deconstruction def process(u: User): IO[Unit] = u match { case Client(email, "manager") logManager(email) case Admin(name, Nil) logAdmin(name, "No scopes") case Admin(name, List("emails")) logAdmin(name, "Only 'emails' scope") case Admin(name, scopes) if scopes.contains("orders") logAdmin(name, "Has 'orders' scope") case _ logDefault("Default action") } 24/50
  • 36. Pattern matching Benefits Compile-time type checks Exhaustiveness checks Enhanced code readability Extensible Drawbacks Extra memory allocations 25/50
  • 37. Recursion Declarative Native mapping to many real-world problems Can be as efficient as a loop* import scala.annotation.tailrec @tailrec def foldLeft[R](list: List[Int]) (acc: R) (f: (R, Int) R): R = list match { case Nil acc case head :: tail foldLeft(tail)(f(acc, head))(f) } // "12345" foldLeft(List(1, 2, 3, 4, 5))(acc = "") { (acc, num) acc + num.toString } 26/50
  • 38. FP in Scala - covered points Immutability Pure functions Referential transparency Algebraic data types Pattern matching Recursion over iteration 27/50
  • 40. Higher-kinded types Simple types:Simple types: Int, String, User Unary typeUnary type constructors:constructors: List[A], Future[A] Binary typeBinary type constructors:constructors: Either[A, B], Tuple[A, B] Higher-kinded typesHigher-kinded types: trait Processor[L[_]] { def run[A](l: L[A]): Unit } val listP = new Processor[List] { def run[A](l: List[A]): Unit = l.foreach(println) } listP.run(List(1, 2, 3)) listP.run(List("a", "b", "c")) 29/50
  • 41. For comprehensions def getProfile(userName: String): Option[Profile] = ??? def getComments(profile: Profile): Option[List[Comment]] = ??? def getBonus(profile: Profile): Option[Int] = ??? val updatedComments = for { profile getUserProfile("Jack") comments getComments(profile) bonus getBonus(profile) } yield comments.take(10) .map( .increaseRating(bonus)) 30/50
  • 42. For comprehensions Define coherent syntax for sequential composition Syntactic sugar for every type with map, flatMap and withFilter methods Can be used with any type supplying these methods Take advantage of pattern matching 31/50
  • 43. Implicit values Provide automatic value forwarding Resolved at compile- time Can be derived from existing context case class Foo(id: Int) object Foo { implicit val o: Ordering[Foo] = Ordering.by(_.id) } def sortFoo(foos: List[Foo]) (implicit o: Ordering[Foo]) = foos.sorted(o) sortFoo( 32/50
  • 44. Syntax extensions Allow to create ad-hoc methods for code reuse and readability Can be used for types imported from other projects/libraries implicit class StringSyntax (s: String) { def filterDigits: String = s.filter(_.isDigit) } // "123" "1a2b3c".filterDigits 33/50
  • 45. Macros Eliminate boilerplate code Provide implementations in compile time Allow to extract code meta data import io.circe._ import io.circe.generic.semiauto. case class Foo(a: Int, b: String) case class Bar( id: Option[String], foo: Foo ) val barDecoder: Decoder[Bar] = deriveDecoder[Foo].map(foo Bar(id = None, foo = foo) ) 34/50
  • 46. Scala language - covered points Higher-kinded types For-comprehensions Implicit values Syntax extensions Macros 35/50
  • 47. Scala in numbers Development and adoption rates Effect on productivity Performance 36/50
  • 48. Scala adoption rates Maintained intense release schedule for over 15 years Development of the language is supported by the European government and enterprise investors[1] As popular as Kotlin for backend development[2] and is 20% more widespread company-wise[3] 1. 2. 3. bit.ly/2Xr3bnH bit.ly/2rPlUgW api.github.com stackshare.io/scala 37/50
  • 49. Scala adoption rates StackOverflow 2019[1] survey claims that almost 60% of respondents have tried Scala and are interested in working with it Approximately 4% of developers are use Scala as their main language at work[2] Source: insights.stackoverflow.com/survey/2019 38/50
  • 50. Scala and productivity Source: bit.ly/2qombqM 39/50
  • 51. Scala’s functional programming style allows writing 2 times less code[1] Reading Scala code is 2 times faster on average[1] Research data[2] shows that Scala projects need ~30% less bug fixes 1. 2. bit.ly/37eiWTH arxiv.org/pdf/1901.10220.pdf 40/50
  • 52. Scala's performance Uses JVM as the main runtime platform Can be compiled to a native binary Up to 35% performance increase with GraalVM[1] Utilizes incremental compilation for only a few seconds delay per build 1. 2. graalvm.org/scala bit.ly/330Ugue 41/50
  • 53. Scala is a rapidly developed language used and supported by many, that can offer substantial productivity benefits while keeping reasonable performance 42/50
  • 54. Scala in real world Areas of application Scala vs. Java & Kotlin Scala vs. Python Scala vs. Golang Future prospects 43/50
  • 55. Areas of application ML pipelines Resilient applications Distributed systems Small or simple systems Low-level manipulation UI-centered applications 44/50
  • 56. Scala vs. Java & Kotlin Java & Kotlin Vast amount of libraries for different needs Reasonable performance for most programming tasks Object-oriented approach JVM as runtime platform Scala Fully compatible with Java and Kotlin libraries Performance competitive with Java/Kotlin Allows for both FP and OOP Target JVM, browser and native environments 45/50
  • 57. Scala vs. Python Python Ad-hoc Spark & Hadoop integration Easy to prototype ideas Great number of math and visualisation libraries Relatively slow on big volumes on data Scala Native Spark & Hadoop integration Compile-time verification Mainly ML-oriented libraries Performs well on terabytes of data 46/50
  • 58. Scala vs. Golang Golang Fixed imperative style Blazing fast speed Concurrency primitives on the language level Scala Extensible declarative style Reasonable performance Libraries for scalability 47/50
  • 59. Future prospects Next major version of Scala is coming at the end of 2020. Take a look at: dotty.epfl.ch 48/50
  • 60. 49/50
  • 61. Thank you for your attention! Any questions? @a7emenov Find me on , and Twitter Telegram Github 50/50