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
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) => {
prev.flatMap({
case Left(err) => IO(Left(err))
case Right(_) => next
})
})
@kdrakon
kdrakon / Rusts_Bindgen_+_Fuse_in_2019.md
Last active Mar 13, 2019
Demonstrates how I got bindgen to generate the bindings to libfuse
View Rusts_Bindgen_+_Fuse_in_2019.md

Rust's Bindgen + Fuse in 2019

I will quickly show how I got bindgen (https://rust-lang.github.io/rust-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
kdrakon / Avro4s.scala
Last active Aug 5, 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] {
View how-to-sequence-in-rust.md

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
kdrakon / paged_vec.rs
Created Jun 5, 2018
A paginated Vec<A>: PagedVec
View paged_vec.rs
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
kdrakon / RustProblem-ConflictingTraitUse.md
Last active May 17, 2018
Rust: How to use more than one Trait implementation while avoiding conflicts
View RustProblem-ConflictingTraitUse.md

Problem

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
kdrakon / JsonSerdeForAvroClasses.java
Created Dec 21, 2017
A Kafka Serde for turning Avro-generated classes into JSON objects (only does serialization)
View JsonSerdeForAvroClasses.java
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
kdrakon / SerdeFunction.java
Created Dec 21, 2017
A Kafka Serde Java 8 Function
View SerdeFunction.java
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
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 akka.actor.{ActorSystem, Props}
import akka.routing.ConsistentHash
import akka.stream.actor._
import akka.stream.scaladsl.{Flow, GraphDSL, RunnableGraph, Sink, Source}
import akka.stream.{ActorMaterializer, ClosedShape, ThrottleMode}
import com.kifi.franz.{MessageId, SQSMessage}
View keybase.md

Keybase proof

I hereby claim:

  • I am kdrakon on github.
  • I am kdrakon (https://keybase.io/kdrakon) on keybase.
  • I have a public key whose fingerprint is 3696 F8F1 D728 6671 EBA2 97B8 5879 13B5 C5C6 51B7

To claim this, I am signing this object:

You can’t perform that action at this time.