- Scala at the Sea: 19 May 2020
- Adam Rosien @arosien
- Inner Product @InnerProductLLC inner-product.com
Functional programming is full of Fancy Words™. Let's collect a list of them to demystify together. Adam will facilitate with live-coding, and will also try to make good jokes.
Here's some terms to bootstrap us: effect, free monad, functor, property-based testing, refinement type, stream, etc.
We will have plenty of time for Q&A during this session.
https://www.inner-product.com/services/training/essential-effects/
How to safely create, compose, and execute effectful Scala programs using the Typelevel cats-effect library.
effect- free monad
functormonad- property-based testing
- scalacheck: http://scalacheck.org/
- munit + scalacheck: https://scalameta.org/munit/docs/integrations/scalacheck.html
- refinement type
- stream
The audience suggested starting with functor, monad, and effect, so here we go!
- captures the notion of
map:List,Option,Futureall havemap - see worksheet for code
- typeclass: interface + implementations for various types
"How do we know an implementation of a typeclass is correct?"
For Functor.map, how do we know it works? Laws!
Laws for Functor:
- identity law:
map(identity)=identity- "doing nothing should really do nothing"
- composition:
fa.map(f).map(g) == fa.map(f andThen g)- "mapping twice should be the same as mapping once with composed transformations"
- captures the notion of
flatMap(andpure) - see worksheet for code
What is an effect? Is it different than a side-effect, which we know is "bad" or "dangerous"?
Examples:
- "passing state after the computation" == state effect, e.g., incrementing a counter when executing something; see worksheet
- "
NullPointerExceptioneffect" ==Option - "Error" effect ==
Either[L, R] - Dependency Injection ==
Reader - println(), a.k.a., logging ==
Writer - asynchronous computation ==
scala.concurrent.Future? Future is not referentially transparent! This can bite you or your friend. cats.effect.IO: do anything, even side-effects!
BIG IDEAS in effects:
- replace side-effects with more structure, more laws, immutability, etc., to make them safe
- the effect is apparent in the type signature; this lets us "see" that something is going on, as opposed to side-effects
- we separate the description of what we want to do from execution of that description
- http://typelevel.org/cats
- books to read: "essential scala" or "scala with cats"
- gitter.im/ - usually there are links to gitter on various project pages https://gitter.im/typelevel/cats
- new effects course: https://www.inner-product.com/services/training/essential-effects/