Skip to content

Instantly share code, notes, and snippets.

View programaker's full-sized avatar

Marcelo da Silva Gomes programaker

View GitHub Profile
@programaker
programaker / 01-with-colima.txt
Last active January 6, 2023 11:16
Replace Docker Desktop
# Replacing Docker Desktop with Colima
https://jacobtomlinson.dev/posts/2022/goodbye-docker-desktop-for-mac-hello-colima/
https://github.com/abiosoft/colima
https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#docker-socket-location
It allows us to keep using docker and docker-compose normally, which is good for legacy scripts.
===
@programaker
programaker / alternative-bitraverse.scala
Last active November 16, 2021 18:58
Dealing with errors using Alternative and Bitraverse typeclasses
import cats.effect.IO
def doThings(things: List[Int]): IO[List[Either[Throwable, String]]] = {
import cats.syntax.traverse._
things.traverse { i =>
if (i % 2 == 0)
IO(Right(s"$i = yay!"))
else
IO(Left(new Exception(s"$i = oh no!")))
@programaker
programaker / align.scala
Created November 8, 2021 17:38
The Align typeclass
import scala.util.Try
import cats.Functor
import cats.Align
import cats.data.Ior
type Decimal = Int
type Roman = String
def f[F[_]: Align: Functor](fi: F[Decimal], fs: F[Roman]): F[String] =
import cats.syntax.align.* // <- HERE
@programaker
programaker / list_lazylist_iterator_view.scala
Last active October 28, 2021 17:35
Scala List x LazyList x Iterator x View
import scala.util.Try
def compute(n: Int): Int =
val res = n * 10
println(s">>> computing $n -> $res")
res
val n = 4
val nth = 1
@programaker
programaker / chainingTapping.scala
Created October 21, 2021 17:26
Chaining & tapping
import cats.effect.std.{Console, Random}
import cats.effect.{IO, Sync}
import scala.util.{Random => RandomStd}
/*
* Std lib has added `tap` and `pipe` functions for all types.
* With `tap` we can use a function to inspect the value without changing it.
* `pipe` is the equivalent of `|>` from F# or Elixir; it sends the value to a function.
* */
@programaker
programaker / exportClause.scala
Last active October 21, 2021 14:36
Scala 3 export clauses
/*
* The good practices say we shouldn't use the same type for everything (domain, json, persistence, ...), but
* most of the time those types are very similar - if not exactly identical - creating a lot of duplication.
*
* Inheritance to the rescue? Nah... not very FP and even the OOP folks are aware that we should
* prefer composition over it. But composition has it's own challenges (Law of Demeter for instance).
*
* Let's see how the new `Export Clauses` in Scala 3 can help with that:
* https://docs.scala-lang.org/scala3/reference/other-new-features/export.html
* */
@programaker
programaker / functionFunctor.scala
Created September 8, 2021 17:18
Functions are Functors
import cats.syntax.functor._
// Some types
trait A
trait B
trait C
// Some functions
val f: A => B = ???
val g: B => C = ???
@programaker
programaker / foldLeftM.scala
Last active October 21, 2021 14:48
Today Traverse was not the answer; it was Foldable
import cats.effect.IO
import cats.syntax.foldable._
import cats.syntax.applicative._
import cats.syntax.applicativeError._
import cats.{ApplicativeThrow, Monad}
import scala.util.Try
// Stores type aliases for effects that have more then one type parameter
object Fs {
@programaker
programaker / customRefinements.scala
Created June 19, 2021 11:29
Custom refinements for domain types
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.boolean.{And, Not}
import eu.timepit.refined.refineV
/* Domain types */
final case class Customer(id: Long, name: String, age: Int)
final case class Order(description: String)
//===
@programaker
programaker / context-functions.scala
Last active May 31, 2021 08:38
Scala 3 context functions
import cats.Monad
import cats.syntax.functor.*
import cats.effect.IO
import cats.effect.unsafe.implicits.global
case class Frunfles(id: Long, name: String)
trait DatabaseConnection:
def exec[F[_]: Monad](op: String): F[Unit]