SlideShare a Scribd company logo
Scala:	
  	
  
Higher	
  Order	
  Func2ons	
  
Higher	
  order	
  func2ons	
  
•  Allow	
  behavior	
  to	
  be	
  passed	
  in	
  as	
  arguments	
  
•  Return	
  func2ons	
  from	
  method	
  calls	
  
•  Allows	
  for	
  more	
  abstract	
  constructs	
  =>	
  more	
  
library-­‐like,	
  requiring	
  less	
  maintenance	
  
Example:	
  Filter	
  
•  Set[A]	
  contains	
  a	
  method	
  called	
  filter:	
  

filter(p: A => Boolean): Set[A]

•  Use	
  it	
  to	
  select	
  elements	
  that	
  comply	
  with	
  a	
  
predicate
Example:	
  Filter	
  
val set = (-10 to 10).toSet
def isEven(i:Int) = i % 2 == 0

// Set[A].filter(p: A => Boolean): Set[A]

set.filter(i => isEven(i))
set.filter(isEven(_))
set.filter(isEven)
Example:	
  Map	
  
•  List[A]	
  contains	
  a	
  method	
  called	
  map:	
  

map[B](f: A => B): List[B]

•  Use	
  it	
  to	
  transform	
  each	
  element	
  in	
  the	
  list	
  by	
  
applying	
  f
Example:	
  Map	
  
def capitalize(s: String) = 
s.head.toUpper + s.tail.toLowerCase

val list = List("samwise", "meriadoc",
"peregrin")

list.map(capitalize)
Some	
  interes2ng	
  examples	
  
•  Collec2ons	
  
– best	
  implementa2on	
  depends	
  on	
  collec2on:	
  
recursive,	
  itera2ve,	
  with	
  auxiliary	
  mutable	
  state.	
  
•  Parallel	
  collec2ons	
  
– interface	
  is	
  fundamentally	
  the	
  same,	
  
paralleliza2on	
  is	
  hidden	
  from	
  you.	
  
•  Futures	
  
– transform	
  the	
  value	
  once	
  it's	
  available	
  
How To Use Higher Order Functions in Scala

More Related Content

PPTX
Grid search (parameter tuning)
PPTX
PPTX
support vector regression
PDF
How to use Map() Filter() and Reduce() functions in Python | Edureka
PPTX
knn classification
PPTX
random forest regression
PPTX
polynomial linear regression
PPTX
logistic regression with python and R
Grid search (parameter tuning)
support vector regression
How to use Map() Filter() and Reduce() functions in Python | Edureka
knn classification
random forest regression
polynomial linear regression
logistic regression with python and R

What's hot (20)

PPTX
Operators in mule dataweave
PPTX
decision tree regression
PPTX
simple linear regression
PPTX
multiple linear regression
PPTX
svm classification
PDF
SupportVectorRegression
PPT
Csc1100 lecture07 ch07_pt1-1
PPTX
Javascript - Array - Writing
PPT
Javascript - Getting Good with Loop and Array
PPTX
PCA and LDA in machine learning
PPTX
Javascript - Array - Creating Array
PPT
20120518 power shell_文字處理及輕量測試
PPTX
PPTX
Binary Heap Tree, Data Structure
PDF
Testing in the World of Functional Programming
PDF
Heap and heapsort
PDF
Pandas,scipy,numpy cheatsheet
Operators in mule dataweave
decision tree regression
simple linear regression
multiple linear regression
svm classification
SupportVectorRegression
Csc1100 lecture07 ch07_pt1-1
Javascript - Array - Writing
Javascript - Getting Good with Loop and Array
PCA and LDA in machine learning
Javascript - Array - Creating Array
20120518 power shell_文字處理及輕量測試
Binary Heap Tree, Data Structure
Testing in the World of Functional Programming
Heap and heapsort
Pandas,scipy,numpy cheatsheet
Ad

Viewers also liked (20)

PPTX
Séminaire "Home sweet home"
PPTX
CabMe(intercity travels)
PDF
Towards Aggregate Programming in Scala
PDF
[Start] Scala
PPTX
CabMe|intercity travels
PDF
Lecture 3
PPTX
Lambda выражения и Java 8
PDF
Spark Jobserver
PDF
Lambda Expressions and Java 8 - Lambda Calculus, Lambda Expressions, Syntacti...
PDF
Introduction to the Actor Model
ODP
Functions & closures
PDF
Functional Programming for OO Programmers (part 2)
PDF
Introduction to parallel and distributed computation with spark
PDF
ScalaTrainings
PPTX
Scala meetup - Intro to spark
PDF
Apache Spark RDDs
PDF
Scala : language of the future
PDF
Scala Days NYC 2016
PDF
Introduction to Functional Programming with Scala
Séminaire "Home sweet home"
CabMe(intercity travels)
Towards Aggregate Programming in Scala
[Start] Scala
CabMe|intercity travels
Lecture 3
Lambda выражения и Java 8
Spark Jobserver
Lambda Expressions and Java 8 - Lambda Calculus, Lambda Expressions, Syntacti...
Introduction to the Actor Model
Functions & closures
Functional Programming for OO Programmers (part 2)
Introduction to parallel and distributed computation with spark
ScalaTrainings
Scala meetup - Intro to spark
Apache Spark RDDs
Scala : language of the future
Scala Days NYC 2016
Introduction to Functional Programming with Scala
Ad

Similar to How To Use Higher Order Functions in Scala (20)

PPT
Collections
PPT
Collections in Java
PPT
Collections
PPT
collections
PDF
TI1220 Lecture 8: Traits & Type Parameterization
PPTX
Monads and friends demystified
PPT
Scala collection
PDF
JAVA PROGRAMMING - The Collections Framework
PDF
Collections Framework Beginners Guide 2
PDF
Collections Framework Begineers guide 2
PPTX
Xebicon2013 scala vsjava_final
PDF
Lecture 4
PPTX
Data structure array
PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PDF
Map, Reduce and Filter in Swift
PPTX
Functional Programming in Swift
PPT
standard template library(STL) in C++
PDF
Practical cats
PDF
Python - Lecture 12
PDF
Scala by Luc Duponcheel
Collections
Collections in Java
Collections
collections
TI1220 Lecture 8: Traits & Type Parameterization
Monads and friends demystified
Scala collection
JAVA PROGRAMMING - The Collections Framework
Collections Framework Beginners Guide 2
Collections Framework Begineers guide 2
Xebicon2013 scala vsjava_final
Lecture 4
Data structure array
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
Map, Reduce and Filter in Swift
Functional Programming in Swift
standard template library(STL) in C++
Practical cats
Python - Lecture 12
Scala by Luc Duponcheel

More from BoldRadius Solutions (18)

PDF
Introduction to the Typesafe Reactive Platform
PDF
Partial Functions in Scala
PDF
Towards Reliable Lookups - Scala By The Bay
PDF
How You Convince Your Manager To Adopt Scala.js in Production
PDF
Why Not Make the Transition from Java to Scala?
PDF
String Interpolation in Scala | BoldRadius
PPTX
Value Classes in Scala | BoldRadius
PDF
Scala Days Highlights | BoldRadius
PDF
What Are For Expressions in Scala?
PPTX
Domain Driven Design Through Onion Architecture
PDF
Pattern Matching in Scala
PDF
What are Sealed Classes in Scala?
PDF
Scala: Collections API
PDF
Immutability in Scala
PDF
Scala Days 2014: Pitching Typesafe
PDF
Code Brevity in Scala
PDF
Demonstrating Case Classes in Scala
PDF
Functional Programming - Worth the Effort
Introduction to the Typesafe Reactive Platform
Partial Functions in Scala
Towards Reliable Lookups - Scala By The Bay
How You Convince Your Manager To Adopt Scala.js in Production
Why Not Make the Transition from Java to Scala?
String Interpolation in Scala | BoldRadius
Value Classes in Scala | BoldRadius
Scala Days Highlights | BoldRadius
What Are For Expressions in Scala?
Domain Driven Design Through Onion Architecture
Pattern Matching in Scala
What are Sealed Classes in Scala?
Scala: Collections API
Immutability in Scala
Scala Days 2014: Pitching Typesafe
Code Brevity in Scala
Demonstrating Case Classes in Scala
Functional Programming - Worth the Effort

Recently uploaded (20)

PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
history of c programming in notes for students .pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Nekopoi APK 2025 free lastest update
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administraation Chapter 3
PDF
Digital Systems & Binary Numbers (comprehensive )
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PTS Company Brochure 2025 (1).pdf.......
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Which alternative to Crystal Reports is best for small or large businesses.pdf
Computer Software and OS of computer science of grade 11.pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily
VVF-Customer-Presentation2025-Ver1.9.pptx
CHAPTER 2 - PM Management and IT Context
history of c programming in notes for students .pptx
Transform Your Business with a Software ERP System
Nekopoi APK 2025 free lastest update
Softaken Excel to vCard Converter Software.pdf
Reimagine Home Health with the Power of Agentic AI​
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administraation Chapter 3
Digital Systems & Binary Numbers (comprehensive )

How To Use Higher Order Functions in Scala

  • 1. Scala:     Higher  Order  Func2ons  
  • 2. Higher  order  func2ons   •  Allow  behavior  to  be  passed  in  as  arguments   •  Return  func2ons  from  method  calls   •  Allows  for  more  abstract  constructs  =>  more   library-­‐like,  requiring  less  maintenance  
  • 3. Example:  Filter   •  Set[A]  contains  a  method  called  filter:   filter(p: A => Boolean): Set[A] •  Use  it  to  select  elements  that  comply  with  a   predicate
  • 4. Example:  Filter   val set = (-10 to 10).toSet def isEven(i:Int) = i % 2 == 0 // Set[A].filter(p: A => Boolean): Set[A] set.filter(i => isEven(i)) set.filter(isEven(_)) set.filter(isEven)
  • 5. Example:  Map   •  List[A]  contains  a  method  called  map:   map[B](f: A => B): List[B] •  Use  it  to  transform  each  element  in  the  list  by   applying  f
  • 6. Example:  Map   def capitalize(s: String) = s.head.toUpper + s.tail.toLowerCase val list = List("samwise", "meriadoc", "peregrin") list.map(capitalize)
  • 7. Some  interes2ng  examples   •  Collec2ons   – best  implementa2on  depends  on  collec2on:   recursive,  itera2ve,  with  auxiliary  mutable  state.   •  Parallel  collec2ons   – interface  is  fundamentally  the  same,   paralleliza2on  is  hidden  from  you.   •  Futures   – transform  the  value  once  it's  available