Skip to content

Instantly share code, notes, and snippets.

View gvolpe's full-sized avatar
🤓
https://leanpub.com/u/gvolpe

Gabriel Volpe gvolpe

🤓
https://leanpub.com/u/gvolpe
View GitHub Profile
def scanEval[F[_]: Sync, S, A](p: Stream[F, A])(start: F[S])(f: (S, A) => F[S]): Stream[F, S] = {
def zipper(ref: Ref[F, S]): Stream[F, S] =
p.zip(Stream.eval(ref.get).repeat).evalMap { case (a, s) =>
for {
ns <- f(s, a)
_ <- ref.set(ns)
} yield ns
}
for {
@gvolpe
gvolpe / shared-state-in-fp.md
Last active March 15, 2022 20:27
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

@gvolpe
gvolpe / di-in-fp.md
Last active April 24, 2024 20:51
Dependency Injection in Functional Programming

Dependency Injection in Functional Programming

There exist several DI frameworks / libraries in the Scala ecosystem. But the more functional code you write the more you'll realize there's no need to use any of them.

A few of the most claimed benefits are the following:

  • Dependency Injection.
  • Life cycle management.
  • Dependency graph rewriting.
import cats.effect.{ExitCode, IO, IOApp}
import cats.instances.list._
import cats.syntax.all._
import fs2._
import scala.concurrent.duration._
object jobs extends IOApp {
val largeStream: Stream[IO, Int] = Stream.range(0, 100).covary[IO]
@gvolpe
gvolpe / completion-stage-to-io.scala
Last active June 12, 2018 13:33
Java's CompletionStage to Cats Effect F[_]: Async / IO
import java.util.concurrent.CompletionStage
import cats.effect.Async
import cats.syntax.flatMap._
case object EmptyValue extends Throwable
def to[F[_], A](fa: F[CompletionStage[A]])(implicit F: Async[F]): F[A] = {
fa.flatMap { f =>
F.async[A] { cb =>

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

import cats.MonadError
import cats.effect.IO
import cats.effect.concurrent.Deferred
import cats.instances.list._
import cats.instances.string._
import cats.kernel.Monoid
import cats.syntax.all._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def blocking[F[_], A](fa: F[A])(implicit F: Async[F], timer: Timer[F]): F[A] =
F.bracket(Async.shift(blockingPool))(_ => ....)(_ => timer.shift)
@gvolpe
gvolpe / RankN shift-and-shiftback.md
Created July 28, 2018 05:25 — forked from SystemFw/RankN shift-and-shiftback.md
Cats-effect, blocking, RankN-types.

cats-effect

The cats-effect project defines a purely functional effect type (IO[A]), and associated typeclasses defining its behaviour. The ones we care about for this example are:

trait Sync[F[_]] extends MonadError[F, Throwable] {
   def delay[A](a: => A): F[A]
   ...
}

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA