Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / my_slack_theme
Created March 18, 2020 14:47
Slack theme
#000000,#AAAAAA,#777777,#FFFFFF,#666666,#FFFFFF,#999999,#BBBBBB
@danslapman
danslapman / asdf.scala
Created September 25, 2020 13:37
asdf
@derive(encoder(identity, Some("statusCode")))
sealed trait Result[T]
object Result {
implicit val swaggerConfig: SwaggerTypeable.Config =
SwaggerTypeable.defaultConfig.withDiscriminator("statusCode")
.copy(mangleTypeParams = false)
implicit def typeable[T: Tag](implicit tType: SwaggerTypeable[T]): SwaggerTypeable[Result[T]] = {
val updateRefName: Endo[SwaggerType] =
(SwaggerType.refPrism >> SwaggerRef.name)
@danslapman
danslapman / date_time_format.sc
Created April 15, 2021 14:56
DateTimeFormatter interpolator
import $ivy.`com.propensive::contextual-core:3.0.1`
import java.time.format.DateTimeFormatter
import scala.language.experimental.macros
import contextual._
object dateTimeFormat {
object DateTimeFormatInterpolator extends Interpolator {
@danslapman
danslapman / ManagedSourcesCachePlugin.scala
Created June 23, 2021 07:12
ManagedSourcesCachePlugin
package sbt
import sbt.Keys._
import sbt.internal.inc.HashUtil
import sbt.internal.remotecache.CustomRemoteCacheArtifact
import sbt.nio.FileStamp
import sbt.nio.file.FileAttributes
import sbt.plugins.JvmPlugin
import sbt.util.InterfaceUtil.toOption
@danslapman
danslapman / .editorconfig
Last active September 19, 2022 15:16
My .editorconfig
#https://github.com/fsprojects/fantomas/blob/master/docs/Documentation.md
[*.{fs,fsx}]
indent_size=4
max_line_length=120
fsharp_max_value_binding_width=120
fsharp_max_function_binding_width=120
fsharp_multiline_block_brackets_on_same_column=true
fsharp_multi_line_lambda_closing_newline=true
fsharp_multiline_block_brackets_on_same_column = true
fsharp_experimental_stroustrup_style = true