Skip to content

Instantly share code, notes, and snippets.

View OlivierBlanvillain's full-sized avatar

Olivier Blanvillain OlivierBlanvillain

View GitHub Profile

Keybase proof

I hereby claim:

  • I am OlivierBlanvillain on github.
  • I am blanvillain (https://keybase.io/blanvillain) on keybase.
  • I have a public key whose fingerprint is F01E F67B B2D4 CCFC A0F8 6E75 DDDB C3BE C93B 9835

To claim this, I am signing this object:

// Needed to make it work with Future, we could use any Functor instead.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.std.scalaFuture._
trait T
trait A
trait B
object DemoOptionT {
@OlivierBlanvillain
OlivierBlanvillain / TypeLevelBacktrack.scala
Last active June 6, 2020 23:52
Example of a workaround for the absence of backtracking in implicit resolution. Original problem statement: https://gist.github.com/atamborrino/daa451aea542e912c2d6
import shapeless.{HList, HNil, ::}
import shapeless.ops.hlist.{Selector, Prepend}
import shapeless.test.illTyped
object TypeLevelBacktrack extends App {
/** [[Parent]] / [[Child]] relationship, father side. */
trait FatherOf[Parent, Child]
/** [[Parent]] / [[Child]] relationship, mother side */
trait MotherOf[Parent, Child]
scalaVersion := "2.11.8"
scalaOrganization := "org.typelevel"
libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.2"
scalacOptions += "-Yliteral-types"

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA

object Playground extends App {
sealed trait IExp
final case class Lit(i: Int) extends IExp
final case class Neg(e: IExp) extends IExp
final case class Add(r: IExp, l: IExp) extends IExp
sealed trait Tree
final case class Leaf(s: String) extends Tree
final case class Node(s: String, ts: List[Tree]) extends Tree
@OlivierBlanvillain
OlivierBlanvillain / ajax.scala
Created March 23, 2017 11:50
Monix based Ajax
object Ajax {
def get(url: String, data: InputData = null, timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = false, responseType: String = "") = {
apply("GET", url, data, timeout, headers, withCredentials, responseType)
}
def post(url: String, data: InputData = null, timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = false, responseType: String = "") = {
lazy val root = project
.aggregate(
`deriving-scalac`,
`deriving-dotty`
)
lazy val `deriving-scalac` = project.in(file(".deriving-scalac"))
.settings(
scalaVersion := "2.12.99-bin-SNAPSHOT", // https://github.com/scala/scala/pull/6050 published local
scalaSource in Compile := baseDirectory.value / "../deriving/src/main/scala",
import reflect.ClassTag
object playground {
/* All combinations of
* - trait / trait with common / extension trait
* - extending class / extension
* - monomorphic / generic implementation
*/