Skip to content

Instantly share code, notes, and snippets.

View vsuharnikov's full-sized avatar

Vyatcheslav Suharnikov vsuharnikov

View GitHub Profile
@vsuharnikov
vsuharnikov / ExtendedHostConfig.scala
Created November 5, 2017 16:57
How to use a non-existent setting in Docker Spotify (sysctl example)
import com.google.common.collect.ImmutableMap
import com.spotify.docker.client.messages.HostConfig
import com.spotify.docker.client.shaded.com.fasterxml.jackson.annotation.JsonProperty
class ExtendedHostConfig(h: HostConfig) extends HostConfig {
@JsonProperty("Sysctls")
def sysctls(): ImmutableMap[String, String] = ImmutableMap.of(
"net.ipv4.tcp_keepalive_time", "15",
"net.ipv4.tcp_keepalive_intvl", "5",
"net.ipv4.tcp_keepalive_probes", "3"
@vsuharnikov
vsuharnikov / Main.scala
Created September 15, 2017 10:10
pureconfig: a case class of a sealed trait by the boolean value
import com.typesafe.config.ConfigObject
import pureconfig._
import pureconfig.error.{CannotParse, ConfigReaderFailures}
sealed trait Enigma
object Enigma {
case class Foo(value: Int) extends Enigma
case object Bar extends Enigma
}
@vsuharnikov
vsuharnikov / Main.scala
Created September 14, 2017 11:08
Serialization and deserialization example
import com.google.common.primitives.Ints
import scala.collection.breakOut
trait Serializable[From, To] {
def serialize(x: From): To
}
trait Deserializable[From, To] {
def deserialize(x: From): To
}
@vsuharnikov
vsuharnikov / Test.scala
Created July 1, 2017 08:37
Partially applied macros
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
object Test {
def foo1[A, B]: Unit = macro impl[A, B]
def foo2[A]: Unit = macro impl[A, Option[Int]]
def impl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: blackbox.Context): c.Expr[Unit] = {
import c.universe._
c.echo(c.enclosingPosition, s"A=${weakTypeOf[A]}, B=${weakTypeOf[B]}")
@vsuharnikov
vsuharnikov / Expr.scala
Created May 30, 2017 03:48
Finally tagless example
trait Lit[T] {
def lit(x: Int): T
}
trait Add[T] {
def add(x: T, y: T): T
}
trait Mul[T] {
def mul(x: T, y: T): T
trait BinaryOperation[T] {
def apply(a: T, b: T): T
}
trait AssociativeBinaryOperation[T] extends BinaryOperation[T]
trait Reduce[F[_]] {
def apply[T](xs: F[T])(op: BinaryOperation[T]): T
}

Scala for comprehension translation helper

"For comprehension" is a another syntax to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

Try it yourself!