Skip to content

Instantly share code, notes, and snippets.

View b-studios's full-sized avatar

Jonathan Immanuel Brachthäuser b-studios

View GitHub Profile
@b-studios
b-studios / 01-shadowing-implicit-functions.scala
Last active May 22, 2017 15:52
"full" code for example from Martin's talk on implicits
class Table(var rows: List[Row] = Nil) {
def add(r: Row) = { rows = r :: rows }
}
class Row(var cells: List[Cell] = Nil) {
def add(c: Cell) = { cells = c :: cells }
}
case class Cell(content: String)
object test {
def table(init: implicit Table => Unit) = {
@b-studios
b-studios / 01.README.md
Last active November 23, 2020 22:58
Syntactic sugar for step-by-step annotated functions in pointfree style

When defining complex functions in pointfree style, I often find myself switching to pointful style. Sometimes, I even convert to ANF and annotate the types to understand the steps.

After completing the function definition, I often convert back to pointfree style for conciseness. End of the story: To understand it again a couple of weeks later, I start expanding to annotated pointful again.

The small 10-lines library at the end of this post allows to define pointfree functions with intermediate type annotations.

Credits: Agda and EqReasoning for syntactical inspiration.

@b-studios
b-studios / oas.kk
Last active February 3, 2017 15:23
Effect handlers as solution to the expression problem
effect expr<a> {
lit (n: int): a;
add (l: a, r: a): a
}
effect mulExpr<a> {
mul (l: a, r: a): a
}
// forall<a,e>. (() -> <expr<int>|e> a) -> e a
@b-studios
b-studios / fixpoint.scala
Last active February 2, 2017 17:41
Relating fixed points with pattern functors
package object fixpoint {
// The idea here is similar (though inverted) to Matryoschka Based:
// https://github.com/slamdata/matryoshka/blob/master/core/shared/src/main/scala/matryoshka/Based.scala
trait Fix[F[_]] {
type Out
def roll: F[Out] => Out
def unroll: Out => F[Out]
}
trait Functor[F[_]] {
@b-studios
b-studios / CofreeChurch.scala
Created February 1, 2017 18:44
Churchencoding of Cofree Terms
trait Expr[-I, +O] {
def Lit: Int => O
def Add: (I, I) => O
}
trait Term {
def apply[R](alg: Expr[R, R]): R
}
@b-studios
b-studios / tceq.scala
Created January 17, 2017 15:29
Typeconstructor equivalence
object tc {
sealed trait =::=[F[_], G[_]] {
def apply[A](f: F[A]): G[A]
}
object =::= {
implicit def idFunctors[F[_]]: F =::= F = new =::=[F, F] {
def apply[A](f: F[A]): F[A] = f
}
}
@b-studios
b-studios / partial.scala
Created January 13, 2017 08:52
Partially applied "type constructors" in Scala
// 1. Instead of using type parameters, we use type members
trait Expr {
type ExprIn
type ExprOut
type Base[+_]
}
trait Monadic {
type M[+_]
}
@b-studios
b-studios / pipe.scala
Last active January 12, 2017 08:45
F# pipe operator in Scala
package object pipe {
implicit class Pipe[T](t: T) {
def |>[S](f: T => S): S = f(t)
}
def filter[T](f: T => Boolean): List[T] => List[T] = _.filter(f)
def map[A, B](f: A => B): List[A] => List[B] = _.map(f)
// Type inference indeed does play along quite well:
val x: Option[Int] = List(1, 2, 3) |>
@b-studios
b-studios / 0.test-2.scala
Created December 20, 2016 10:50
Variance, Invariance, Nothing et al.
// Finds a R in an T
trait Select[T, R] {
def apply(t: T): R
}
trait LowPrio {
def apply[T, R](f: T => R): Select[T, R] = new Select[T, R] {
def apply(t: T): R = f(t)
}
implicit def autoLeft[T, R, O]: Select[(R, O), R] = Select(_._1)
@b-studios
b-studios / autoproject.scala
Last active December 19, 2016 10:10
Autoprojection
// Finds a R in an T
trait Select[T, R] {
def apply(t: T): R
}
trait LowPrio {
implicit def autoLeft[T, R, O]: Select[(R, O), R] = ???
}
object Select extends LowPrio {
implicit def autoRight[T, R, O]: Select[(O, R), R] = ???