SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Developing a principled
approach to I/O in Scala
cats-effect
Target audience: You are likely a Haskell programmer whose got an interest in Scala and you wish to use it with a functional style library like Cats, Scalaz though this talk
focuses on the cats ecosystem.
What
• Provide a standard I/O for the Cats ecosystem

• Cats ecosystem is a Haskell-inspired Scala library ☺

• Abstractions work in JVM and JavaScript
Develop higher-order combinators as you normally would:

1/ develop a function : f

2/ develop a function : g

3/ compose them : f . g

4/ develop other HOFs

F . G should work in the JVM and JavaScript worlds - why would I re-write it again?
Why
• The project aims to standardise the way functional
programmers work with I/O in Scala

•Each team and/or individual was using
various approaches to develop their
solutions - not having a “common
language” means that more time is spent
on understanding.
The I/O abstraction leverages from Java with the usual try-catch-finally but they require embellishments to make them composable functions. What if there was a way to
do this in a functional style? We are functional programmers and we refuse to accept that we cannot change the situation. So let’s see what we can do…
A first IO typeclass
“run” would return
would return nothing
i.e. Unit
We started with crafting the IO typeclass (note that its not a monad yet !) and we seem to have develop something but its not quite useful yet. Why? Because we wanted
to compose , combine IO actions into something bigger.
An improved IO typeclass (1)
“++” would run
itself followed by
the other IO-action.
“empty” creates a
run that does
nothing.
This improved IO typeclass is much better and I can do much more things with it
An improved IO typeclass (2)
“++” allows us to
chain IO actions.
Here’s how we can possibly leverage our new IO typeclass to run 2 (potentially more) IO actions together. But Its not too useful because we cannot define what’s the
input to the IO action; we can solve this but we need more work !
IO Monad (1/3)
-“unit” aka pure
-“flatMap” aka >>=
-“map” aka “map” or “<$>"
Over here, I’ve pre-developed the Functor and Monad - its bare minimum but suffice for this example. As you are trying to understand what’s going on - don’t fret over
the syntax of this example and instead try to focus on the bigger idea here which is our IO monad can now return a meaningful value which is something our previous IO
implementation could not do.
IO Monad (2/3)
The process of encoding an
IO Monad using our
previous definitions.
Let’s see it in action next and beware that our IO Monad can now return a meaningful value and you can see this when comparing this version and the previous.
IO Monad (3/3)
Now that IO Monad returns a meaningful value, we can now proceed to do something more useful with it. Let’s dissect a little. You would notice that the type signature of
“demo” is “IO[Unit]” which returns nothing and the reason is because I suppressed the value to be returned in the definition of “demo” via the “yield ( )” expression.
Quantum leap to the future :
cats-effect !
That was a long story to get to this part of the talk where we start talking about cats-effect which is IO monad designed for the cats ecosystem. The cats-effect library
leverages the cats library which is type-class based approach to functional programming inspired none other than Haskell.
Cats-effect
Monadic comprehension in
action. In effect, its the
same as “ioa >> ioa”.
An example using cats-effect https://github.com/typelevel/cats-effect and this example serves to highlight the benefits of leveraging the efforts presented by this library.
The main takeaway from the presentation till now is the realisation that cats-effect (via Scala) gives Scala programmers a way to standardise the approach of performing
IO i.e. principled approach to IO in Scala. ☺
Concurrency
How IS concurrency linked to the IO monad in cats-effect? Well, the reason is that the library has a abstractions that allows the programmer to develop your own
concurrent abstractions; they come in a few forms: MVar, Ref, Semaphore & Deferred. Over here, I’m going to use an Haskell example found in this paper “Concurrent
Haskell” by SPJ et al which I will develop a construct concurrency example known as a Channel. Since we are all students of Haskell, you would immediately see the
resemblance to [Control.Concurrent.Chan] package.
Concurrency
• Ideas => Cross pollination 【异花传粉】

• From papers, journals, conference slides,
talks etc

• Self learning, validation of ideas

• You gotta be something of a polyglot
But that paper’s code is written in Haskell ? How are we (haskell students) suppose to translate it successfully? That’s the 

It’s an incredible skill to be able to translate ideas from different sources, internalise them and cross-pollinate them. Ideas are often more easily understood if you can see
it in action (e.g. papers in Haskell) and when you internalise them (you understand why it works, how it works) and finally you might want to try them out in another
environment (e.g. Scala) and it would be best if you didn’t have to re-invent everything ;) 

It helps when something like cats-effect allows you to just “translate” from Haskell idea/source Scala source
A buffer variable
As in the paper entitled “Concurrent Haskell”, this is the code presented in that paper. The encoding here is straightforward. Might be a good idea about now to read up
or refresh your knowledge about [Control.Concurrent.MVar]. Here’s how we can translate it to Scala via cats-effect.
Concurrency - Example
Source of image: https://typelevel.org/cats-effect/concurrency/mvar.html
This slide is from the project’s website and my purpose was to see if I could translate the code from one of my favourite Haskell papers of all time (aka Concurrent
Haskell) using only MVars and this codebase is housed in [Control.Concurrent.MVar].
Channel Variable via MVar
An MVar can very nearly be used to mediate a producer/consumer connection: the producer puts items into the MVar and the consumer takes them out. The fly in the
ointment is, ofcourse, that there is nothing to stop the producer overrunning, and writing a second value before the consumer has removed the first.

This problem is easily solved, by using a second MVar to handle acknowledgements from the consumer to the producer. We call the resulting abstraction a CVar (short for
channel variable).
Channel Variable - How to use
There are a number of concurrent abstractions highlighted in that paper by SPJ et al and I’ve implemented them all w/o too much trouble (e.g. Quantity Semaphore, Skip
Channel (though the source is readily available in Haskell doc which is wonderous).

Final thing to do is to execute it: CVarDemo.run(null) and you should see a message like

Result of writing and reading a buffer-variable is : (0, 1, 2, 3).
Questions
• I’d be happy if you connect with me on Twitter and
LinkedIn, see below:

• @RaymondTayBL

• https://www.linkedin.com/in/raymondtayboonleong/

• I did two books on developing HPC solutions using
OpenCL (GPGPU) and programming Actors using Akka.
Books

Contenu connexe

Tendances

Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript FundamentalsSrdjan Strbanovic
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building BlocksCate Huston
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Elixir – Peeking into Elixir's Processes, OTP and Supervisors
Elixir – Peeking into Elixir's Processes, OTP and SupervisorsElixir – Peeking into Elixir's Processes, OTP and Supervisors
Elixir – Peeking into Elixir's Processes, OTP and SupervisorsBenjamin Tan
 
Being Dangerous with Twig
Being Dangerous with TwigBeing Dangerous with Twig
Being Dangerous with TwigRyan Weaver
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureManish Jangir
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep DiveManish Jangir
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About JavascriptManish Jangir
 

Tendances (20)

Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Use the @types, Luke
Use the @types, LukeUse the @types, Luke
Use the @types, Luke
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
Elixir – Peeking into Elixir's Processes, OTP and Supervisors
Elixir – Peeking into Elixir's Processes, OTP and SupervisorsElixir – Peeking into Elixir's Processes, OTP and Supervisors
Elixir – Peeking into Elixir's Processes, OTP and Supervisors
 
Being Dangerous with Twig
Being Dangerous with TwigBeing Dangerous with Twig
Being Dangerous with Twig
 
Java 8 concurrency abstractions
Java 8 concurrency abstractionsJava 8 concurrency abstractions
Java 8 concurrency abstractions
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About Javascript
 

Similaire à Principled io in_scala_2019_distribution

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 programmingsamthemonad
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaScala Italy
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.brandongulla
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationMartin Odersky
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to ClojureRenzo Borgatti
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural PatternsSameh Deabes
 
Concurrent processing and distributed computing
Concurrent processing and distributed computingConcurrent processing and distributed computing
Concurrent processing and distributed computingRahul Ramteke
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slidesMartin Odersky
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 

Similaire à Principled io in_scala_2019_distribution (20)

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
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of Scala
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 
scala-intro
scala-introscala-intro
scala-intro
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentation
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Concurrent processing and distributed computing
Concurrent processing and distributed computingConcurrent processing and distributed computing
Concurrent processing and distributed computing
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Flatmap
FlatmapFlatmap
Flatmap
 
Devoxx
DevoxxDevoxx
Devoxx
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Graphql
GraphqlGraphql
Graphql
 

Plus de Raymond Tay

Building a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beamBuilding a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beamRaymond Tay
 
Toying with spark
Toying with sparkToying with spark
Toying with sparkRaymond Tay
 
Distributed computing for new bloods
Distributed computing for new bloodsDistributed computing for new bloods
Distributed computing for new bloodsRaymond Tay
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scalaRaymond Tay
 
Introduction to cuda geek camp singapore 2011
Introduction to cuda   geek camp singapore 2011Introduction to cuda   geek camp singapore 2011
Introduction to cuda geek camp singapore 2011Raymond Tay
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to ErlangRaymond Tay
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDARaymond Tay
 

Plus de Raymond Tay (8)

Building a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beamBuilding a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beam
 
Practical cats
Practical catsPractical cats
Practical cats
 
Toying with spark
Toying with sparkToying with spark
Toying with spark
 
Distributed computing for new bloods
Distributed computing for new bloodsDistributed computing for new bloods
Distributed computing for new bloods
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
Introduction to cuda geek camp singapore 2011
Introduction to cuda   geek camp singapore 2011Introduction to cuda   geek camp singapore 2011
Introduction to cuda geek camp singapore 2011
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
 

Dernier

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 

Dernier (20)

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 

Principled io in_scala_2019_distribution

  • 1. Developing a principled approach to I/O in Scala cats-effect Target audience: You are likely a Haskell programmer whose got an interest in Scala and you wish to use it with a functional style library like Cats, Scalaz though this talk focuses on the cats ecosystem.
  • 2. What • Provide a standard I/O for the Cats ecosystem • Cats ecosystem is a Haskell-inspired Scala library ☺ • Abstractions work in JVM and JavaScript Develop higher-order combinators as you normally would: 1/ develop a function : f 2/ develop a function : g 3/ compose them : f . g 4/ develop other HOFs F . G should work in the JVM and JavaScript worlds - why would I re-write it again?
  • 3. Why • The project aims to standardise the way functional programmers work with I/O in Scala •Each team and/or individual was using various approaches to develop their solutions - not having a “common language” means that more time is spent on understanding. The I/O abstraction leverages from Java with the usual try-catch-finally but they require embellishments to make them composable functions. What if there was a way to do this in a functional style? We are functional programmers and we refuse to accept that we cannot change the situation. So let’s see what we can do…
  • 4. A first IO typeclass “run” would return would return nothing i.e. Unit We started with crafting the IO typeclass (note that its not a monad yet !) and we seem to have develop something but its not quite useful yet. Why? Because we wanted to compose , combine IO actions into something bigger.
  • 5. An improved IO typeclass (1) “++” would run itself followed by the other IO-action. “empty” creates a run that does nothing. This improved IO typeclass is much better and I can do much more things with it
  • 6. An improved IO typeclass (2) “++” allows us to chain IO actions. Here’s how we can possibly leverage our new IO typeclass to run 2 (potentially more) IO actions together. But Its not too useful because we cannot define what’s the input to the IO action; we can solve this but we need more work !
  • 7. IO Monad (1/3) -“unit” aka pure -“flatMap” aka >>= -“map” aka “map” or “<$>" Over here, I’ve pre-developed the Functor and Monad - its bare minimum but suffice for this example. As you are trying to understand what’s going on - don’t fret over the syntax of this example and instead try to focus on the bigger idea here which is our IO monad can now return a meaningful value which is something our previous IO implementation could not do.
  • 8. IO Monad (2/3) The process of encoding an IO Monad using our previous definitions. Let’s see it in action next and beware that our IO Monad can now return a meaningful value and you can see this when comparing this version and the previous.
  • 9. IO Monad (3/3) Now that IO Monad returns a meaningful value, we can now proceed to do something more useful with it. Let’s dissect a little. You would notice that the type signature of “demo” is “IO[Unit]” which returns nothing and the reason is because I suppressed the value to be returned in the definition of “demo” via the “yield ( )” expression.
  • 10. Quantum leap to the future : cats-effect ! That was a long story to get to this part of the talk where we start talking about cats-effect which is IO monad designed for the cats ecosystem. The cats-effect library leverages the cats library which is type-class based approach to functional programming inspired none other than Haskell.
  • 11. Cats-effect Monadic comprehension in action. In effect, its the same as “ioa >> ioa”. An example using cats-effect https://github.com/typelevel/cats-effect and this example serves to highlight the benefits of leveraging the efforts presented by this library. The main takeaway from the presentation till now is the realisation that cats-effect (via Scala) gives Scala programmers a way to standardise the approach of performing IO i.e. principled approach to IO in Scala. ☺
  • 12. Concurrency How IS concurrency linked to the IO monad in cats-effect? Well, the reason is that the library has a abstractions that allows the programmer to develop your own concurrent abstractions; they come in a few forms: MVar, Ref, Semaphore & Deferred. Over here, I’m going to use an Haskell example found in this paper “Concurrent Haskell” by SPJ et al which I will develop a construct concurrency example known as a Channel. Since we are all students of Haskell, you would immediately see the resemblance to [Control.Concurrent.Chan] package.
  • 13. Concurrency • Ideas => Cross pollination 【异花传粉】 • From papers, journals, conference slides, talks etc • Self learning, validation of ideas • You gotta be something of a polyglot But that paper’s code is written in Haskell ? How are we (haskell students) suppose to translate it successfully? That’s the It’s an incredible skill to be able to translate ideas from different sources, internalise them and cross-pollinate them. Ideas are often more easily understood if you can see it in action (e.g. papers in Haskell) and when you internalise them (you understand why it works, how it works) and finally you might want to try them out in another environment (e.g. Scala) and it would be best if you didn’t have to re-invent everything ;) It helps when something like cats-effect allows you to just “translate” from Haskell idea/source Scala source
  • 14. A buffer variable As in the paper entitled “Concurrent Haskell”, this is the code presented in that paper. The encoding here is straightforward. Might be a good idea about now to read up or refresh your knowledge about [Control.Concurrent.MVar]. Here’s how we can translate it to Scala via cats-effect.
  • 15. Concurrency - Example Source of image: https://typelevel.org/cats-effect/concurrency/mvar.html This slide is from the project’s website and my purpose was to see if I could translate the code from one of my favourite Haskell papers of all time (aka Concurrent Haskell) using only MVars and this codebase is housed in [Control.Concurrent.MVar].
  • 16. Channel Variable via MVar An MVar can very nearly be used to mediate a producer/consumer connection: the producer puts items into the MVar and the consumer takes them out. The fly in the ointment is, ofcourse, that there is nothing to stop the producer overrunning, and writing a second value before the consumer has removed the first. This problem is easily solved, by using a second MVar to handle acknowledgements from the consumer to the producer. We call the resulting abstraction a CVar (short for channel variable).
  • 17. Channel Variable - How to use There are a number of concurrent abstractions highlighted in that paper by SPJ et al and I’ve implemented them all w/o too much trouble (e.g. Quantity Semaphore, Skip Channel (though the source is readily available in Haskell doc which is wonderous). Final thing to do is to execute it: CVarDemo.run(null) and you should see a message like Result of writing and reading a buffer-variable is : (0, 1, 2, 3).
  • 18. Questions • I’d be happy if you connect with me on Twitter and LinkedIn, see below: • @RaymondTayBL • https://www.linkedin.com/in/raymondtayboonleong/ • I did two books on developing HPC solutions using OpenCL (GPGPU) and programming Actors using Akka.
  • 19. Books