Skip to content

Instantly share code, notes, and snippets.

@AndreasKostler
AndreasKostler / flatten.scala
Created November 26, 2019 21:21
Theorem flatten
// Super simple, recursive flatten
// The _ means we have a weakly polymorphic type
// i.e. we don't care about what type _ is (it will, or will not, be resolved later)
// the type signature for flatMap on Seq is Seq[A] => (A => Seq[B]) => Seq[B]
def flatten(s: Seq[_]): Seq[_] = s flatMap {
case s: Seq[_] => flatten(s) // If s is a Seq, we recursively flatten
case s => Seq(s) // If s is atomic, we're done; just package up s
}
// degenerate case
@AndreasKostler
AndreasKostler / keybase.md
Last active September 13, 2019 08:19
keybase.md

Keybase proof

I hereby claim:

  • I am andreaskostler on github.
  • I am kandre1980 (https://keybase.io/kandre1980) on keybase.
  • I have a public key ASAXdg4Wt3DUsCSjWq9LXIf6oCNka0lb1LbHvzYhbEyKMAo

To claim this, I am signing this object:

@AndreasKostler
AndreasKostler / SellId.scala
Last active April 16, 2019 09:16
Typed routing
package model
import algebra.DeleteDealerErrors.SellIdError
import cats.MonadError
import play.api.libs.json._
import play.api.mvc.PathBindable
import scala.util.Try
final case class SellId(value: String)
@AndreasKostler
AndreasKostler / unboxed.scala
Last active June 24, 2016 07:00
Unboxed recursive union types via the Curry Howard Correspondence
object UnionTypes {
private type ¬[A] = A => Nothing
private type ¬¬[A] = ¬[¬[A]]
private type ∈[S, T <: Disjunction] = ¬¬[S] <:< ¬[T#D]
private type ∉[S, T <: Disjunction] = ¬¬[S] <:!< ¬[T#D]
/** `S` is `T`. */
type Is[S, T] = ∈[S, OneOf[T]#Or[Nothing]]
object Def {
import shapeless.{ =:!=, HNil, HList, Nat, Poly1, Succ, Witness }
import shapeless.nat.{ _0 => D0, _1 => D1, _2 => D2, _3 => D3 }
import shapeless.ops.nat.{ Diff, GT, LTEq, ToInt }
import shapeless.ops.hlist.{ Mapper, ToTraversable, ZipConst }
trait Value {
def toShortString = ???
}
object Value {
object Def {
import shapeless.{ =:!=, HNil, HList, Nat, Poly1, Succ, Witness }
import shapeless.nat.{ _0 => D0, _1 => D1, _2 => D2, _3 => D3 }
import shapeless.ops.nat.{ Diff, GT, LTEq, ToInt }
import shapeless.ops.hlist.{ Mapper, ToTraversable, ZipConst }
trait Value {
def toShortString = ???
}
object Value {
object Abc {
import shapeless.{ =:!=, Nat, Succ }
import shapeless.nat.{ _0 => D0, _1 => D1, _2 => D2, _3 => D3 }
import shapeless.ops.nat.{ Diff, GT, LTEq, ToInt }
import shapeless.ops.hlist._
trait Value {
def toShortString = ???
}
object Value {
@AndreasKostler
AndreasKostler / 1.scala
Last active September 23, 2015 06:32
Shapeless inference puzzle
import shapeless._
trait Test {
def ex[InL <: HList, H, T <: HList](in : InL)(implicit isHCons: IsHCons1.Aux[InL, List[H], T])
val ii: List[Int]::List[Int]::HNil
ex(ii)
}
trait IsHCons1[L] { type H; type T <: HList}
object IsHCons1 {
import shapeless._
import syntax._
import poly._
object LeftFolderOps {
trait LeftFolder[C <: Coproduct, In, F] extends Serializable {
def apply(c: C, in: In): In
}
trait LeftFolder[C <: Coproduct, In, F] extends Serializable {
def apply(c: C, in: In): In
}
object LeftFolder {
def apply[C <: Coproduct, In, F](implicit folder: LeftFolder[C, In, F]) = folder
implicit def hdLeftFolder[H, T <: Coproduct, In, F]
(implicit f: Case2.Aux[F, In, H, In]): LeftFolder[H :+: T, In, F] = new LeftFolder[H :+: T, In, F] {
def apply(c: H :+: T, in: In): In = c match {