SlideShare a Scribd company logo
That new function is called the forward composition of the two functions f and g. In Scala, this operation is written as f andThen g …
…
The forward composition is denoted by (pronounced “before”) and can be defined as
The symbol , means “is defined as”.
We could write the forward composition as a fully parametric function,
def andThen[X, Y, Z](f: X => Y)(g: Y => Z): X => Z = { x => g(f(x)) }
The type signature of this curried function is
This type signature requires the types of the function arguments to match in a certain way, or else the composition is undefined.
The backward composition of two functions f and g works in the opposite order: first g is applied and
then f is applied to the result. Using the symbol (pronounced “after”) for this operation, we can write
In Scala, the backward composition is called compose and used as f compose g. This method may be
Implemented as a fully parametric function
def compose[X, Y, Z](f: Y => X)(g: Z => Y): Z => X = { z => f(g(z)) }
The type signature of this curried function is Sergei	Winitzki
sergei-winitzki-11a6431

More Related Content

What's hot (20)

PDF
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Philip Schwarz
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Philip Schwarz
 
PDF
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
Philip Schwarz
 
PDF
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Philip Schwarz
 
PDF
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Philip Schwarz
 
PDF
Scala 3 enum for a terser Option Monad Algebraic Data Type
Philip Schwarz
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Philip Schwarz
 
PDF
Monads do not Compose
Philip Schwarz
 
PDF
The Functional Programming Triad of Map, Filter and Fold
Philip Schwarz
 
PDF
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
PDF
Simple IO Monad in 'Functional Programming in Scala'
Philip Schwarz
 
PDF
Functors
Philip Schwarz
 
PDF
Scala categorytheory
Knoldus Inc.
 
PDF
State Monad
Philip Schwarz
 
PDF
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
 
ODP
Clojure basics
Knoldus Inc.
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Philip Schwarz
 
PPT
Introduction to Functional Programming in JavaScript
tmont
 
PDF
Monoids - Part 2 - with examples using Scalaz and Cats
Philip Schwarz
 
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Philip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
Philip Schwarz
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Philip Schwarz
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Philip Schwarz
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Philip Schwarz
 
Monads do not Compose
Philip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
Simple IO Monad in 'Functional Programming in Scala'
Philip Schwarz
 
Functors
Philip Schwarz
 
Scala categorytheory
Knoldus Inc.
 
State Monad
Philip Schwarz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
 
Clojure basics
Knoldus Inc.
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5
Philip Schwarz
 
Introduction to Functional Programming in JavaScript
tmont
 
Monoids - Part 2 - with examples using Scalaz and Cats
Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Philip Schwarz
 

Similar to Function Composition - forward composition versus backward composition (20)

PPTX
Composition and inverse of functions
Charliez Jane Soriano
 
PDF
Functional Programming in PHP
pwmosquito
 
PDF
Composition birds-and-recursion
David Atchley
 
PDF
List-based Monadic Computations for Dynamic Languages
Wim Vanderbauwhede
 
PPTX
Generating functions
sweetysweety8
 
PPTX
Functional Programming Concepts for Imperative Programmers
Chris
 
PPTX
Function notation by sadiq
Sadiq Hussain
 
PDF
List-based Monadic Computations for Dynamically Typed Languages (Python version)
Wim Vanderbauwhede
 
PDF
5.2. Function composition
Jan Plaza
 
PPTX
composite functions
Glory Codilla
 
PPTX
Functional programming in javascript
Boris Burdiliak
 
PDF
Matlab differential
pramodkumar1804
 
PDF
fwHugRw0jkicxddgjiiiighhhuiiigxxxcbnjiu42.pdf
hiijhgg
 
PDF
Funcionesreales 160109205602
Andres Narváez Játiva
 
PPTX
function on mathematics
AkashDas124
 
PPTX
The Algebric Functions
itutor
 
PDF
Functional programming java
Maneesh Chaturvedi
 
PPTX
ForLoopandUserDefinedFunctions.pptx
AaliyanShaikh
 
PDF
Functions 2
Tyler Murphy
 
PPTX
2 integration and the substitution methods x
math266
 
Composition and inverse of functions
Charliez Jane Soriano
 
Functional Programming in PHP
pwmosquito
 
Composition birds-and-recursion
David Atchley
 
List-based Monadic Computations for Dynamic Languages
Wim Vanderbauwhede
 
Generating functions
sweetysweety8
 
Functional Programming Concepts for Imperative Programmers
Chris
 
Function notation by sadiq
Sadiq Hussain
 
List-based Monadic Computations for Dynamically Typed Languages (Python version)
Wim Vanderbauwhede
 
5.2. Function composition
Jan Plaza
 
composite functions
Glory Codilla
 
Functional programming in javascript
Boris Burdiliak
 
Matlab differential
pramodkumar1804
 
fwHugRw0jkicxddgjiiiighhhuiiigxxxcbnjiu42.pdf
hiijhgg
 
Funcionesreales 160109205602
Andres Narváez Játiva
 
function on mathematics
AkashDas124
 
The Algebric Functions
itutor
 
Functional programming java
Maneesh Chaturvedi
 
ForLoopandUserDefinedFunctions.pptx
AaliyanShaikh
 
Functions 2
Tyler Murphy
 
2 integration and the substitution methods x
math266
 
Ad

More from Philip Schwarz (20)

PDF
Folding Cheat Sheet Series Titles - a series of 9 decks
Philip Schwarz
 
PDF
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
PDF
List Unfolding - 'unfold' as the Computational Dual of 'fold', and how 'unfol...
Philip Schwarz
 
PDF
Drawing Heighway’s Dragon - Part 4 - Interactive and Animated Dragon Creation
Philip Schwarz
 
PDF
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
Philip Schwarz
 
PDF
Drawing Heighway’s Dragon - Part 3 - Simplification Through Separation of Con...
Philip Schwarz
 
PDF
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
 
PDF
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
 
PDF
Drawing Heighway’s Dragon - Part II - Recursive Function Simplification - Fro...
Philip Schwarz
 
PDF
Drawing Heighway’s Dragon - Recursive Function Rewrite - From Imperative Styl...
Philip Schwarz
 
PDF
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
PDF
Fibonacci Function Gallery - Part 1 (of a series) - with minor corrections
Philip Schwarz
 
PDF
Fibonacci Function Gallery - Part 1 (of a series)
Philip Schwarz
 
PDF
The Debt Metaphor - Ward Cunningham in his 2009 YouTube video
Philip Schwarz
 
PDF
Folding Cheat Sheet Series Titles (so far)
Philip Schwarz
 
PDF
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism - An Example
Philip Schwarz
 
PDF
Folding Cheat Sheet #8 - eighth in a series
Philip Schwarz
 
PDF
Function Applicative for Great Good of Leap Year Function
Philip Schwarz
 
PDF
Folding Cheat Sheet #7 - seventh in a series
Philip Schwarz
 
PDF
Folding Cheat Sheet #6 - sixth in a series
Philip Schwarz
 
Folding Cheat Sheet Series Titles - a series of 9 decks
Philip Schwarz
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
List Unfolding - 'unfold' as the Computational Dual of 'fold', and how 'unfol...
Philip Schwarz
 
Drawing Heighway’s Dragon - Part 4 - Interactive and Animated Dragon Creation
Philip Schwarz
 
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
Philip Schwarz
 
Drawing Heighway’s Dragon - Part 3 - Simplification Through Separation of Con...
Philip Schwarz
 
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
 
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
 
Drawing Heighway’s Dragon - Part II - Recursive Function Simplification - Fro...
Philip Schwarz
 
Drawing Heighway’s Dragon - Recursive Function Rewrite - From Imperative Styl...
Philip Schwarz
 
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
Fibonacci Function Gallery - Part 1 (of a series) - with minor corrections
Philip Schwarz
 
Fibonacci Function Gallery - Part 1 (of a series)
Philip Schwarz
 
The Debt Metaphor - Ward Cunningham in his 2009 YouTube video
Philip Schwarz
 
Folding Cheat Sheet Series Titles (so far)
Philip Schwarz
 
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism - An Example
Philip Schwarz
 
Folding Cheat Sheet #8 - eighth in a series
Philip Schwarz
 
Function Applicative for Great Good of Leap Year Function
Philip Schwarz
 
Folding Cheat Sheet #7 - seventh in a series
Philip Schwarz
 
Folding Cheat Sheet #6 - sixth in a series
Philip Schwarz
 
Ad

Recently uploaded (20)

PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 

Function Composition - forward composition versus backward composition

  • 1. That new function is called the forward composition of the two functions f and g. In Scala, this operation is written as f andThen g … … The forward composition is denoted by (pronounced “before”) and can be defined as The symbol , means “is defined as”. We could write the forward composition as a fully parametric function, def andThen[X, Y, Z](f: X => Y)(g: Y => Z): X => Z = { x => g(f(x)) } The type signature of this curried function is This type signature requires the types of the function arguments to match in a certain way, or else the composition is undefined. The backward composition of two functions f and g works in the opposite order: first g is applied and then f is applied to the result. Using the symbol (pronounced “after”) for this operation, we can write In Scala, the backward composition is called compose and used as f compose g. This method may be Implemented as a fully parametric function def compose[X, Y, Z](f: Y => X)(g: Z => Y): Z => X = { z => f(g(z)) } The type signature of this curried function is Sergei Winitzki sergei-winitzki-11a6431