Skip to content

Instantly share code, notes, and snippets.

@danslapman
danslapman / my_slack_theme
Created March 18, 2020 14:47
Slack theme
#000000,#AAAAAA,#777777,#FFFFFF,#666666,#FFFFFF,#999999,#BBBBBB
@danslapman
danslapman / DefaultsTo.scala
Last active January 24, 2020 14:12
Quirky patterns
//https://github.com/mongodb/mongo-scala-driver/blob/master/bson/src/main/scala/org/mongodb/scala/bson/DefaultHelper.scala
/**
* Neat helper to obtain a default type should one not be given eg:
*
* {{{
* def find[T]()(implicit e: T DefaultsTo Document) { ... }
* }}}
*
* The signature of the `find` method ensures that it can only be called if the caller can supply an object of type
@danslapman
danslapman / PropSubset.scala
Last active February 26, 2024 08:33
Witness that a case class has proper subset of properties of another case class
import shapeless._
import shapeless.labelled.FieldType
import shapeless.ops.record.Selector
import scala.annotation.implicitNotFound
/*
Witnesses, that all fields of `Projection` exists in `Source` with the same types and names
*/
@implicitNotFound("${Projection} is not a valid projection of ${Source}")
@danslapman
danslapman / dtr.scala
Created October 6, 2019 19:07
DateTime range
def inRange(start: LocalTime, end: LocalTime)(dt: LocalDateTime) = {
val time = dt.toLocalTime
if (start.isBefore(end))
(time.isAfter(start) || time == start) && time.isBefore(end)
else
(time.isAfter(start) || time == start) ^ time.isBefore(end)
}
@danslapman
danslapman / mloop.sc
Last active October 23, 2019 08:14
Stateful monad loops in Scala
import $ivy.`org.typelevel::cats-core:2.0.0`
import $plugin.$ivy.`org.typelevel::kind-projector:0.10.3`
import cats._
import cats.instances.option._
import cats.syntax.all._
import scala.annotation.tailrec
def next(offset: Int): Option[Vector[Int]] = if (offset < 50) Vector.tabulate(10)(identity).some else Vector.empty[Int].some
@danslapman
danslapman / AckedReplicate.scala
Created June 26, 2019 11:30
Acked replicateN
import scala.concurrent.{ExecutionContext, Future, Promise}
import cats.instances.future._
import cats.syntax.functor._
import com.timcharper.acked.AckTup
import shapeless.{Nat, Sized}
import shapeless.syntax.sized._
import shapeless.ops.nat.ToInt
object AckedReplicate {
def replicateN[T, N <: Nat](implicit size: ToInt[N]): AckTup[T] => Sized[List[AckTup[T]], N] = {
@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 {
@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 / 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 / sbtopts
Created March 12, 2019 13:47
sbt configuration
cat /usr/local/etc/sbtopts
# ------------------------------------------------ #
# The SBT Configuration file. #
# ------------------------------------------------ #
-J-Xmx6G