A partial function is a function that may not be defined for all values of its argument type. In Scala, partial functions
are unary functions implementing the PartialFunction[A, B]
trait, where A
is the argument type and B
the result type.
To define a partial function, use a case
identical to those used in match
expressions:
val doubledOdds: PartialFunction[Int, Int] = {
case i if i % 2 == 1 => i * 2
}
To check if a partial function is defined for an argument, use the isDefinedAt
method:
doubledOdds.isDefinedAt(3) // true
doubledOdds.isDefinedAt(4) // false
Trying to apply a partial function to an argument not belonging to its domain results in MatchError
:
doubledOdds(4) // Exception in thread "main" scala.MatchError: 4
Using partial functions
A partial function can be passed as an argument to a method:
val res = List(1, 2, 3).collect({ case i if i % 2 == 1 => i * 2 }) // List(2, 6)
You can define a default value for arguments not in domain with applyOrElse
:
doubledOdds.applyOrElse(4, _ + 1) // 5
Two partial function can be composed with orElse
, the second function will be applied for arguments where the first
one is not defined:
val incrementedEvens: PartialFunction[Int, Int] = {
case i if i % 2 == 0 => i + 1
}
val res2 = List(1, 2, 3).collect(doubledOdds.orElse(incrementedEvens)) // List(2, 3, 6)
Contributors to this page:
Contents
- Introduction
- Scala Features
- Why Scala 3?
- A Taste of Scala
- Hello, World!
- The REPL
- Variables and Data Types
- Control Structures
- Domain Modeling
- Methods
- First-Class Functions
- Singleton Objects
- Collections
- Contextual Abstractions
- Toplevel Definitions
- Summary
- A First Look at Types
- String Interpolation
- Control Structures
- Domain Modeling
- Tools
- OOP Modeling
- FP Modeling
- Methods
- Method Features
- Main Methods in Scala 3
- Summary
- Functions
- Anonymous Functions
- Function Variables
- Partial Functions
- Eta-Expansion
- Higher-Order Functions
- Write Your Own map Method
- Creating a Method That Returns a Function
- Summary
- Packaging and Imports
- Scala Collections
- Collections Types
- Collections Methods
- Summary
- Functional Programming
- What is Functional Programming?
- Immutable Values
- Pure Functions
- Functions Are Values
- Functional Error Handling
- Summary
- Types and the Type System
- Inferred Types
- Generics
- Intersection Types
- Union Types
- Algebraic Data Types
- Variance
- Opaque Types
- Structural Types
- Dependent Function Types
- Other Types
- Contextual Abstractions
- Extension Methods
- Context Parameters
- Context Bounds
- Given Imports
- Type Classes
- Multiversal Equality
- Implicit Conversions
- Summary
- Concurrency
- Scala Tools
- Building and Testing Scala Projects with sbt
- Worksheets
- Interacting with Java
- Scala for Java Developers
- Scala for JavaScript Developers
- Scala for Python Developers
- Where To Go Next