Skip to content

Instantly share code, notes, and snippets.

View vsuharnikov's full-sized avatar

Vyatcheslav Suharnikov vsuharnikov

View GitHub Profile

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!

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
}
@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
@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 / 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 / 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 / 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 / logback.xml
Created December 28, 2017 14:54
Size and time rolled logs
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<property name="default.pattern" value="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%.15thread] %logger{26} - %msg%n"/>
<property name="logback.file.final-directory" value="${logback.file.directory:-${waves.directory}/log}"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
@vsuharnikov
vsuharnikov / README.md
Created April 4, 2018 20:46
Удалям данные в ВК

Удаляем посты на стене

$$('.ui_actions_menu_item')
  .filter(function (x) {
    var attr = x.getAttributeNode('onclick');
    if (attr == null) return false;
    return attr.value.indexOf('delete') > 0;
  })
 .forEach(function (x) {
@vsuharnikov
vsuharnikov / Main.scala
Created April 7, 2019 08:24
cats - foldLeftM - short circuit
val xs: List[Int] = List(1, 1, 2, 1, 1)
val result = xs.foldLeftM(0) {
case (r, x) =>
println(s"r=$r, x=$x")
if (x % 2 == 0) Left("even")
else Right(r + x)
}
println(result)
/*