Take it from these guys
We'll use cats and cats-effect, Typelevel projects
Lack of side effects
Lack of referential transparency
for any expression `a`
given `x = a`
All occurrences of `x` in a program `p` can be replaced with `a`
val prog1 = (x, x)
val prog2 = (a, a)
prog1 == prog2
If nothing has happened yet, how do we actually run an IO?
Do we really need to do unsafe
?
No ;) (but it depends™)
Richest link you'll see today: Documentation of cats.effect.IO
(concurrency, parallelism, threadpools, referential transparency, everything)
(prepareRepo *> runInRepo).guarantee(cleanup)
a *> b == a.flatMap(_ => b) //import cats.syntax.apply._
Depends solely on how you compose pieces
Given a list of tasks:
case class Task(skill: Skill)
case class Skill(name: String)
Execute all of them and return the total time.
Rules:
throw
Either
NoThrows.scala
tagless final + MTL
What about effects?
Doesn't work
try/finally
isn't suitable for effects
JavaApisResource.scala
Demo time? ActiveMqResource.scala
import cattrix.Metrics
import dbframework.DatabaseQuery
def databaseQuery: IO[DatabaseQuery] = ???
val prog: Metrics.Step[IO, DatabaseQuery] = for {
t <- Metrics.timer("time")
_ <- Metrics.incCounter("active")
query <- Metrics.run(() => database.query("12345"))
_ <- Metrics.decCounter("active")
_ <- Metrics.time(t)
_ <- Metrics.mark("success")
} yield query
Or roll your own
Carry around a `DBIO ~> F`
Just use fs2
Or scalaz-zio stream (when it's ready ;))
Use Ref/Deferred/MVar
Source: @impurepics
Use Semaphore
Use fs2 Queue/Topic
¯\_(ツ)_/¯
No apparent pure solution in sight
Stick to Akka (for now)
Code is red in IntelliJ? https://impurepics.com/posts/2018-08-09-troubleshoot-cats.html Code doesn't compile? Turn on partial unification, don't import conflicting syntax implicits
This talk wouldn't be possible without countless hours of work of:
Alex Nedelcu, Fabio Labella, John A De Goes, Michael Pilquist, Daniel Spiewak, Ross A. Baker, Paul Chiusano, Pavel Chlupacekand many others
Slides: https://git.io/fp017
Code: https://git.io/fp01d
https://fs2.io/concurrency-primitives.html
https://vimeo.com/294736344
https://www.youtube.com/watch?v=x3GLwl1FxcA
https://twitter.com/jdegoes/status/936301872066977792
https://twitter.com/impurepics/status/983407934574153728
https://github.com/pauljamescleary/scala-pet-store
https://www.youtube.com/watch?v=oFk8-a1FSP0
https://www.youtube.com/watch?v=sxudIMiOo68
https://www.youtube.com/watch?v=GZPup5Iuaqw
https://www.youtube.com/watch?v=EL3xy9DKhno
https://www.youtube.com/watch?v=X-cEGEJMx_4
https://www.youtube.com/watch?v=0jIaeXMaH2c
https://www.youtube.com/watch?v=po3wmq4S15A
https://www.youtube.com/watch?v=7xSfLPD6tiQ
https://www.youtube.com/watch?v=5S03zTekRJc
https://typelevel.org/blog/2018/10/06/intro-to-mtl.html
https://typelevel.org/blog/2018/08/25/http4s-error-handling-mtl.html
https://typelevel.org/blog/2018/08/07/refactoring-monads.html
https://typelevel.org/blog/2018/06/07/shared-state-in-fp.html
https://www.reddit.com/r/scala/comments/8ygjcq/can_someone_explain_to_me_the_benefits_of_io/e2jfp9b/
https://github.com/gvolpe/typelevel-stack.g8
https://github.com/gvolpe/http4s-good-practices
https://github.com/gvolpe/advanced-http4s
https://github.com/kubukoz/brick-store
https://github.com/kubukoz/classy-playground
https://github.com/ChristopherDavenport/log4cats
https://github.com/ChristopherDavenport/linebacker
https://github.com/ChristopherDavenport/cats-par
https://github.com/ChristopherDavenport/log4cats-writer-example
https://github.com/SystemFw/upperbound
Get in touch