Skip to content

Instantly share code, notes, and snippets.

View quelgar's full-sized avatar
🇦🇺

Lachlan O'Dea quelgar

🇦🇺
View GitHub Profile
@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 / jsmap.hs
Last active February 22, 2017 14:54
Is Javascript's array map function basically a comonadic cobind (=>> in Haskell)? Have a look as the jsmap function below.
-- Inspired by http://nedbatchelder.com/blog/201301/stupid_languages.html
-- which describes how the Javascript array.map function seems weird
-- and also http://blog.sigfpe.com/2008/03/comonadic-arrays.html
-- which describes the comonad for arrays
import Data.Array
import Data.Char
class Functor w => Comonad w where
(=>>) :: w a -> (w a -> b) -> w b
@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 / 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 / Framing.scala
Last active March 15, 2019 02:55
A sink to frame a ZIO stream by delimited boundaries. (eg lines of text)
import scalaz.zio.console._
import scalaz.zio.stream._
import scalaz.zio.{Chunk, ZIO}
object Framing {
def lfDelimiter = Chunk('\n')
@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}