Skip to content

Instantly share code, notes, and snippets.

View sderosiaux's full-sized avatar
💭
Need help?

Stéphane Derosiaux sderosiaux

💭
Need help?
View GitHub Profile
@sderosiaux
sderosiaux / deploy.dhall
Created July 25, 2019 18:00
Dhall with Kubernetes
let types = /private/tmp/dhall-kubernetes/types.dhall
let defaults = /private/tmp/dhall-kubernetes/defaults.dhall
let deployment: types.Deployment =
defaults.Deployment
//{
metadata = defaults.ObjectMeta //{ name = "nginx" },
spec = Some (
defaults.DeploymentSpec
//{
object Tree {
sealed trait Tree[A]
case class Leaf[A](a: A) extends Tree[A]
case class Node[A](left: Tree[A], right: Tree[A]) extends Tree[A]
def translate[A, B, C, F[_], E](t: Tree[A], check: (A, B) => Boolean, newState: B => B, endState: (A, B) => C, err: A => E)(implicit F: MonadError[F, E]): StateT[F, B, Tree[C]] = t match {
case Leaf(a) => StateT(s => if (check(a, s)) F.raiseError(err(a)) else F.pure((newState(s), Leaf(endState(a, s)))))
case Node(left, right) =>
implicit val A = Apply[StateT[F, B, ?]]
(translate(left, check, newState, endState, err), translate(right, check, newState, endState, err)).mapN(Node.apply)
@sderosiaux
sderosiaux / forLoop.scala
Last active April 16, 2018 10:23
A stack-safe recursive classic synchrone for-loop?
def forLoop[A, C](
from: A,
continue: A => Boolean,
next: A => A,
compute: (C, A) => C,
acc: C): C = {
@tailrec
def go(from: A, acc: C): C = {
if (continue(from))
go(next(from), compute(acc, from))
@sderosiaux
sderosiaux / balance.scala
Created February 20, 2018 23:52
Parenthesis balancing using Monoid
// From haskell "Monoidal Parsing—Edward Kmett"
import cats.implicits._
final case class Balance(l: Int, r: Int)
object Balance {
val EMPTY = Balance(0, 0)
val LEFT = Balance(0, 1)
val RIGHT = Balance(1, 0)
}
@sderosiaux
sderosiaux / infix.scala
Created December 14, 2017 23:53
How to make infix stuff in Scala
trait Toto[F[_]] {
def hello[A, B](fa: F[A])(f: A => B): F[B]
}
implicit val Fun = new Toto[List] {
override def hello[A, B](fa: List[A])(f: A => B): List[B] = fa.map(f)
}
implicit class FunImplicits[F[_], A](t: F[A]) {
def !!!![B](f: A => B)(implicit toto: Toto[F]): F[B] = toto.hello(t)(f)
@sderosiaux
sderosiaux / trampoline.scala
Created November 28, 2017 22:40
Trampoline using Cats
def value(i: List[Int]): Trampoline[List[Int]] = i match {
case Nil => Free.pure(Nil)
case head :: tail => Free.defer(value(tail)).flatMap { l => Free.pure(head + 1 :: l) }
}
import cats.implicits._
println(value((1 to 10).toList).run) // List(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
@sderosiaux
sderosiaux / google_ips.sh
Created November 8, 2017 13:11
Google Cloud IPs
for i in 1 2 3 4 5; do nslookup -q=TXT "_cloud-netblocks$i.googleusercontent.com" 8.8.8.8 | tr ' ' '\n' | grep ip4 | cut -d':' -f2 ; done
@sderosiaux
sderosiaux / In.scala
Created July 31, 2017 22:45
Kafka streams
val builder = new KStreamBuilder
val textLines = builder.stream[String, String]("TextLinesTopic")
val pattern = Pattern.compile("\\W+")
val wordCounts: KTable[String, String] = textLines
.flatMapValues(line => pattern.split(line.toLowerCase).toIterable.asJava)
.groupBy { case (_, word: String) => word }
.count("Counts")
.mapValues(_.toString)
.to(Serdes.String, Serdes.String, "WordsWithCountsTopic")
@sderosiaux
sderosiaux / InValueTimestampExtractor.java
Created July 19, 2017 09:19 — forked from nfo/InValueTimestampExtractor.java
Kafka Streams - Custom timestamp extractor, from a `long` field named "timestamp"
/**
* Handle records with a timestamp in their Avro value.
* Expects a LONG field named "timestamp".
* Any problem makes this extractor return the record's internal timestamp.
*/
public class InValueTimestampExtractor implements TimestampExtractor {
@Override
public long extract(ConsumerRecord<Object, Object> record) {
if (record != null && record.value() != null) {
@sderosiaux
sderosiaux / SimpleMacro.scala
Created June 5, 2017 08:45
Simple Scala Macro
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
class LoggerImpl(val c: Context) {
import c.universe._
def getClassSymbol(s: Symbol): Symbol = if (s.isClass) s else getClassSymbol(s.owner)
def logImpl(msg: Expr[String]): Expr[Unit] = {
val cl = getClassSymbol(c.internal.enclosingOwner).toString
val time = c.Expr[String](q"new java.util.Date().toString")