SlideShare a Scribd company logo
1 of 50
Demystifying
functional effect systems in Scala
Dmitry Karlinsky
Backend Engineer at Wix.com, Data Streams team
twitter: @drkarlinsky linkedin/dkarlinsky github.com/darlinsky
We use ZIO
Demystifying
functional effect systems in Scala
FUNCTIONAL
EFFECT SYSTEMS
Cats Effect
Let’s build a
functional effect system in Scala
FUNCTIONAL
EFFECT SYSTEMS
What are
Functional Effect Systems?
FUNCTIONAL
EFFECT SYSTEMS
non pure
operations such
as IO
Effects
FUNCTIONAL
EFFECT SYSTEMS
Allow describing
effects as values
that can be easily
and safely
composed into
descriptions of whole
programs
Effect Systems
FUNCTIONAL
EFFECT SYSTEMS
How to build an effect system?
Start with
basic Effects
and Runtime
Add
capabilities
Concurrency
with Fibers
FUNCTIONAL
EFFECT SYSTEMS
Agenda
Creating and
combining
effects
1 Failing and
Recovering
from Errors
2 Asynchrony3 Concurrency
with Fibers
4
Creating and
combining effects
01
IO Monad
Data structure for
describing effects
Composition describes
sequencing
TIO
IO Monad
Data structure for
describing effects
Composition describes
sequencing
Derived Operators
TIO
IO Monad
Hello World
EXAMPLEEXAMPLE
IO Monad
Declarative Encoding
(Free Monad)
TIO
Runtime
Minimal Recursive
Implementation
RUNTIME
Runtime
Running Hello World
RUNTIMEEXAMPLE
Failing and Recovering
from Errors
02
Failure
Representing Errors
TIO
Failure
Recovering from Errors
Recover Effect
TIO
Example
Helpers
Example: Fail and
Recover
EXAMPLE
Failure
Recovering from Errors
Runtime
RUNTIME
Example
Example: Fail and
Recover
EXAMPLE
Limitations
Fully synchronous - no way to represent
async effects
Not stack safe
Stack Safety
Example: Foreach
EXAMPLE
STACK SAFETY
Asynchrony
03
Asynchrony:
A process that continues its execution in a
different place or time than the one it started in.
Fabio Labella: “How do Fibers Work? A Peek Under the Hood”
Async Effects
Effect Constructor
TIO
Async Effects
Example
Non Blocking Sleep
EXAMPLE
Async Effects
Runtime
runAsync
RUNTIME
Async Effects
Runtime
Implementation
RUNTIME
Async Effects
Example: Sleep
EXAMPLE
Stack Safety
And Now?
EXAMPLE
Limitations
Not stack safe
Stack Safety
Helper
Executor
STACK SAFETY
Stack Safety
Scheduling effects on a
thread pool
RUNTIME
Stack Safety
And Now?
EXAMPLE
Concurrency with
Fibers
04
Concurrency:
A property of systems where several processes
execute at the same time.
CONCURRENCY
WITH FIBERS
Fibers
TIO.flatMap combinator describes sequences of computations.
Logical threads of execution are not tied to specific OS threads.
New combinators:
fork creates new Fibers
CONCURRENCY
WITH FIBERS
join lets one fiber await the result of another
Concurrency
Fiber Cominators
TIO
Concurrency
Example: Fork Join
EXAMPLE
Concurrency
FiberRuntime
● start()
● onDone()
RUNTIME
Concurrency
FiberRuntime
● eval()
RUNTIME
Concurrency
Example: Fork Join
EXAMPLE
Concurrency
Bonus Example
foreachPar()
EXAMPLE
Start with
a basic IO
monad
Add
capabilities
Concurrency
with Fibers
FUNCTIONAL
EFFECT SYSTEMS
What’s Next
❏ Locking effects to alternative Executors (e.g for blocking effects)
❏ Fiber interruption
❏ Resource management (e.g Bracket)
❏ Fiber supervision - auto interruption of child fibers
❏ Interruption safe resource management
What’s Next
More Resources
● My blog post (that this talk is based on) - goes into more details
● Fabio Labella — How do Fibers Work? A Peek Under the Hood
● FIBER SUPERVISION IN ZIO — Wiem Zine El Abidine, Adam Fraser | Scalar 2020
Thank You
twitter: @drkarlinsky linkedin/dkarlinsky github.com/darlinsky
Q&A
twitter: @drkarlinsky linkedin/dkarlinsky github.com/darlinsky

More Related Content

What's hot

Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
Binu Bhasuran
 
Jfokus functional groovy
Jfokus functional groovyJfokus functional groovy
Jfokus functional groovy
Andres Almiray
 

What's hot (20)

Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroid
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future Task
 
Introduction to RxJava on Android
Introduction to RxJava on AndroidIntroduction to RxJava on Android
Introduction to RxJava on Android
 
02 prepcode
02 prepcode02 prepcode
02 prepcode
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
AutoComplete
AutoCompleteAutoComplete
AutoComplete
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark Arts
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)C#: Understanding ConfigureAwait(false)
C#: Understanding ConfigureAwait(false)
 
VerneMQ @ Paris Erlang User Group June 29th 2015
VerneMQ @ Paris Erlang User Group June 29th 2015VerneMQ @ Paris Erlang User Group June 29th 2015
VerneMQ @ Paris Erlang User Group June 29th 2015
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 
Jfokus functional groovy
Jfokus functional groovyJfokus functional groovy
Jfokus functional groovy
 
Wrapping java in awesomeness aka condensator
Wrapping java in awesomeness aka condensatorWrapping java in awesomeness aka condensator
Wrapping java in awesomeness aka condensator
 
Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in Android
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
 
Asynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NETAsynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NET
 

Similar to Demystifying functional effect systems in Scala

香港六合彩 » SlideShare
香港六合彩 » SlideShare香港六合彩 » SlideShare
香港六合彩 » SlideShare
yayao
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 

Similar to Demystifying functional effect systems in Scala (20)

Introduction to actor model with examples on Akka.NET
Introduction to actor model with examples on Akka.NETIntroduction to actor model with examples on Akka.NET
Introduction to actor model with examples on Akka.NET
 
Role of locking
Role of lockingRole of locking
Role of locking
 
Role of locking- cds
Role of locking- cdsRole of locking- cds
Role of locking- cds
 
香港六合彩 » SlideShare
香港六合彩 » SlideShare香港六合彩 » SlideShare
香港六合彩 » SlideShare
 
Shopzilla On Concurrency
Shopzilla On ConcurrencyShopzilla On Concurrency
Shopzilla On Concurrency
 
Apache commons chain in Spring Framework
Apache commons chain in Spring FrameworkApache commons chain in Spring Framework
Apache commons chain in Spring Framework
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Unleashing the Power of Terraform and Azure DevOps
Unleashing the Power of Terraform and Azure DevOpsUnleashing the Power of Terraform and Azure DevOps
Unleashing the Power of Terraform and Azure DevOps
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Play framework : A Walkthrough
Play framework : A WalkthroughPlay framework : A Walkthrough
Play framework : A Walkthrough
 
Java 8 - Completable Future
Java 8 - Completable FutureJava 8 - Completable Future
Java 8 - Completable Future
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Linux Assignment 3
Linux Assignment 3Linux Assignment 3
Linux Assignment 3
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Performance analysis of synchronisation problem
Performance analysis of synchronisation problemPerformance analysis of synchronisation problem
Performance analysis of synchronisation problem
 
Spring 2
Spring 2Spring 2
Spring 2
 
Parallel Extentions to the .NET Framework
Parallel Extentions to the .NET FrameworkParallel Extentions to the .NET Framework
Parallel Extentions to the .NET Framework
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Demystifying functional effect systems in Scala