This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| //=== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // $ scalac Test.scala | |
| // and then | |
| // $ javap -c Test$ | |
| object Test { | |
| def s1: String = | |
| "aaa" + | |
| "/bbb" + | |
| "/ccc" | |
| def s2: String = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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! | |
| //--------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*** 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | |
| } | |
| //-------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |