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 / KafkaUtils.scala
Created December 14, 2016 02:11
How to use Kafka in tests (+ schemarepo)
import java.nio.file.Files
import kafka.admin.AdminUtils
import kafka.server.{KafkaConfig, KafkaServerStartable}
import kafka.utils.ZkUtils
import org.apache.curator.test.TestingServer
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.schemarepo.{InMemoryRepository, SubjectConfig, ValidatorFactory}
import org.springframework.util.SocketUtils
import collection.JavaConversions._
@sderosiaux
sderosiaux / ignore_exitVM.scala
Created December 14, 2016 01:42
Handle exitVM event of the JVM and just ignore it
System.setSecurityManager(new SecurityManager() {
override def checkPermission(perm: Permission): Unit = {
if (perm.getName.startsWith("exitVM")) {
throw new SecurityException("System.exit is disabled")
}
}
override def checkPermission(perm: Permission, context: scala.Any): Unit = {
checkPermission(perm)
@sderosiaux
sderosiaux / git-of-theseus.md
Last active December 7, 2016 23:56
git-of-theseus install on Windows with ubuntu bash

Because there are so much crap to install that you discover one by one thanks to SO, here it is:

sudo apt-get build-dep python-matplotlib
sudo apt-get install python-pip libpng-dev libfreetype6-dev pkg-config python-dev libblas-dev liblapack-dev libatlas-base-dev gfortran scipy python-tk 
sudo pip install smmap git

Then you got this error, and you must edit this file:

_tkinter.TclError: no display name and no $DISPLAY environment variable

Keybase proof

I hereby claim:

  • I am sderosiaux on github.
  • I am chtefi (https://keybase.io/chtefi) on keybase.
  • I have a public key whose fingerprint is F6D8 1D9C D7E8 0CD7 1DB0 34B0 51D3 1B66 50EB 283A

To claim this, I am signing this object:

@sderosiaux
sderosiaux / AkkaFSM.scala
Last active December 4, 2016 09:14
Taming Awful Legacy Data Feeds Using Akka (FSM)
// From https://engineering.footballradar.com/taming-awful-legacy-data-feeds-using-akka/
sealed trait AcmeMessage
sealed trait Incoming extends AcmeMessage
sealed trait Outgoing extends AcmeMessage
case class Login(username: String, password: String) extends Incoming
case object LoginSuccess extends Outgoing
case object LoginFailure extends Outgoing
case class Logout(username: String) extends Incoming
function hyloWith(cata, ana) {
return function hylo (value) {
let { next: n, element:acc, done } = ana(value);
let { next, element } = ana(n); // not a monoid here, it could (no "zero" value)
while (!done) {
acc = cata(acc, element);
({ next, element, done } = ana(next));
}
return acc
@sderosiaux
sderosiaux / Variances.java
Last active November 20, 2016 23:01
Covariance and contravariance in Java/Scala
static class GrandPa { void a(){} }
static class Parent extends GrandPa { void b(){} }
static class Child extends Parent { void c(){} }
// we cannot push into a covariant type: we don't know the underlying type.
// we know it's a least a Parent, but which type? the caller could treat it as a List<Child> and call c() on the items
// we can only read from it and be sure it's a Parent
private static void co(List<? extends Parent> list) {
// list.add(new GrandPa()); // no, we don't know the underlying type
// list.add(new Parent()); // no, we don't know the underlying type
@sderosiaux
sderosiaux / pingServers.scala
Last active November 18, 2016 21:30
A tentative to reproduce some JS async/await code in Scala
// reference: https://hackernoon.com/an-ode-to-async-await-7da2dd3c2056
val servers = List("http://www.sevengramscaffe.com", "http://www.hernanparra.co", "http://www.theimeandyou.com", "http://www.luchoycarmelo.com")
Await.result(async {
val result = await { pingServers(servers) }
result.foreach { case (url, count) => println(s"$url failed $count time(s)") }
}, 1 minute)
def pingServers(servers: List[String]) = Future.sequence(servers.map(pingOneServer)).map(_.filter(_._2 > 0).toMap)
def pingOneServer(url: String) = Future { (url, pingAttempts(url, 3)) }
@sderosiaux
sderosiaux / index.js
Created November 5, 2016 17:06
Rx - switchmap
var Rx = require('rxjs/Rx');
const o = (name, start, interval) => Rx.Observable.create(function(observer) {
console.log("creating " + name + " (starts at " + start + ") with interval " + interval + "ms")
setInterval(() => observer.next({ name: name + " (" + interval + "ms)", start: start++ }), interval)
});
let start = new Date().getTime();
o("origin", 0, 500)
@sderosiaux
sderosiaux / LeaderElection.java
Last active October 23, 2016 00:13
Curator (Zookeeper)
curator = CuratorFrameworkFactory.newClient(ZOOKEEPER_SERVER,
2000, 10000, new ExponentialBackoffRetry(1000, 3));
curator.start();
curator.blockUntilConnected();
leaderSelector = new LeaderSelector(curator, ELECTION_PATH, this);
leaderSelector.autoRequeue();
leaderSelector.start();