Skip to content

Instantly share code, notes, and snippets.

View notxcain's full-sized avatar
🏠
Working from home

Denis Mikhaylov notxcain

🏠
Working from home
View GitHub Profile
@notxcain
notxcain / ActionRT.scala
Created September 21, 2018 08:15
ActionRT
package aecor.data
import aecor.data.ActionT.{ActionFailure, ActionResult}
import cats.data._
import cats.implicits._
import cats.{Applicative, Functor, Monad, ~>}
final class ActionT[F[_], S, E, R, A] private (
val unsafeRun: (S, (S, E) => Folded[S], Chain[E]) => F[ActionResult[R, E, A]]
) extends AnyVal {
import cats.implicits._
import cats.{ Applicative, Monad }
// Операции над контекстом
trait ContextWriter[F[_]] {
def put(key: String, value: String): F[Unit]
}
// Tagless Encoding
trait Counter[F[_]] {
def increment(value: Int): F[Unit]
def decrement(value: Int): F[Unit]
def value: F[Int]
}
// Free Encoding
@notxcain
notxcain / Logger.scala
Created June 7, 2018 14:12
Logger[F[_]]
import cats.Applicative
import cats.effect.Sync
import com.evotor.common.logging.Logger.Level
import org.slf4j.LoggerFactory
import scala.reflect.ClassTag
trait Logger[F[_]] {
def log(level: Level.Value, msg: => String, throwable: Throwable): F[Unit]
@notxcain
notxcain / Observable.scala
Created May 14, 2018 11:46
Effect Polymorphic Push-based Observable
import cats.data.EitherT
import cats.effect.Timer
import cats.implicits._
import cats.{ApplicativeError, Foldable, Functor, Monad, MonadError}
import scala.concurrent.duration.FiniteDuration
trait Observable[F[_], A] { outer =>
def subscribe[O](observer: Observer[F, A, O]): F[O]
final def foldLeft[O](o: O)(f: (O, A) => O)(
@notxcain
notxcain / KafkaStream.scala
Last active May 11, 2018 11:45
Fs2 Kafka Consumer
import java.util
import java.util.{Collections, UUID}
import cats.effect.{Async, Timer}
import io.evotor.webhooks.common.Committable
import fs2._
import org.apache.kafka.clients.consumer._
import org.apache.kafka.common.TopicPartition
import org.apache.kafka.common.serialization.Deserializer
import scala.concurrent.blocking
@notxcain
notxcain / Root.scala
Created March 30, 2018 07:29
Opaque Sealed Hierarchy
// Tired of subtyping and `extends Product with Serializable`?
// It is impossible to instantiate a concrete subtype of `Root`
// It preserves pattern matching exhaustiveness check
sealed abstract class Root
object Root {
private final case class LeafInt(i: Int) extends Root
object LeafInt { def apply(i: Int): Root = new LeafInt(i) }
private final case class LeafString(s: String) extends Root
object LeafString { def apply(s: String): Root = new LeafString(s) }
@notxcain
notxcain / Cache.scala
Created May 3, 2017 15:26
Monix function cache with TTL
object Cache {
def apply[A, B](ttl: FiniteDuration)(f: A => Task[B]): A => Task[B] = {
val mvar = MVar(Map.empty[A, B])
a =>
for {
cached <- mvar.read.map(_.get(a))
out <- cached match {
case Some(b) => Task.pure(b)
case None =>
for {
@notxcain
notxcain / kill-pods.sh
Last active December 6, 2017 08:28
Continuously kill all pods except one randomly chosen.
#!/bin/bash
app=$1
chosen_one=$(kubectl get pods -l app=$app | tail -1 | awk '{print $1}')
echo "Keeping alive $chosen_one"
while true; do
pods=$(kubectl get pods -l app=$app | tail -n +2 | awk '{print $1}')
for pod in $pods; do
#!/bin/bash
working_dir="$( cd "$1" && pwd )"
chart_name=$(basename $(pwd))
unamestr=$(uname)
if [[ "$unamestr" == 'Darwin' ]]; then
sha='shasum'
elif [[ "$unamestr" == 'Linux' ]]; then
sha='sha1sum'