Skip to content

Instantly share code, notes, and snippets.

@danslapman
danslapman / JSR310.scala
Created June 20, 2018 11:02
JSR310 custom formatters
import java.time._
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.temporal.{ChronoField, TemporalAccessor}
import java.{time => jt}
import scala.util.Try
trait ZonedParser {
protected val formatter: DateTimeFormatter
@danslapman
danslapman / conf.sc
Created May 16, 2018 10:16
Parallel lazy future with cats
interp.configureCompiler(_.settings.YpartialUnification.value = true)
@danslapman
danslapman / ex.sc
Last active October 17, 2019 13:42
Add resolver and compiler plugin to ammonite REPL
import coursierapi.MavenRepository
interp.repositories.update(interp.repositories() ::: List(MavenRepository.of(
"http://dl.bintray.com/danslapman/maven"
))
@
import $ivy.`danslapman::shapeless-circe:1.0`
import $plugin.$ivy.`org.spire-math::kind-projector:0.9.7`
@danslapman
danslapman / combined.sc
Last active September 26, 2018 06:34
Combine two partial functions narrowing domain
import scala.util.{Success, Try}
val pf1: PartialFunction[Try[String], String] = {
case Success(s) => s
}
val pf2: PartialFunction[String, String] = {
case s if s.contains("pass") => s
}
@danslapman
danslapman / build.sbt
Created December 29, 2017 11:48
Copy classes from alternative target
copyProbeClasses := {
val log = streams.value.log
val probeTarget = new File(crossTarget.value, "probe-classes")
(probeTarget ** "*" filter(_.isFile)).get.foreach { fl =>
log.info("Copying " + fl.relativeTo(probeTarget).get.toString)
Files.copy(fl.toPath, new File((classDirectory in Compile).value, fl.relativeTo(probeTarget).get.toString).toPath, REPLACE_EXISTING)
}
}
@danslapman
danslapman / build.sbt
Created December 28, 2017 14:40
sbt config scope
name := "sbt-scopes"
version := "0.1"
scalaVersion := "2.12.4"
lazy val Probe = config("probe").intransitive
.extend(Compile)
.describedAs("Probe configuration")
@danslapman
danslapman / example.scala
Created November 10, 2017 09:02
Custom atom
package ru.tinkoff.testApi
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import io.circe.generic.JsonCodec
import ru.tinkoff.tschema.akkaHttp.{MkRoute, Serve}
import ru.tinkoff.tschema.swagger.SwaggerTypeable
import ru.tinkoff.tschema.typeDSL.DSLAtom
import shapeless.{HList, Witness}
@danslapman
danslapman / LazyFuture.scala
Created October 25, 2017 18:49
Lazy future
type LazyFuture[T] = ReaderT[Future, ExecutionContext, T]
object LazyFuture {
def apply[T](value: => T): LazyFuture[T] = ReaderT.apply { implicit ExecutionContext =>
Future(value)
}
}
@danslapman
danslapman / exc.py
Created April 7, 2017 13:34
Print exception with line number
import linecache
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
@danslapman
danslapman / NumericPow.scala
Last active February 21, 2018 14:30
Generic power operator
implicit class PowerOp[T <: AnyVal](val value: T) extends AnyVal {
import Numeric.Implicits._
import scala.{math => scmath}
@inline def **(power: T)(implicit numeric: Numeric[T]): Double =
scmath.pow(value.toDouble(), power.toDouble())
}