Faces Everywhere
Code
sbt console
Applications
FUNCTIONAL ALGEBRA BY EXAMPLE
Figure:
Credit http://www.flickr.com/photos/slambo_42
Susan Potter @SusanPotter CodeMesh December 2013 1
FACES EVERYWHERE
Faces Everywhere
Code
sbt console
Applications
OBVIOUS FACE
3
Faces Everywhere
Code
sbt console
Applications
LESS OBVIOUS FACE
4
Faces Everywhere
Code
sbt console
Applications
MANUFACTURED FACE
5
Faces Everywhere
Code
sbt console
Applications
SQUINT FACE
6
Faces Everywhere
Code
sbt console
Applications
MAYBE FACE
7
Faces Everywhere
Code
sbt console
Applications
OO PATTERNS VS FP ABSTRACTIONS
→
More (Subjective -> Objective)
→
More (Ambiguous -> Precise)
→
"Fluffy" Interfaces -> Generic Functions
8
Faces Everywhere
Code
sbt console
Applications
OO PATTERNS VS FP ABSTRACTIONS
→
More (Subjective -> Objective)
→
More (Ambiguous -> Precise)
→
"Fluffy" Interfaces -> Generic Functions
8
Faces Everywhere
Code
sbt console
Applications
OO PATTERNS VS FP ABSTRACTIONS
→
More (Subjective -> Objective)
→
More (Ambiguous -> Precise)
→
"Fluffy" Interfaces -> Generic Functions
8
Faces Everywhere
Code
sbt console
Applications
LAYING BRICKS
9
Faces Everywhere
Code
sbt console
Applications
WARNING
→
Abstract Algebra -> (Continuous, Infinite)
→
Real World -> usually (Discrete, Finite)
10
Faces Everywhere
Code
sbt console
Applications
WARNING
→
Abstract Algebra -> (Continuous, Infinite)
→
Real World -> usually (Discrete, Finite)
10
Faces Everywhere
Code
sbt console
Applications
11
CODE
Faces Everywhere
Code
sbt console
Applications
EXAMPLE UNIX PIPE
1 2 3
find . -name "*. rb" \ | xargs egrep "#.*? TODO:" \ | wc -l Character-based, through file descriptors
13
Faces Everywhere
Code
sbt console
Applications
EXAMPLE FUNCTION COMPOSITION
1
( length . mapToUpper . sanitize ) input Value based, through functions
14
Faces Everywhere
Code
sbt console
Applications
VALUING VALUES IN REAL WORLD 1 2
3
sealed trait PossiblyMaybe [+A] final case class Somefink [A](a: A) extends PossiblyMaybe [A] final case object Nowt extends PossiblyMaybe [ Nothing ]
4 5 6
7 8 9 10
object PossiblyMaybeOps { def noneDefault [A](pm: PossiblyMaybe [A])(a: A): A = pm match { case Somefink (x) => x case _ => a } } Note _ in second match, caters for nulls 15
Faces Everywhere
Code
sbt console
Applications
FUNCTOR
1 2
class Functor f where fmap � (a → b) → f a → f b
16
SBT CONSOLE
Faces Everywhere
Code
sbt console
Applications
CONTRAVARIANT FUNCTOR
1 2
class Contravariant f where contramap � (b → a) → f a → f b
18
Faces Everywhere
Code
sbt console
Applications
BI FUNCTOR
1 2
class Bifunctor f where bimap � (a → c) → (b → d) → f a b → f c d
19
Faces Everywhere
Code
sbt console
Applications
PRO FUNCTOR
1 2
class Profunctor f where dimap � (c → a) → (b → d) → f a b → f c d
20
Faces Everywhere
Code
sbt console
Applications
SAME TYPE, MANY INTERFACES
A type defined as a Monad can also be →
An Applicative
→
A Functor
→
And possibly many others :)
21
Faces Everywhere
Code
sbt console
Applications
SAME TYPE, MANY INTERFACES
A type defined as a Monad can also be →
An Applicative
→
A Functor
→
And possibly many others :)
21
Faces Everywhere
Code
sbt console
Applications
SAME TYPE, MANY INTERFACES
A type defined as a Monad can also be →
An Applicative
→
A Functor
→
And possibly many others :)
21
APPLICATIONS
Faces Everywhere
Code
sbt console
Applications
KNOWN USES
→
Monoids:
Accumulators are everywhere, almost
→
Functors:
Lots of places (endo, contravariant, bi, pro)
→
Monads:
→
Applicatives:
→
More … e.g. Arrows, Zippers, Lenses, etc.
Effects, "Linear Happy Path", and more "validations", and more
23
Faces Everywhere
Code
sbt console
Applications
THINKING ALGEBRAICALLY
→
Properties:
→
Data Types:
property based testing: quickcheck, scalacheck
start closed, extend using "type classes", dependent types, etc when relevant
→ Abstractions: build small building blocks, use motar to build solid walls → Dist Systems: using algebraic abstractions, properties to build more useful distributed systems
24
Faces Everywhere
Code
sbt console
Applications
ROYAL FAIL
http://www.flickr.com/photos/dadavidov/
25
Faces Everywhere
Code
sbt console
Applications
QUESTIONS
Questions?
26