Skip to content

Instantly share code, notes, and snippets.

View joroKr21's full-sized avatar
🏠
Working from home

Georgi Krastev joroKr21

🏠
Working from home
View GitHub Profile
import scala.annotation.tailrec
sealed trait Tree[+A] {
def isSymmetric: Boolean
}
object Tree {
final case class Node[+A](left: Tree[A], value: A, right: Tree[A]) extends Tree[A] {
def isSymmetric: Boolean = {
@tailrec def loop(
@joroKr21
joroKr21 / shapeless-convert.scala
Created October 30, 2020 23:18
Simple conversions between case classes and sealed traits with Shapeless
object Test {
import shapeless._
import shapeless.labelled._
trait Convert[A, B] {
def apply(a: A): B
}
object Convert {
implicit def id[A]: Convert[A, A] = x => x
@joroKr21
joroKr21 / typelevelcps.scala
Created June 7, 2018 20:23 — forked from milessabin/typelevelcps.scala
Using type level continuation passing style to rewrite a whitebox macro (which relies on fundep materialization) as a blackbox macro
import scala.language.higherKinds
// Whitebox ...
trait Schema[T, R] {
def conv(t: T): R
}
object Schema {
// Whitebox macro: R is computed from T
implicit def mkSchema[T, R]: Schema[T, R] = ??? // macro ...
/** Enable MTL-style programming by avoiding ambiguous implicits. */
class MTL[M](val M: M) extends AnyVal
object MTL {
type MonadMTL[F[_]] <: Monad[F]
implicit def mtl[M](implicit M: M): MTL[M] = new MTL[M](M)
private def MonadMTL[M[f[_]] <: Monad[f], F[_]](M: M[F]): MonadMTL[F] =
M.asInstanceOf[MonadMTL[F]]
/** An artificial hierarchy for MTL type classes. */
sealed abstract class SingletonOf[A, +B] {
}
object SingletonOf {
}
type <::[A, +B] = SingletonOf[A, B]
def single[A <: Singleton](a: A): SingletonOf[A, A] =
new SingletonOf[A, A] { }
@joroKr21
joroKr21 / Unfix.scala
Last active September 22, 2017 09:57
import cats.Functor
import shapeless._
import scala.language.higherKinds
trait Unfix[A] {
type F[_]
def apply(a: A): Fix[F]
}
@joroKr21
joroKr21 / MkLenses.scala
Created December 1, 2016 15:14
Materialize lenses for all keys in a record.
import shapeless._
import shapeless.labelled._
import shapeless.ops.hlist._
import shapeless.ops.record._
import shapeless.syntax.singleton._
object MkLenses {
object mkLenses extends Poly1 {
implicit def mkLens[R <: HList, K](implicit mk: MkRecordSelectLens[R, K]) =
at[K](_ => mk())
import shapeless._
import shapeless.ops.coproduct.Selector
import shapeless.ops.coproduct.ToHList
import shapeless.ops.hlist.LiftAll
import shapeless.ops.hlist.Mapper
import shapeless.ops.hlist.ZipConst
object Partition extends App {
trait Dummy[-A]
@joroKr21
joroKr21 / HBaseOutputFormat.scala
Created February 8, 2015 19:31
HBase OutputFormat for Apache Flink
import org.apache.flink.api.common.io.OutputFormat
import org.apache.flink.configuration.Configuration
import org.apache.hadoop.hbase._
import client._
import util.Bytes
import language.{ implicitConversions, reflectiveCalls }
import java.math.BigDecimal
import java.nio.ByteBuffer
@joroKr21
joroKr21 / EvalExpr.java
Created July 26, 2014 17:04
The infamous expression problem solved with recursive types in Java
interface EvalExpr<E extends EvalExpr<E>> extends Expr<E> {
int eval();
}
interface EvalExprFix extends EvalExpr<EvalExprFix> {
}
class EvalConst<E extends EvalExpr<E>> extends Const<E> implements EvalExpr<E> {