Skip to content

Instantly share code, notes, and snippets.

@danslapman
danslapman / ApplyOne.sc
Created July 10, 2018 20:15
Combine partial functions of different types
import scala.reflect.runtime.universe._
type ¬[A] = A => Nothing
type ∨[T, U] = ¬[¬[T] with ¬[U]]
type ¬¬[A] = ¬[¬[A]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) }
val intpf: PartialFunction[Int, Int] = { case i: Int => i }
val strpf: PartialFunction[String, String] = { case str: String => str }
@danslapman
danslapman / fields.sc
Created July 19, 2018 10:37
Get field names of case class using shapeless
import $ivy.`com.chuusai::shapeless:2.3.3`
import shapeless._
trait Fields[T] {
def fields: List[String]
override def toString: String = fields.mkString(", ")
}
object Fields extends LabelledProductTypeClassCompanion[Fields] {
def apply[T](fs: List[String]): Fields[T] = new Fields[T] {
override def fields: List[String] = fs
@danslapman
danslapman / hsequence.sc
Last active October 6, 2018 21:17
Auto-generating patches for fields
import $ivy.`org.typelevel::cats-core:1.4.0`
import $ivy.`org.typelevel::kittens:1.1.1`
import cats._
import cats.data.{Validated, ValidatedNel}
//import cats.instances.list._
import cats.sequence._
import shapeless._
import shapeless.labelled._
import shapeless.record._
@danslapman
danslapman / quill_dynamic_query.sc
Created December 17, 2018 12:35
Dynamic querying in Quill
import $ivy.`io.getquill::quill:2.6.0`
import io.getquill._
val ctx = new SqlMirrorContext(MirrorSqlDialect, Literal)
import ctx._
case class Entity(field1: String, field2: Int)
def getEntity(field1: Option[String], field2: Option[Int]) = {
@danslapman
danslapman / coalesce.sc
Created December 17, 2018 16:22
coalesce for Quill
def coalesce[T] = quote { column: Option[T] => default: T =>
infix"coalesce($column, $default)".as[T]
}
@danslapman
danslapman / kek
Created March 7, 2019 18:53
homebrew setup
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in /usr/local/sbin.
Consider setting the PATH for example like so
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
@danslapman
danslapman / sbtopts
Created March 12, 2019 13:47
sbt configuration
cat /usr/local/etc/sbtopts
# ------------------------------------------------ #
# The SBT Configuration file. #
# ------------------------------------------------ #
-J-Xmx6G
@danslapman
danslapman / prettifier.scala
Created March 13, 2019 12:22
scalatest bson Prettifier
import org.scalactic.Prettifier
import reactivemongo.bson._
implicit val bdocPrettifier = Prettifier({
case doc: BSONDocument => BSONDocument.pretty(doc)
})
@danslapman
danslapman / yik.sc
Created May 8, 2019 12:15
Encoding enumeratum enums with morphling
case class TEnum[P[_], R, A](repr: P[R], ve: ValueEnum[R, A])(implicit val ev: A <:< ValueEnumEntry[R]) extends TType[P, A]
..
def tEnum[I, E](repr: Schema[TSchema, I], ve: ValueEnum[I, E])(implicit ev: E <:< ValueEnumEntry[I]): Schema[TSchema, E] =
prim(HMutu(TEnum(repr, ve)))
..
override val encoder: TSchema ~> Encoder = new (TSchema ~> Encoder) {
@danslapman
danslapman / dropNulls.scala
Created June 21, 2019 13:30
dropNulls from BSONDocument
import cats.free.Trampoline
import cats.instances.stream._
import cats.instances.try_._
import cats.syntax.nested._
import cats.syntax.traverse._
import reactivemongo.bson._
import scala.util.{Failure, Success, Try}
implicit class BSONValueOps(private val bv: BSONValue) extends AnyVal {