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 / monadic-oa.scala
Created June 8, 2016 13:19
Some experiments with monadic object algebras
object monadicOA {
type ??? = Any
trait Monad[M[+_]] {
def unit[A]: A => M[A]
def bind[A, B]: M[A] => (A => M[B]) => M[B]
def map[A, B]: M[A] => (A => B) => M[B]
}
object Monad {
@b-studios
b-studios / TLFoldable.scala
Last active July 31, 2016 18:58
Experiments with type-level foldables
sealed trait Nat
trait Succ[T <: Nat] <: Nat
trait Zero <: Nat
sealed trait Bool { type Not <: Bool }
trait True <: Bool { type Not = False }
trait False <: Bool { type Not = True }
trait Foldable[Base] {
@b-studios
b-studios / TLChurch.scala
Last active July 31, 2016 19:52
Simple example of type-level church encoding
object tl_church {
trait Booleans {
type Bool <: { type Not <: Bool }
type True <: Bool
type False <: Bool
}
trait Nats {
type Nat
@b-studios
b-studios / stateful-pimp.scala
Created August 2, 2016 16:13
Nanolibrary for a stateful pimp-my-library pattern.
package object stateful {
import scala.collection.mutable
trait Stateful {
private val cache = mutable.HashMap.empty[Int, Any]
// TODO use some reasonable HashMap implementation here
// TODO how slow is the reflection here, perform microbenchmarks?
protected[stateful] def getStateOrElseUpdate(d: Decorate[_])(s: => d.State): d.State =
@b-studios
b-studios / TypeConstructor.scala
Created September 20, 2016 10:18
Trying to help the compiler inferring type constructors
// Default functor and monad TCs
trait Functor[F[_]] {
def map[A, B](fa: F[A], f: A => B): F[B]
}
trait Monad[M[_]] {
def unit[A](value: A): M[A]
def flatMap[A, B](ma: M[A], f: A => M[B]): M[B]
def map[A, B](ma: M[A], f: A => B): M[B] = flatMap(ma, f andThen unit)
}
@b-studios
b-studios / FreerMonads.scala
Created September 20, 2016 16:46
A straightforward translation of "Freer Monads, More Extensible Effects" to Scala
object freer {
trait Functor[F[_]] {
def map[A, B](fa: F[A], f: A => B): F[B]
}
object Functor {
@inline
def apply[F[_]](implicit f: Functor[F]): Functor[F] = f
implicit class FunctorOps[A, F[_]: Functor](fa: F[A]) {
@b-studios
b-studios / build.sbt
Last active December 14, 2016 15:09
New encoding for extensible objects
scalaVersion := "2.11.7"
// For this dependency you have to clone
// https://github.com/b-studios/MixinComposition
// and run `sbt publishLocal`
libraryDependencies += "de.unimarburg" % "mixin-composition_2.11" % "0.2-SNAPSHOT"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6"
@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] = ???
@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 / 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) |>