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
@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.
@gvolpe
gvolpe / scala3.3.0-RC1-value-discard.scala
Created January 31, 2023 09:51
The "-Wvalue-discard" flag doesn't seem to work with the `IO` monad
//> using scala "3.3.0-RC1"
//> using lib "org.typelevel::cats-effect:3.4.5"
//> using plugin "com.github.ghik:::zerowaste:0.2.3"
//> using options "-Wunused:imports"
//> using options "-Werror"
//> using options "-Wvalue-discard"
import cats.effect.*
object Main extends IOApp.Simple:
@gvolpe
gvolpe / parTraverseN.scala
Last active February 15, 2024 15:29
parTraverse with a limit of N using a Semaphore
import cats.Traverse
import cats.effect._
import cats.effect.concurrent.Semaphore
import cats.temp.par._
import cats.syntax.all._
import scala.concurrent.duration._
object Main extends IOApp {
import ParTask._
@gvolpe
gvolpe / build.sbt
Last active September 29, 2023 10:26
Minimal SBT build for pfps-examples
ThisBuild / scalaVersion := "2.13.5"
lazy val root = (project in file("."))
.settings(
name := "minimal",
libraryDependencies ++= Seq(
compilerPlugin(
"org.typelevel" %% "kind-projector" % "0.11.3"
cross CrossVersion.full
),
@gvolpe
gvolpe / JFuture.scala
Last active September 7, 2023 07:09
Java Future to Cats Effect F[_]
import java.util.concurrent.Future
import cats.effect.{Effect, Sync, Timer}
import cats.syntax.all._
import fs2._
import fs2.async.mutable.Signal
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
@gvolpe
gvolpe / CsvApp.scala
Last active July 6, 2023 00:19
CSV file reader using the Fs2 streaming library.
import cats.effect._
import cats.syntax.functor._
import fs2._
import java.nio.file.Paths
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
import scala.util.Try
object CsvApp extends IOApp {

Scalafmt config

How can I get scalafmt to format my case class like this?

case class Person(
    name: String,
    age: Int
) derives Eq, Show
import cats.{ ApplicativeError, MonadError }
import cats.data.{ Kleisli, OptionT }
import cats.effect.Sync
import cats.effect.concurrent.Ref
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s._
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe._
import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.string.MatchesRegex
object refinements {
type EmailPred = MatchesRegex[W.`"""(?=[^\\s]+)(?=(\\w+)@([\\w\\.]+))"""`.T]
type Email = String Refined EmailPred
}
@gvolpe
gvolpe / IsUUID.scala
Last active July 22, 2022 16:17
Scala 3 custom newtypes
import java.util.UUID
import monocle.Iso
trait IsUUID[A]:
def iso: Iso[UUID, A]
object IsUUID:
def apply[A](using ev: IsUUID[A]): IsUUID[A] = ev