Skip to content

Instantly share code, notes, and snippets.

Sean Policarpio kdrakon

Block or report user

Report or block kdrakon

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
kdrakon / GenerateSwaggerJson.scala
Created Nov 7, 2019
A helper to write the JSON OpenAPI spec from tapir (
View GenerateSwaggerJson.scala
package io.policarp
import{BufferedWriter, FileWriter}
import java.nio.file.Paths
import cats.implicits._
import cats.kernel.Semigroup
import io.circe.Printer
import io.circe.syntax._
kdrakon / IOShortCircuit.scala
Created May 9, 2019
Short-circuit a list of Cats Effect (IO) using a foldLeft
View IOShortCircuit.scala
object Main extends App {
val l = List(IO(println(1).asRight), IO(println(2).asRight), IO("foo".asLeft), IO(println(4).asRight))
val io = l.foldLeft[IO[Either[String, Unit]]](IO(Right(())))((prev, next) => {
case Left(err) => IO(Left(err))
case Right(_) => next
kdrakon /
Last active Mar 13, 2019
Demonstrates how I got bindgen to generate the bindings to libfuse

Rust's Bindgen + Fuse in 2019

I will quickly show how I got bindgen ( to generate the bindings to Fuse (libfuse) with the current stable release of Rust. By doing so, this should demonstrate how to bootstrap writing your own Fuse file system in Rust.

I do realise that there are some crates that already exist that aid in making Fuse drivers in Rust, but this was more or less an excuse to also try out bindgen, which I don't believe those existing libraries utilise.

I will be using:

kdrakon / Avro4s.scala
Last active Nov 18, 2019
Using Avro4s with Confluent Kafka Avro Serializer + Schema Registry
View Avro4s.scala
import java.util
import com.sksamuel.avro4s.RecordFormat
import org.apache.avro.generic.GenericRecord
import org.apache.kafka.common.serialization.{Deserializer, Serde, Serializer}
object Avro4s {
implicit class CaseClassSerde(inner: Serde[GenericRecord]) {
def forCaseClass[T](implicit recordFormat: RecordFormat[T]): Serde[T] = {
val caseClassSerializer: Serializer[T] = new Serializer[T] {

How to sequence in Rust

Well, I wouldn't say it's exactly the same sequencing in terms of FP languages like Scala et al., but it does the job for the given collection types in Rust; invert something of type F<G<A>> into G<F<A>>.

Usually, we'd expect F and G as types that are iterable and applicative. In the case of Rust, what I seem to understand from the standard library is that it is expected that there exists a trait implementation—FromIterator—that should allow G to be treated like an applicative. Since collect() is already iterating over a collection of G<A>, it's straightforward for an implementation of FromIterator for F to turn the results (i.e. a collection of them) of the first FromIterator into a G<F<A>>—which is what it does.

let opt = vec![Some(1), Some(2), Some(3)];
let sequenced = opt.into_iter().collect::<Option<Vec<i32>>>();
print!("{:?}", sequenced); // Some([1, 2, 3])
kdrakon /
Created Jun 5, 2018
A paginated Vec<A>: PagedVec
pub struct PagedVec<'a, A: 'a> {
indexes: usize,
page_length: usize,
pages: Vec<Vec<&'a A>>,
impl<'a, A> PagedVec<'a, A> {
pub fn from(vec: &'a Vec<A>, page_length: usize) -> PagedVec<'a, A> {
PagedVec {
indexes: vec.len(),
kdrakon /
Last active May 17, 2018
Rust: How to use more than one Trait implementation while avoiding conflicts


Say you initially have code like this:

struct DeserializeError(String);
trait Deserialize<T> {
    fn into_type(self) -> Result<T, DeserializeError>

struct Foo {}
impl Deserialize<Foo> for Vec<u8> {
kdrakon /
Created Dec 21, 2017
A Kafka Serde for turning Avro-generated classes into JSON objects (only does serialization)
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.kafka.streams.kstream.ValueMapper;
import java.nio.charset.Charset;
public class JsonSerdeForAvroClasses<T> {
public static <T> JsonSerdeForAvroClasses<T> gen() {
kdrakon /
Created Dec 21, 2017
A Kafka Serde Java 8 Function
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import java.util.Map;
import java.util.function.Function;
public class SerdeFunction<T> implements Serde<T> {
private final Function<T, byte[]> serializer;
kdrakon / ConsistentHashGroupedStreams.scala
Created Sep 6, 2016
Example of Consistent Hashing for Akka groupBy Streams
View ConsistentHashGroupedStreams.scala
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{Executors, TimeUnit}
import{ActorSystem, Props}
import akka.routing.ConsistentHash
import{Flow, GraphDSL, RunnableGraph, Sink, Source}
import{ActorMaterializer, ClosedShape, ThrottleMode}
import com.kifi.franz.{MessageId, SQSMessage}
You can’t perform that action at this time.