
Typelevel Summit, March 2016
[.grey]##
Not-Invented-Here
you receive: more power
oodles of composable libraries
your job
… and more
It’s so useful… why?
The Typelevel ecosystem
Why the Typelevel ecosystem works
Typelevel adoption and integration
databases, message queues, Kafka stuff, cloud storage, …
service HTTP requests and responses, JSON handling, clients to other services, …
property-based testing, …
parsing, validation, typed invariants, …
command-line parsing, configuration, logging, …
While ensuring we have:
safe and well-defined composability
managed side-effects and concurrency
complex stream processing (if desired)
http4s | doobie | ||
fs2 | |||
Cats Effect | |||
Cats |
☝︎
stacks grow upwards
|
|
|
|
|
|
|
|
|
|
|
|
More typeclasses:
MonadError
: raiseError
, handleErrorWith
, …
Parallel
: parMapN
, parTupled
, …
…
Data types:
Chain
: efficient List
-like data structure)
Ior
: modeling errors vs. warnings
NonEmptyList
, NonEmptyChain
: non-empty collections
and more
Cats Effect |
Cats |
Typeclasses:
MonadCancel
: cancelation-safe finalizers
Spawn
: concurrent forking and cancelation
Concurrent
: concurrent coordination
Sync
: safely suspend side-effects
and more
Data types:
IO
: safely represent and compose side-effects
Resource
: safe lifecycle management of state
Fiber
: handle to concurrently executing effects
Ref
: atomically updated state
Deferred
: blocking data synchronization
Semaphore
: limit concurrent access
Queue
: concurrent FIFO
and more
val hello = IO.println("hello")
val world = IO.println("world")
(hello, world).tupled
hello world
val hello = IO.println("hello")
val world = IO.println("world")
- (hello, world).tupled
+ (hello, world).parTupled
Parallel execution (parMapN
, parTupled
, etc.)
hello world
or
world hello
fs2 | [.grey]## |
Cats Effect | effects, concurrency, resources, … |
Cats | sequencing, … |
http4s | [.grey]## |
fs2 | streaming requests, responses |
Cats Effect | effects, concurrency, resources, … |
Cats | sequencing, … |
doobie | [.small.grey]## |
fs2 | streaming queries, results |
Cats Effect | effects, concurrency, resources, … |
Cats | sequencing, … |
val qi: ConnectionIO[Int] = ???
val qd: ConnectionIO[Double] = ???
val both: ConnectionIO[(Int, Double)] =
for {
i <- qi
d <- qd
} yield (i, d)
Sequential composition with monad (flatMap
)
val qi: ConnectionIO[Int] = ???
val qd: ConnectionIO[Double] = ???
val both: ConnectionIO[(Int, Double)] =
- for {
- i <- qi
- d <- qd
- } yield (i, d)
+ (qi, qd).tupled
Independent composition with applicative (mapN
, tupled
, etc.)
“A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over, beginning with a working simple system.”
Why?
A product with the business logic removed, but all of the assumptions left in.
[.grey]#
[.grey]#
— Tim Perrett, Frameworks are fundamentally broken
remove assumptions
no assumptions
How much effort is it to add a new library?
What existing code do you need to change?
How well do they work together? (friction)
Anti-pattern: Introducing a library that has multiplicative (exponential?) integration costs, because it requires high coupling.
Claim: Using Typelevel libraries have (only!) additive costs.
Because of: composability, parametricity, lawfulness, closure, …
?
project sites
blog posts
books
training
mentoring
GitHub
meetups
conferences
streamers
community-led
for-hire
tooling
Using Future
, want to gain safety and reasonability via effect types like IO
.
Divide and conquer with Cats Effect.
The abstractions of Cats, etc., are:
composable;
closed; and
have no side-effects.
With Cats Effect even side-effecting code can be made safe.
The Typelevel layers are orthogonal to your domain.
Discord: http://sca.la/typeleveldiscord ☜ super helpful people!
Books
Scala with Cats by Noel Welsh and Dave Gurnell scalawithcats.com
Essential Effects by Adam Rosien essentialeffects.dev ☜ my book
Practical FP in Scala by Gabriel Volpe leanpub.com/pfp-scala
Adam Rosien @arosien
Hire us to help with your projects, or train your staff!