SlideShare a Scribd company logo
Oleksiy Dyagilev
• lead software engineer in epam
• working on scalable computing and data grids (GigaSpaces, Storm, Spark)
• blog http://dyagilev.org
• Abstract Algebra (1900s?) and Category Theory (1940s)
• Mathematicians study abstract structures and relationships between them
• Abstract Algebra (1900s?) and Category Theory (1940s)
• Mathematicians study abstract structures and relationships between them
• Early of 1990s, Eugenio Moggi described the general use of monad to
structure programs
• Abstract Algebra (1900s?) and Category Theory (1940s)
• Mathematicians study abstract structures and relationships between them
• Early of 1990s, Eugenio Moggi described the general use of monad to
structure programs
• Early of 1990s, monad appeared in Haskell, a purely functional language.
As well as other concepts such as Functor, Monoid, Arrow, etc
• Abstract Algebra (1900s?) and Category Theory (1940s)
• Mathematicians study abstract structures and relationships between them
• Early of 1990s, Eugenio Moggi described the general use of monad to
structure programs
• Early of 1990s, monad appeared in Haskell, a purely functional language.
As well as other concepts such as Functor, Monoid, Arrow, etc
• 2003, Martin Odersky creates Scala, a languages that unifies object-
oriented and functional paradigms. Influenced by Haskell, Java, Erlang, etc.
• Abstract Algebra (1900s?) and Category Theory (1940s)
• Mathematicians study abstract structures and relationships between them
• Early of 1990s, Eugenio Moggi described the general use of monad to
structure programs
• Early of 1990s, monad appeared in Haskell, a purely functional language.
As well as other concepts such as Functor, Monoid, Arrow, etc
• 2003, Martin Odersky creates Scala, a languages that unifies object-
oriented and functional paradigms. Influenced by Haskell, Java, Erlang, etc.
• 2014, Java 8 released. Functional programming support – lambda, streams
• How abstractions from Math (Category Theory, Abstract Algebra) help in functional programming & Big Data
• How to leverage them and become a better programmer
User user = findUser(userId);
if (user != null) {
Address address = user.getAddress();
if (address != null) {
String zipCode = address.getZipCode();
if (zipCode != null) {
City city = findCityByZipCode(zipCode);
if (city != null) {
return city.getName();
}
}
}
}
return null;
Example #1
Optional<String> cityName = findUser(userId)
.flatMap(user -> user.getAddress())
.flatMap(address -> address.getZipCode())
.flatMap(zipCode -> findCityByZipCode(zipCode))
.map(city -> city.getName());
which
may not return a result.
Refactored with Optional
Stream<Employee> employees = companies.stream()
.flatMap(company -> company.departments())
.flatMap(department -> department.employees());
Example #2
which can return several values.
• container with a type M<T> (e.g. Optional<T>)
• method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>))
• constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x))
• container with a type M<T> (e.g. Optional<T>)
• method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>))
• constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x))
M<U> map(f) { return flatMap(x -> unit(f(x))) }
Bonus: now we can define M<U> map(T -> U)
• container with a type M<T> (e.g. Optional<T>)
• method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>))
• constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x))
1. Left identity: unit(x).flatMap(f) = f(x)
2. Right identity: m.flatMap(x -> unit(x)) = m
3. Associativity: m.flatMap(f).flatMap(g) = m.flatMap(x -> f(x).flatMap(g)))
M<U> map(f) { return flatMap(x -> unit(f(x))) }
Bonus: now we can define M<U> map(T -> U)
Optional<User> user = findUser(userId);
Optional<Order> order = findOrder(orderId);
Optional<Payment> payment = findPayment(orderId);
Optional<Placement> placement = user
.flatMap(u ->
(order.flatMap(o ->
(payment.map(p -> submitOrder(u, o, p))))));
Java: looks ugly 
Optional<User> user = findUser(userId);
Optional<Order> order = findOrder(orderId);
Optional<Payment> payment = findPayment(orderId);
Optional<Placement> placement = user
.flatMap(u ->
(order.flatMap(o ->
(payment.map(p -> submitOrder(u, o, p))))));
Java: looks ugly 
• Scala, for-comprehension
• Haskell, do-notation
• F#, computational expressions
Optional<User> user = findUser(userId);
Optional<Order> order = findOrder(orderId);
Optional<Payment> payment = findPayment(orderId);
Optional<Placement> placement = user
.flatMap(u ->
(order.flatMap(o ->
(payment.map(p -> submitOrder(u, o, p))))));
Java: looks ugly 
val placement =
for {
u <- findUser(userId)
o <- findOrder(orderId)
p <- findPayment(orderId)
} yield submitOrder(u, o, p)
Scala: built-in monad Support 
• Scala, for-comprehension
• Haskell, do-notation
• F#, computational expressions
trait Parser[T] extends (String => ParseResult[T])
sealed abstract class ParseResult[T]
case class Success[T](result: T, rest: String) extends ParseResult[T]
case class Failure() extends ParseResult[Nothing]
val letter: Parser[Char] = …
val digit: Parser[Char] = …
val space: Parser[Char] = …
def map[U](f: T => U): Parser[U] = parser { in => this(in) map f }
def flatMap[U](f: T => Parser[U]): Parser[U] = parser { in => this(in) withNext f }
def * : Parser[List[T]] = …
trait Parser[T] extends (String => ParseResult[T])
sealed abstract class ParseResult[T]
case class Success[T](result: T, rest: String) extends ParseResult[T]
case class Failure() extends ParseResult[Nothing]
val letter: Parser[Char] = …
val digit: Parser[Char] = …
val space: Parser[Char] = …
def map[U](f: T => U): Parser[U] = parser { in => this(in) map f }
def flatMap[U](f: T => Parser[U]): Parser[U] = parser { in => this(in) withNext f }
def * : Parser[List[T]] = …
val userParser = for {
firstName <- letter.*
_ <- space
lastName <- letter.*
_ <- space
phone <- digit.*} yield User(firstName, lastName, phone)
“John Doe 0671112222”
scala.Option java.Optional Absence of value
scala.List java.Stream Multiple results
scala.Future scalaz.Task java.CompletableFuture Asynchronous computations
scalaz.Reader Read from shared environment
scalaz.Writer Collect data in addition to computed values
scalaz.State Maintain state
scala.Try scalaz./ Handling failures
• Remove boilerplate
• Modularity: separate computations from combination strategy
• Composability: compose computations from simple ones
• Improve maintainability
• Better readability
• Vocabulary
New data
All data Batch view
Real-time view
Data
stream
Batch processing
Real-time processing
Serving layer
Query
and merge
• Write job logic once and run on many Platforms(Hadoop, Storm)
• Library authors talk about monoids all the time 
• Write job logic once and run on many Platforms(Hadoop, Storm)
• Library authors talk about monoids all the time 
def wordCount[P <: Platform[P]]
(source: Producer[P, String], store: P#Store[String, Long]) =
source.flatMap { sentence =>
toWords(sentence).map(_ -> 1L)
}.sumByKey(store)
• Write job logic once and run on many Platforms(Hadoop, Storm)
• Library authors talk about monoids all the time 
def wordCount[P <: Platform[P]]
(source: Producer[P, String], store: P#Store[String, Long]) =
source.flatMap { sentence =>
toWords(sentence).map(_ -> 1L)
}.sumByKey(store)
def sumByKey(store: P#Store[K, V])(implicit semigroup: Semigroup[V]): Summer[P, K, V] = …
Given a set S and a binary operation +, we say that (𝑠, +) is a Semigroup if ∀ 𝑥, 𝑦, 𝑧 ∈ 𝑆:
• Closure: 𝑥 + 𝑦 ∈ 𝑆
• Associativity: (𝑥 + 𝑦) + 𝑧 = 𝑥 + (𝑦 + 𝑧)
Monoid is a semigroup with identity element:
• Identity: ∃ 𝑒 ∈ 𝑆: 𝑒 + 𝑥 = 𝑥 + 𝑒 = 𝑥
• 3 * 2 (numbers under multiplication, 1 is the identity element)
• 1 + 5 (numbers under addition, 0 is the identity element)
• “ab” + “cd” (strings under concatenation, empty string is the identity element)
• many more
Input
data
map
map
map
map
reduce
reduce
reduce
output
Having a sequence of elements of monoid M,
we can reduce them into a final value
Associativity ensure that we can parallelize computation(not exactly true)
Identity allows to skip elements that don’t affect the result
Associativity: (𝑥 + 𝑦) + 𝑧 = 𝑥 + (𝑦 + 𝑧)
General Associativity Theorem
https://proofwiki.org/wiki/General_Associativity_Theorem
given:
𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒 + 𝑓 + 𝑔 + ℎ
you can place parentheses anywhere
((𝑎 + 𝑏) + (𝑐 + 𝑑)) + ( 𝑒 + 𝑓 + 𝑔 + ℎ )
or
(𝑎 + 𝑏 + 𝑐 + 𝑑) + (𝑒 + 𝑓 + 𝑔 + ℎ)
𝑎 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 ℎ
+ + + +
+
+
+
𝑎 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 ℎ
+ + + +
+
+
+
a b c d e f g h
a + b + c + d + e + fBatch processing
Real-time processing
𝐵0 𝐵1 𝐵2 𝐵3 𝐵4 𝐵5 𝐵6 𝐵7
time1h now
Real-time sums from 0,
each batch
Batch proc. recomputes
total sum
a b c d e f g h
a + b + c + d + e + fBatch processing
Real-time processing
𝐵0 𝐵1 𝐵2 𝐵3 𝐵4 𝐵5 𝐵6 𝐵7
time1h now
Query
and sum
real-time + batch
(𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒 + 𝑓) + 𝑔 + ℎ
(this is where Semigroup required)
Bloom filter is a space-efficient probabilistic data structure to test presence of an element in a set
0 0 0 0 0 0 0 0 0 0 0 0
𝑚
Operations:
• Insert element
• Query if element is present. The answer is either No or Maybe (false positives are possible)
Consists of:
• 𝑘 hash functions: ℎ1, ℎ2, … ℎ 𝑘
• bit array of 𝑚 bits
0 0 1 0 0 0 0 1 0 1 0 0
ℎ1(𝑒) ℎ2(𝑒) … ℎ 𝑘(𝑒)
𝑒
set bit value to 1
0 0 1 0 1 0 1 1 0 0 0 0
ℎ1(𝑒) ℎ2(𝑒) … ℎ 𝑘(𝑒)
𝑒
check if all bits are set to 1
0 0 1 0 1 0 0 1 0 0 0 0Filter A: {𝑒1, 𝑒2, 𝑒3}
1 0 1 0 0 0 0 0 1 0 0 0Filter B: {𝑒4, 𝑒5, 𝑒6}
+ OR
1 0 1 0 1 0 0 1 1 0 0 0Filter A + B: {𝑒1, 𝑒2, 𝑒3, 𝑒4, 𝑒5, 𝑒6}
A few can be found in in Algebird (Abstract Algebra for Scala) https://github.com/twitter/algebird/
• Bloom Filter
• HyperLogLog
• CountMinSketch
• TopK
• etc
• Monad is just a useful pattern in functional programming
• You don’t need to understand Category Theory to use Monads
• Once you grasp the idea, you will see this pattern everywhere
• Semigroup (commutative) and monoid define properties useful in distributed computing and Lambda Architecture.
• It’s all about associativity and commutativity. No nonsense!
Monads and Monoids by Oleksiy Dyagilev

More Related Content

What's hot

Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Philip Schwarz
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
Luca Molteni
 
03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quiz
Sebastian Rettig
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
O T
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
Muthu Vinayagam
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
gekiaruj
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
Gil Cohen
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
Intro C# Book
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
sxw2k
 
Collection and framework
Collection and frameworkCollection and framework
Collection and framework
SARAVANAN GOPALAKRISHNAN
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
Knoldus Inc.
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
Intro C# Book
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
Intro C# Book
 

What's hot (16)

Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quiz
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Collection and framework
Collection and frameworkCollection and framework
Collection and framework
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 

Similar to Monads and Monoids by Oleksiy Dyagilev

Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
Naveenkumar Muguda
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
Debasish Ghosh
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
Naveenkumar Muguda
 
Practical cats
Practical catsPractical cats
Practical cats
Raymond Tay
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
Hang Zhao
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
Meetu Maltiar
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
Krish_ver2
 
R language introduction
R language introductionR language introduction
R language introduction
Shashwat Shriparv
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
Knoldus Inc.
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
Albert Bifet
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
kenbot
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
Philip Schwarz
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
Movel
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
Alok Mohapatra
 

Similar to Monads and Monoids by Oleksiy Dyagilev (20)

Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
 
Practical cats
Practical catsPractical cats
Practical cats
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
R language introduction
R language introductionR language introduction
R language introduction
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 

More from JavaDayUA

STEMing Kids: One workshop at a time
STEMing Kids: One workshop at a timeSTEMing Kids: One workshop at a time
STEMing Kids: One workshop at a time
JavaDayUA
 
Flavors of Concurrency in Java
Flavors of Concurrency in JavaFlavors of Concurrency in Java
Flavors of Concurrency in Java
JavaDayUA
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9
JavaDayUA
 
Continuously building, releasing and deploying software: The Revenge of the M...
Continuously building, releasing and deploying software: The Revenge of the M...Continuously building, releasing and deploying software: The Revenge of the M...
Continuously building, releasing and deploying software: The Revenge of the M...
JavaDayUA
 
The Epic Groovy Puzzlers S02: The Revenge of the Parentheses
The Epic Groovy Puzzlers S02: The Revenge of the ParenthesesThe Epic Groovy Puzzlers S02: The Revenge of the Parentheses
The Epic Groovy Puzzlers S02: The Revenge of the Parentheses
JavaDayUA
 
20 Years of Java
20 Years of Java20 Years of Java
20 Years of Java
JavaDayUA
 
How to get the most out of code reviews
How to get the most out of code reviewsHow to get the most out of code reviews
How to get the most out of code reviews
JavaDayUA
 
Unlocking the Magic of Monads with Java 8
Unlocking the Magic of Monads with Java 8Unlocking the Magic of Monads with Java 8
Unlocking the Magic of Monads with Java 8
JavaDayUA
 
Virtual Private Cloud with container technologies for DevOps
Virtual Private Cloud with container technologies for DevOpsVirtual Private Cloud with container technologies for DevOps
Virtual Private Cloud with container technologies for DevOps
JavaDayUA
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java Platform
JavaDayUA
 
Interactive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and ArchitectureInteractive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and Architecture
JavaDayUA
 
MapDB - taking Java collections to the next level
MapDB - taking Java collections to the next levelMapDB - taking Java collections to the next level
MapDB - taking Java collections to the next level
JavaDayUA
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
JavaDayUA
 
Design rationales in the JRockit JVM
Design rationales in the JRockit JVMDesign rationales in the JRockit JVM
Design rationales in the JRockit JVM
JavaDayUA
 
Next-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
Next-gen DevOps engineering with Docker and Kubernetes by Antons KrangaNext-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
Next-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
JavaDayUA
 
Apache Cassandra. Inception - all you need to know by Mikhail Dubkov
Apache Cassandra. Inception - all you need to know by Mikhail DubkovApache Cassandra. Inception - all you need to know by Mikhail Dubkov
Apache Cassandra. Inception - all you need to know by Mikhail Dubkov
JavaDayUA
 
Solution Architecture tips & tricks by Roman Shramkov
Solution Architecture tips & tricks by Roman ShramkovSolution Architecture tips & tricks by Roman Shramkov
Solution Architecture tips & tricks by Roman Shramkov
JavaDayUA
 
Testing in Legacy: from Rags to Riches by Taras Slipets
Testing in Legacy: from Rags to Riches by Taras SlipetsTesting in Legacy: from Rags to Riches by Taras Slipets
Testing in Legacy: from Rags to Riches by Taras Slipets
JavaDayUA
 
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max MyslyvtsevReactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
JavaDayUA
 
Spark-driven audience counting by Boris Trofimov
Spark-driven audience counting by Boris TrofimovSpark-driven audience counting by Boris Trofimov
Spark-driven audience counting by Boris Trofimov
JavaDayUA
 

More from JavaDayUA (20)

STEMing Kids: One workshop at a time
STEMing Kids: One workshop at a timeSTEMing Kids: One workshop at a time
STEMing Kids: One workshop at a time
 
Flavors of Concurrency in Java
Flavors of Concurrency in JavaFlavors of Concurrency in Java
Flavors of Concurrency in Java
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9
 
Continuously building, releasing and deploying software: The Revenge of the M...
Continuously building, releasing and deploying software: The Revenge of the M...Continuously building, releasing and deploying software: The Revenge of the M...
Continuously building, releasing and deploying software: The Revenge of the M...
 
The Epic Groovy Puzzlers S02: The Revenge of the Parentheses
The Epic Groovy Puzzlers S02: The Revenge of the ParenthesesThe Epic Groovy Puzzlers S02: The Revenge of the Parentheses
The Epic Groovy Puzzlers S02: The Revenge of the Parentheses
 
20 Years of Java
20 Years of Java20 Years of Java
20 Years of Java
 
How to get the most out of code reviews
How to get the most out of code reviewsHow to get the most out of code reviews
How to get the most out of code reviews
 
Unlocking the Magic of Monads with Java 8
Unlocking the Magic of Monads with Java 8Unlocking the Magic of Monads with Java 8
Unlocking the Magic of Monads with Java 8
 
Virtual Private Cloud with container technologies for DevOps
Virtual Private Cloud with container technologies for DevOpsVirtual Private Cloud with container technologies for DevOps
Virtual Private Cloud with container technologies for DevOps
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java Platform
 
Interactive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and ArchitectureInteractive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and Architecture
 
MapDB - taking Java collections to the next level
MapDB - taking Java collections to the next levelMapDB - taking Java collections to the next level
MapDB - taking Java collections to the next level
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
 
Design rationales in the JRockit JVM
Design rationales in the JRockit JVMDesign rationales in the JRockit JVM
Design rationales in the JRockit JVM
 
Next-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
Next-gen DevOps engineering with Docker and Kubernetes by Antons KrangaNext-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
Next-gen DevOps engineering with Docker and Kubernetes by Antons Kranga
 
Apache Cassandra. Inception - all you need to know by Mikhail Dubkov
Apache Cassandra. Inception - all you need to know by Mikhail DubkovApache Cassandra. Inception - all you need to know by Mikhail Dubkov
Apache Cassandra. Inception - all you need to know by Mikhail Dubkov
 
Solution Architecture tips & tricks by Roman Shramkov
Solution Architecture tips & tricks by Roman ShramkovSolution Architecture tips & tricks by Roman Shramkov
Solution Architecture tips & tricks by Roman Shramkov
 
Testing in Legacy: from Rags to Riches by Taras Slipets
Testing in Legacy: from Rags to Riches by Taras SlipetsTesting in Legacy: from Rags to Riches by Taras Slipets
Testing in Legacy: from Rags to Riches by Taras Slipets
 
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max MyslyvtsevReactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
 
Spark-driven audience counting by Boris Trofimov
Spark-driven audience counting by Boris TrofimovSpark-driven audience counting by Boris Trofimov
Spark-driven audience counting by Boris Trofimov
 

Recently uploaded

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 

Recently uploaded (20)

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 

Monads and Monoids by Oleksiy Dyagilev

  • 2. • lead software engineer in epam • working on scalable computing and data grids (GigaSpaces, Storm, Spark) • blog http://dyagilev.org
  • 3. • Abstract Algebra (1900s?) and Category Theory (1940s) • Mathematicians study abstract structures and relationships between them
  • 4. • Abstract Algebra (1900s?) and Category Theory (1940s) • Mathematicians study abstract structures and relationships between them • Early of 1990s, Eugenio Moggi described the general use of monad to structure programs
  • 5. • Abstract Algebra (1900s?) and Category Theory (1940s) • Mathematicians study abstract structures and relationships between them • Early of 1990s, Eugenio Moggi described the general use of monad to structure programs • Early of 1990s, monad appeared in Haskell, a purely functional language. As well as other concepts such as Functor, Monoid, Arrow, etc
  • 6. • Abstract Algebra (1900s?) and Category Theory (1940s) • Mathematicians study abstract structures and relationships between them • Early of 1990s, Eugenio Moggi described the general use of monad to structure programs • Early of 1990s, monad appeared in Haskell, a purely functional language. As well as other concepts such as Functor, Monoid, Arrow, etc • 2003, Martin Odersky creates Scala, a languages that unifies object- oriented and functional paradigms. Influenced by Haskell, Java, Erlang, etc.
  • 7. • Abstract Algebra (1900s?) and Category Theory (1940s) • Mathematicians study abstract structures and relationships between them • Early of 1990s, Eugenio Moggi described the general use of monad to structure programs • Early of 1990s, monad appeared in Haskell, a purely functional language. As well as other concepts such as Functor, Monoid, Arrow, etc • 2003, Martin Odersky creates Scala, a languages that unifies object- oriented and functional paradigms. Influenced by Haskell, Java, Erlang, etc. • 2014, Java 8 released. Functional programming support – lambda, streams
  • 8. • How abstractions from Math (Category Theory, Abstract Algebra) help in functional programming & Big Data • How to leverage them and become a better programmer
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. User user = findUser(userId); if (user != null) { Address address = user.getAddress(); if (address != null) { String zipCode = address.getZipCode(); if (zipCode != null) { City city = findCityByZipCode(zipCode); if (city != null) { return city.getName(); } } } } return null; Example #1
  • 18. Optional<String> cityName = findUser(userId) .flatMap(user -> user.getAddress()) .flatMap(address -> address.getZipCode()) .flatMap(zipCode -> findCityByZipCode(zipCode)) .map(city -> city.getName()); which may not return a result. Refactored with Optional
  • 19. Stream<Employee> employees = companies.stream() .flatMap(company -> company.departments()) .flatMap(department -> department.employees()); Example #2 which can return several values.
  • 20. • container with a type M<T> (e.g. Optional<T>) • method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>)) • constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x))
  • 21. • container with a type M<T> (e.g. Optional<T>) • method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>)) • constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x)) M<U> map(f) { return flatMap(x -> unit(f(x))) } Bonus: now we can define M<U> map(T -> U)
  • 22. • container with a type M<T> (e.g. Optional<T>) • method M<U> flatMap(T -> M<U>) (e.g. flatMap(T -> Optional<U>)) • constructor to put T into M<T>; same as a static method M<T> unit(T) (e.g. Optional.of(x)) 1. Left identity: unit(x).flatMap(f) = f(x) 2. Right identity: m.flatMap(x -> unit(x)) = m 3. Associativity: m.flatMap(f).flatMap(g) = m.flatMap(x -> f(x).flatMap(g))) M<U> map(f) { return flatMap(x -> unit(f(x))) } Bonus: now we can define M<U> map(T -> U)
  • 23. Optional<User> user = findUser(userId); Optional<Order> order = findOrder(orderId); Optional<Payment> payment = findPayment(orderId); Optional<Placement> placement = user .flatMap(u -> (order.flatMap(o -> (payment.map(p -> submitOrder(u, o, p)))))); Java: looks ugly 
  • 24. Optional<User> user = findUser(userId); Optional<Order> order = findOrder(orderId); Optional<Payment> payment = findPayment(orderId); Optional<Placement> placement = user .flatMap(u -> (order.flatMap(o -> (payment.map(p -> submitOrder(u, o, p)))))); Java: looks ugly  • Scala, for-comprehension • Haskell, do-notation • F#, computational expressions
  • 25. Optional<User> user = findUser(userId); Optional<Order> order = findOrder(orderId); Optional<Payment> payment = findPayment(orderId); Optional<Placement> placement = user .flatMap(u -> (order.flatMap(o -> (payment.map(p -> submitOrder(u, o, p)))))); Java: looks ugly  val placement = for { u <- findUser(userId) o <- findOrder(orderId) p <- findPayment(orderId) } yield submitOrder(u, o, p) Scala: built-in monad Support  • Scala, for-comprehension • Haskell, do-notation • F#, computational expressions
  • 26.
  • 27. trait Parser[T] extends (String => ParseResult[T]) sealed abstract class ParseResult[T] case class Success[T](result: T, rest: String) extends ParseResult[T] case class Failure() extends ParseResult[Nothing] val letter: Parser[Char] = … val digit: Parser[Char] = … val space: Parser[Char] = … def map[U](f: T => U): Parser[U] = parser { in => this(in) map f } def flatMap[U](f: T => Parser[U]): Parser[U] = parser { in => this(in) withNext f } def * : Parser[List[T]] = …
  • 28. trait Parser[T] extends (String => ParseResult[T]) sealed abstract class ParseResult[T] case class Success[T](result: T, rest: String) extends ParseResult[T] case class Failure() extends ParseResult[Nothing] val letter: Parser[Char] = … val digit: Parser[Char] = … val space: Parser[Char] = … def map[U](f: T => U): Parser[U] = parser { in => this(in) map f } def flatMap[U](f: T => Parser[U]): Parser[U] = parser { in => this(in) withNext f } def * : Parser[List[T]] = … val userParser = for { firstName <- letter.* _ <- space lastName <- letter.* _ <- space phone <- digit.*} yield User(firstName, lastName, phone) “John Doe 0671112222”
  • 29. scala.Option java.Optional Absence of value scala.List java.Stream Multiple results scala.Future scalaz.Task java.CompletableFuture Asynchronous computations scalaz.Reader Read from shared environment scalaz.Writer Collect data in addition to computed values scalaz.State Maintain state scala.Try scalaz./ Handling failures
  • 30. • Remove boilerplate • Modularity: separate computations from combination strategy • Composability: compose computations from simple ones • Improve maintainability • Better readability • Vocabulary
  • 31.
  • 32. New data All data Batch view Real-time view Data stream Batch processing Real-time processing Serving layer Query and merge
  • 33. • Write job logic once and run on many Platforms(Hadoop, Storm) • Library authors talk about monoids all the time 
  • 34. • Write job logic once and run on many Platforms(Hadoop, Storm) • Library authors talk about monoids all the time  def wordCount[P <: Platform[P]] (source: Producer[P, String], store: P#Store[String, Long]) = source.flatMap { sentence => toWords(sentence).map(_ -> 1L) }.sumByKey(store)
  • 35. • Write job logic once and run on many Platforms(Hadoop, Storm) • Library authors talk about monoids all the time  def wordCount[P <: Platform[P]] (source: Producer[P, String], store: P#Store[String, Long]) = source.flatMap { sentence => toWords(sentence).map(_ -> 1L) }.sumByKey(store) def sumByKey(store: P#Store[K, V])(implicit semigroup: Semigroup[V]): Summer[P, K, V] = …
  • 36. Given a set S and a binary operation +, we say that (𝑠, +) is a Semigroup if ∀ 𝑥, 𝑦, 𝑧 ∈ 𝑆: • Closure: 𝑥 + 𝑦 ∈ 𝑆 • Associativity: (𝑥 + 𝑦) + 𝑧 = 𝑥 + (𝑦 + 𝑧) Monoid is a semigroup with identity element: • Identity: ∃ 𝑒 ∈ 𝑆: 𝑒 + 𝑥 = 𝑥 + 𝑒 = 𝑥 • 3 * 2 (numbers under multiplication, 1 is the identity element) • 1 + 5 (numbers under addition, 0 is the identity element) • “ab” + “cd” (strings under concatenation, empty string is the identity element) • many more
  • 37. Input data map map map map reduce reduce reduce output Having a sequence of elements of monoid M, we can reduce them into a final value Associativity ensure that we can parallelize computation(not exactly true) Identity allows to skip elements that don’t affect the result
  • 38. Associativity: (𝑥 + 𝑦) + 𝑧 = 𝑥 + (𝑦 + 𝑧) General Associativity Theorem https://proofwiki.org/wiki/General_Associativity_Theorem given: 𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒 + 𝑓 + 𝑔 + ℎ you can place parentheses anywhere ((𝑎 + 𝑏) + (𝑐 + 𝑑)) + ( 𝑒 + 𝑓 + 𝑔 + ℎ ) or (𝑎 + 𝑏 + 𝑐 + 𝑑) + (𝑒 + 𝑓 + 𝑔 + ℎ)
  • 39. 𝑎 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 ℎ + + + + + + +
  • 40. 𝑎 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 ℎ + + + + + + +
  • 41. a b c d e f g h a + b + c + d + e + fBatch processing Real-time processing 𝐵0 𝐵1 𝐵2 𝐵3 𝐵4 𝐵5 𝐵6 𝐵7 time1h now Real-time sums from 0, each batch Batch proc. recomputes total sum
  • 42. a b c d e f g h a + b + c + d + e + fBatch processing Real-time processing 𝐵0 𝐵1 𝐵2 𝐵3 𝐵4 𝐵5 𝐵6 𝐵7 time1h now Query and sum real-time + batch (𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒 + 𝑓) + 𝑔 + ℎ (this is where Semigroup required)
  • 43.
  • 44. Bloom filter is a space-efficient probabilistic data structure to test presence of an element in a set 0 0 0 0 0 0 0 0 0 0 0 0 𝑚 Operations: • Insert element • Query if element is present. The answer is either No or Maybe (false positives are possible) Consists of: • 𝑘 hash functions: ℎ1, ℎ2, … ℎ 𝑘 • bit array of 𝑚 bits
  • 45. 0 0 1 0 0 0 0 1 0 1 0 0 ℎ1(𝑒) ℎ2(𝑒) … ℎ 𝑘(𝑒) 𝑒 set bit value to 1
  • 46. 0 0 1 0 1 0 1 1 0 0 0 0 ℎ1(𝑒) ℎ2(𝑒) … ℎ 𝑘(𝑒) 𝑒 check if all bits are set to 1
  • 47. 0 0 1 0 1 0 0 1 0 0 0 0Filter A: {𝑒1, 𝑒2, 𝑒3} 1 0 1 0 0 0 0 0 1 0 0 0Filter B: {𝑒4, 𝑒5, 𝑒6} + OR 1 0 1 0 1 0 0 1 1 0 0 0Filter A + B: {𝑒1, 𝑒2, 𝑒3, 𝑒4, 𝑒5, 𝑒6}
  • 48. A few can be found in in Algebird (Abstract Algebra for Scala) https://github.com/twitter/algebird/ • Bloom Filter • HyperLogLog • CountMinSketch • TopK • etc
  • 49. • Monad is just a useful pattern in functional programming • You don’t need to understand Category Theory to use Monads • Once you grasp the idea, you will see this pattern everywhere • Semigroup (commutative) and monoid define properties useful in distributed computing and Lambda Architecture. • It’s all about associativity and commutativity. No nonsense!