Skip to content

Instantly share code, notes, and snippets.

View Fristi's full-sized avatar

Mark Fristi

View GitHub Profile
@Fristi
Fristi / Data.scala
Last active July 26, 2017 09:21
Types are data and data are types
package data
import cats.Cartesian
import cats.data.{NonEmptyList, State, StateT, Validated}
import cats.functor.Invariant
import cats.implicits._
import eu.timepit.refined._
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.boolean._
import eu.timepit.refined.collection._
@Fristi
Fristi / PartitionConsumer.scala
Last active November 23, 2016 11:36
Partition parallel consumption
import monix.eval.Callback
import monix.execution.atomic.AtomicBoolean
import monix.execution.cancelables.{AssignableCancelable, RefCountCancelable}
import monix.execution.{Ack, Cancelable, Scheduler}
import monix.reactive._
import monix.reactive.exceptions.CompositeException
import monix.reactive.observers.Subscriber
import monix.reactive.subjects.PublishSubject
import scala.collection.mutable
@Fristi
Fristi / docker-compose.yml
Last active September 20, 2016 19:40
Working Confluent platform on Mac OSX
---
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:3.0.1
environment:
ZOOKEEPER_CLIENT_PORT: 32181
ZOOKEEPER_TICK_TIME: 2000
kafka:
@Fristi
Fristi / VersionedJson.scala
Created September 12, 2016 14:47
Versioning of json. Blob with version + json payload. The scodec codec will read the version and apply JSON AST transformations to converge to the latest JSON AST.
import cats.kernel.Semigroup
import fommil.sjs.FamilyFormats._
import scodec._
import scodec.bits._
import scodec.codecs._
import shapeless._
import shapeless.ops.nat.ToInt
import spray.json._
import spray.json.lenses.JsonLenses._
@Fristi
Fristi / Tagless.scala
Last active August 31, 2016 07:38
Introspectable Json algebra. Allows you to derive codecs and documentation. Category + Monoid lifted into the algebra GADT. Composing algebra's is possible via the heterogenous polymorphic tail. Port of JsonGrammar2 by Martijn van Steenbergen en Sjoerd Visscher
package net.vectos
import cats.arrow.Category
import io.circe.Json
import shapeless._
import scala.language.higherKinds
object algebra {
sealed trait JsonGrammarF[F[-_,+_]] extends Category[F] {
@Fristi
Fristi / PartialIso.scala
Last active June 18, 2016 17:33
Partial Iso's in Scala
package net.vectos
import cats._
import cats.syntax.all._
import cats.arrow.Category
import cats.functor.Invariant
import io.circe._
import io.circe.syntax._
import io.circe.generic.auto._
import shapeless._
@Fristi
Fristi / MachineLearningWeek2.scala
Created April 17, 2016 09:43
Exercises of Coursera Machine Learning week 2 in Scala
package nl.mdj.ml
import java.io.File
import breeze.linalg._
import breeze.numerics._
/**
* Exercises of Coursera Machine Learning week 2 in Scala
*/
@Fristi
Fristi / CombineK.scala
Last active February 25, 2016 14:55
Combining multiple * -> * kinded type constructors which have a `Apply` available to create one container with a HList inside it. To map this F[HList] we can use IsoK which transforms the HList to a product type. Useful for various cases, like options,decoders, etc. Alternative to cats' CartesianBuilder. CartesianBuilder has 22 member limit.
package codecgen
import cats._
import cats.std.option._
import cats.syntax.all._
import shapeless._
import shapeless.ops.hlist.Prepend
import scala.annotation.implicitNotFound
import scala.language.higherKinds
@Fristi
Fristi / Weeks.scala
Created February 23, 2016 15:58
Get week intervals for a specified period
import org.joda.time.{Interval, Period, DateTime}
object Main {
def getWeeks(startDate: DateTime, endDate: DateTime) = {
val weekPeriod = new Period().withWeeks(1)
def loop(current: Interval, weeks: Seq[Interval]): Seq[Interval] = {
if(current.getEnd.isBefore(endDate)) {
loop(new Interval(current.getStart.plus(weekPeriod), weekPeriod), weeks :+ current)
} else {
@Fristi
Fristi / Decoder.scala
Last active February 23, 2016 15:15
Functional BitReader for reading one byte at the time and reading bits from right to left. This format is used by Blizzard to encode data in replay files for games like StarCraft 2 + Heroes of the Storm. See Go source code; https://github.com/martine/goblizzard/blob/master/src/blizzard/replay/bitreader_test.go
import cats.{Functor, Cartesian}
import cats.functor.Invariant
import cats.syntax.all._
import cats.data.Xor
import scodec.bits._
final case class BitBuffer(source: BitVector, buffer: BitVector) {
def size = buffer.size + source.size
def consume(n: Long): Xor[String, DecodeResult[BitVector]] = {