Skip to content

Instantly share code, notes, and snippets.

View quelgar's full-sized avatar
🇦🇺

Lachlan O'Dea quelgar

🇦🇺
View GitHub Profile
@quelgar
quelgar / tagged.scala
Created July 16, 2015 06:27
Tagged types experiment
bject Tag {
type Tagged[T] = {
type Tag = T
}
type @@[A, T] = A with Tagged[T]
@quelgar
quelgar / exceptiontoeither.scala
Last active August 29, 2015 14:25
Generically convert thrown exceptions to an Either
/**
* Catch exceptions to create an `Either` value.
*
* Useful when you want to deal an API that reports failure conditions using
* exceptions. This method allows catching exceptions and converting them to
* `Left` values.
*
* An advantage vs using `scala.util.Try` is that you have full control over
* which exceptions are caught.
*
@quelgar
quelgar / Udp.scala
Last active April 15, 2016 12:57
Basic Akka Streams Source for receiving UDP datagrams
package test
import java.net.InetSocketAddress
import akka.actor.{ActorLogging, ActorRef, Props}
import akka.io
import akka.stream.ActorMaterializer
import akka.stream.actor.ActorPublisher
import akka.stream.scaladsl.Source
@quelgar
quelgar / CacheStage.scala
Last active February 10, 2019 01:16
**NOTE**: I think this turned out to be pretty buggy. A custom Akka graph stage that caches the latest value for a period of time.
import java.time.{Clock, Instant}
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.{Attributes, FlowShape, Inlet, Outlet, _}
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler, _}
import scaladsl.{Flow, _}
import scala.concurrent.Await
import scala.concurrent.duration._
@quelgar
quelgar / GADT.scala
Created March 10, 2017 01:32
Basic GADT in Scala
// Scala version of code from https://en.wikipedia.org/wiki/Generalized_algebraic_data_type
sealed abstract class Expr[A]
final case class EBool(a: Boolean) extends Expr[Boolean]
final case class EInt(a: Int) extends Expr[Int]
import monix.reactive._
import monix.execution._
import Ack.{Stop, Continue}
import scala.util.control.NonFatal
import scala.concurrent._
import scala.concurrent.duration._
def feed[A](in: Iterator[A], out: Observer[A])
(implicit s: Scheduler): Future[Ack] = {
@quelgar
quelgar / MonixNioTest.scala
Last active August 22, 2017 00:39
Testing accepting TCP connections with Monix NIO.
package monix.rsocket.tcp
import java.nio.charset.StandardCharsets
import monix.eval.{Callback, Task}
import monix.reactive.Observable
import monix.nio.tcp._
import monix.execution.Scheduler.Implicits.global
@quelgar
quelgar / Framing.scala
Last active January 19, 2018 00:11
Monix Pipe for byte stream framing
import java.nio.ByteBuffer
import monix.nio.text.UTF8Codec._
import monix.nio.file
import monix.reactive._
import monix.reactive.observers.Subscriber
import monix.execution._
import monix.execution.exceptions
import monix.execution.atomic.Atomic
import monix.reactive.subjects.Subject
@quelgar
quelgar / ConnectableTest.scala
Last active June 25, 2018 01:55
Testing reliably subscribing to a Monix ConnectableObservable before connecting.
package au.com.str.stellar
import cats.implicits._
import monix.eval.{Task, TaskApp}
import monix.reactive.Observable
import scala.concurrent.duration._
object Test extends TaskApp {
@quelgar
quelgar / ZioInputStreamTest.scala
Last active October 27, 2019 14:44
Inputstream to ZIO Stream of byte chunks.
import java.io.InputStream
import java.nio.file.{Files, Path, Paths}
import scalaz.zio.console.putStrLn
import scalaz.zio.duration._
import scalaz.zio.stream.Stream
import scalaz.zio.stream.Stream.Fold
import scalaz.zio.{App, Chunk, IO, Managed}