Skip to content

Instantly share code, notes, and snippets.

Avatar

Christophe Calvès chrilves

View GitHub Profile
View highSchoolEquation.scala
/***************************************************************
Just a small example of Algebraic Data Type (ADT) use
to show why they are called "algebraic".
This is a good exercice to check that you understand
the basics of ADT correctly.
Note: Run-it with Dotty (Scala 3) version >= 0.22
dotr -indent -new-syntax -strict -Yerased-terms -Yexplicit-nulls
View Resource.scala
/** Simulates a resource can be created and closed
* like a file handler, database connection handler, etc.
*
* Like the ones above, its methods are only available if
* the resource is opened, it must always be closed
* after use to avoid leaks and closing and already closed
* connection is a fault.
*
* @param name the name of the connection
* @param failureProba the probability opening and closing fail.
View validation-cheat-sheet.scala
import $ivy.`io.github.jto:validation-xml_2.12:2.1.1`
import jto.validation._
import jto.validation.xml._
import jto.validation.xml.Rules._
import scala.xml._
import scala.reflect.runtime.universe._
final case class Test(x: Int, y: Int)
implicit val testR: Rule[Node, Test] =
View Wild_exposed_as_a_GADT.scala
/* Modified version of https://gist.github.com/non/51b83d0abc929cc4f0b153accf2bf02f
* to expose it's GADT's nature and provide the folding function.
*
* Should run out of the box with: amm <the_file>
*/
import $ivy.`org.typelevel:cats-core_2.12:1.6.0`
object demo {
import cats.{Applicative, Functor, Id, Semigroupal, Traverse}
View Lens_and_zipper_equivalence.scala
object LensToZiper {
/*
This is a small sleight of hand to show how
[lenses](https://en.wikibooks.org/wiki/Haskell/Lenses_and_functional_references)
are actually related to to zippers (getters + setters).
This connection give some insight about why lenses
have the properties they have. So let's define all
we need for lenses:
View ForceCovariance.scala
import scala.language.higherKinds
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
/* Any functor is by definition covariant because:
Let A and B be two types such that A <: B, then
(fa: F[A]).map((a:A) => (a:S)) : F[S]
but also
View The_Batch_Monad.scala
/*
This code was made to investigate if it is possible
to use a batch API as if it were a single item one.
More precisely, I have this batch API:
final case class Request(id: Int)
final case class Response(id: Int)
View One_concept_to_explain_them_all_and_in_the_theory_bind_them.scala
/*
This piece of commented code aims to clarify some
misconceptions between several advanced concepts
in pure functional programming/category theory:
free monads, finally tagless approach, algebraic
effects.
These concepts are actually very close. They rely
on similar concepts and even represent the "same"
object (up to isomorphism!) from the theoretical
View GADTs_For_Implicits_Demo.scala
object Test {
/** Simple definition of Heterogeneous Lists
*/
sealed abstract class HList {
def :::[H](head: H): H ::: this.type = Test.:::(head, this)
}
/** The empty list
@chrilves
chrilves / IsA.scala
Created Nov 23, 2018
A proof that the type `A` is of the form `F[elem]` for some type `elem`
View IsA.scala
/** A proof that the type {{{A}}} is of
* the form {{{F[elem]}}} for some type
* {{{elem}}}.
*/
sealed abstract class IsA[F[_], A] {
type elem
def fold[G[_]](p: G[F[elem]]): G[A]
@inline implicit final def to: A =:= F[elem] = {
type G[X] = X =:= F[elem]