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 / 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]
@programaker
programaker / Test.scala
Created April 8, 2021 11:50
How String concatenation is optimized by Scala/Java
// $ scalac Test.scala
// and then
// $ javap -c Test$
object Test {
def s1: String =
"aaa" +
"/bbb" +
"/ccc"
def s2: String = {
@programaker
programaker / lst-valueof.scala
Last active December 15, 2020 09:26
Literal singleton types and ValueOf typeclass
trait DataSource[A] {
def source: Option[A]
}
object DataSource {
implicit val EvenIntDataSource: DataSource[Int] = new DataSource[Int] {
import scala.util.Random
override def source: Option[Int] =
Some(Random.between(100, 1000)).filter(_ % 2 == 0)
}
@programaker
programaker / classes-as-modules-of-partially-applied-functions.scala
Last active May 29, 2021 12:49
Classes as modules of partially-applied functions
//Functional programming is all about:
//. Pure, total and deterministic functions
//. ADTs (Algebraic Data Types)
//. Typeclasses
//However, in hybrid languages like Scala, OOP classes can still play a role!
//---------------------------------------------------------
@programaker
programaker / type-safety-through-phantom-types.scala
Last active January 10, 2021 13:38
Type-safety through phantom types
/*** Type-safety through Phantom types ***/
sealed trait Status
object Status {
case object Enabled extends Status
case object Disabled extends Status
//Using type aliases to avoid `Object.type` spreading all over the code
type Enabled = Enabled.type
type Disabled = Disabled.type
@programaker
programaker / about-variance.scala
Last active August 28, 2019 16:27
Scala: about variance
class Producer[+X](x: X) {
def produce: X = x
}
class Consumer[-X](name: String) {
def consume(x: X): Unit = println(s">>> $name is consuming $x polimorfically")
}
//--------------------------------------
@programaker
programaker / dot-zshrc.sh
Last active January 18, 2022 21:06
My .zshrc file
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
@programaker
programaker / monads-are-monoids-in-the-category-of-endofunctors.txt
Created November 3, 2018 02:57
Monads are Monoids in the Category of Endofunctors
"Monads are Monoids in the Category of Endofunctors"
If you are a functional programmer, you've probably heard this statement before.
To understand it, first we need to understand its component parts:
. Category
. Monoid
. Endofunctor
After this, we will glue those parts together piece by piece to grasp the whole.
@programaker
programaker / why-do-we-need-monads.scala
Last active August 13, 2021 04:19
Why do we need Monads?
//Functional programming is programming combining functions.
//
//We have a bunch of simple functions, then combine them into
//more and more complex functions until have a full program.
//
//The simplest case is when the functions are COMPOSABLE, for example, given the functions:
f: A => B
g: B => C
h: C => D