SlideShare a Scribd company logo
1 of 8
First class functions
First class functions
val even = Function[Int, Boolean] {
    def apply(i: Int) = i % 2 == 0
}


val even: (Int => Boolean) = (i: Int) => i % 2 == 0
val even = (i: Int) => i % 2 == 0


even.apply(42)           // true
even(13)              // false
First class functions
val numbers = List(1, 2, 3, 4, 5)


numbers.filter(even)                   // List(2, 4)


numbers.filter((i: Int) => i > 2)   // List(3, 4, 5)
numbers.filter(i => i > 2)          // List(3, 4, 5)
numbers.filter(_ > 2)                  // List(3, 4, 5)
Collections
numbers.filter(i => i > 2)   // List(3, 4, 5)
numbers.find(i => i > 2)     // Some(3)
numbers.exists(i => i > 2) // true
numbers.forall(i => i > 2)   // false


numbers.map(i => i * 2)      // List(2, 4, 6, 8, 10)


numbers.foldLeft(0) { (a, b) => a + b }     // 15
Deferred execution - constructed example

 helloButton.addActionListener(e =>
     println(“Hello World!”)
 )
Closures
val people = List(Person(“Alf”), Person(“Fredrik”))


val name = “Fredrik”
val nameFilter = (p: Person) => p.name == name


people.filter(nameFilter)   // Person(“Fredrik”)
Closures
val people = List(Person(“Alf”), Person(“Fredrik”))


var name = “Fredrik”
val nameFilter = (p: Person) => p.name == name


people.filter(nameFilter)   // Person(“Fredrik”)
name = “Alf”
people.filter(nameFilter)   // Person(“Alf”)
Tasks (30 min)
Open the 'first-class-functions' project
Tests in package
scalaexamples.firstclassfunctions
Add @Test to one and one method
Follow instructions in the code
Make the tests pass

More Related Content

What's hot

Facebook fql and tweepy
Facebook fql and tweepyFacebook fql and tweepy
Facebook fql and tweepy
DaeMyung Kang
 

What's hot (9)

Beginning web programming with PHP [PHP 101-02]
Beginning web programming with PHP [PHP 101-02]Beginning web programming with PHP [PHP 101-02]
Beginning web programming with PHP [PHP 101-02]
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 EvolutionАлександр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 Evolution
 
Mamun
MamunMamun
Mamun
 
Data Structure
Data StructureData Structure
Data Structure
 
Decompiladores
DecompiladoresDecompiladores
Decompiladores
 
Swift勉強会第3回 〜UIを作ろう②〜
Swift勉強会第3回 〜UIを作ろう②〜Swift勉強会第3回 〜UIを作ろう②〜
Swift勉強会第3回 〜UIを作ろう②〜
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Static type checking in python
Static type checking in pythonStatic type checking in python
Static type checking in python
 
Facebook fql and tweepy
Facebook fql and tweepyFacebook fql and tweepy
Facebook fql and tweepy
 

Viewers also liked

Viewers also liked (9)

1.7 functional programming
1.7 functional programming1.7 functional programming
1.7 functional programming
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
 
2.4 xml support
2.4 xml support2.4 xml support
2.4 xml support
 
1.3 tools and-repl
1.3 tools and-repl1.3 tools and-repl
1.3 tools and-repl
 
1.5 pattern matching
1.5 pattern matching1.5 pattern matching
1.5 pattern matching
 
1.6 oo traits
1.6 oo traits1.6 oo traits
1.6 oo traits
 
2.3 implicits
2.3 implicits2.3 implicits
2.3 implicits
 
2.2 higher order-functions
2.2 higher order-functions2.2 higher order-functions
2.2 higher order-functions
 
2.5 the quiz-game
2.5 the quiz-game2.5 the quiz-game
2.5 the quiz-game
 

Similar to 1.4 first class-functions

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 

Similar to 1.4 first class-functions (20)

Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
A Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptA Skeptics guide to functional style javascript
A Skeptics guide to functional style javascript
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Kotlin Collections
Kotlin CollectionsKotlin Collections
Kotlin Collections
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Option, Either, Try and what to do with corner cases when they arise
Option, Either, Try and what to do with corner cases when they ariseOption, Either, Try and what to do with corner cases when they arise
Option, Either, Try and what to do with corner cases when they arise
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Kotlin collections
Kotlin collectionsKotlin collections
Kotlin collections
 
Arrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com ArrowArrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com Arrow
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 

Recently uploaded

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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
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​
 
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
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

1.4 first class-functions

  • 2. First class functions val even = Function[Int, Boolean] { def apply(i: Int) = i % 2 == 0 } val even: (Int => Boolean) = (i: Int) => i % 2 == 0 val even = (i: Int) => i % 2 == 0 even.apply(42) // true even(13) // false
  • 3. First class functions val numbers = List(1, 2, 3, 4, 5) numbers.filter(even) // List(2, 4) numbers.filter((i: Int) => i > 2) // List(3, 4, 5) numbers.filter(i => i > 2) // List(3, 4, 5) numbers.filter(_ > 2) // List(3, 4, 5)
  • 4. Collections numbers.filter(i => i > 2) // List(3, 4, 5) numbers.find(i => i > 2) // Some(3) numbers.exists(i => i > 2) // true numbers.forall(i => i > 2) // false numbers.map(i => i * 2) // List(2, 4, 6, 8, 10) numbers.foldLeft(0) { (a, b) => a + b } // 15
  • 5. Deferred execution - constructed example helloButton.addActionListener(e => println(“Hello World!”) )
  • 6. Closures val people = List(Person(“Alf”), Person(“Fredrik”)) val name = “Fredrik” val nameFilter = (p: Person) => p.name == name people.filter(nameFilter) // Person(“Fredrik”)
  • 7. Closures val people = List(Person(“Alf”), Person(“Fredrik”)) var name = “Fredrik” val nameFilter = (p: Person) => p.name == name people.filter(nameFilter) // Person(“Fredrik”) name = “Alf” people.filter(nameFilter) // Person(“Alf”)
  • 8. Tasks (30 min) Open the 'first-class-functions' project Tests in package scalaexamples.firstclassfunctions Add @Test to one and one method Follow instructions in the code Make the tests pass

Editor's Notes

  1. Vanskelig å forklare foldleft med flere parameter lister?
  2. Nevne at dette er et fake eksempel