Skip to content

Instantly share code, notes, and snippets.

View ChristopherDavenport's full-sized avatar

Christopher Davenport ChristopherDavenport

View GitHub Profile
@ChristopherDavenport
ChristopherDavenport / generic-server.catscript
Created April 28, 2021 22:57
Generic Json Http Server with Catscript
#!/usr/bin/env catscript
// dependency: "org.http4s" %% "http4s-ember-server" % "1.0.0-M21"
// dependency: "org.http4s" %% "http4s-circe" % "1.0.0-M21"
// dependency: "org.http4s" %% "http4s-dsl" % "1.0.0-M21"
// dependency: "ch.qos.logback" % "logback-classic" % "1.2.3"
import org.http4s._
import org.http4s.circe._
import org.http4s.circe.middleware._
import org.http4s.dsl.io._
@ChristopherDavenport
ChristopherDavenport / catscript.json
Last active April 25, 2021 20:04
Catscript Channel
{
"catscript": {
"repositories": [
"central"
],
"dependencies": [
"io.chrisdavenport::catscript:latest.release"
]
}
}
@ChristopherDavenport
ChristopherDavenport / BetterWay.sh
Last active April 23, 2021 02:12
Scala Scripting
#!/usr/bin/env sbt -Dsbt.version=1.5.0 -Dsbt.main.class=sbt.ScriptMain -Dsbt.supershell=false -error
// Note that `ScriptMain` doesn't work with Scala 3 yet, and won't unless a
// volunteer steps up: https://github.com/sbt/sbt/issues/6274
/***
scalaVersion := "2.13.5"
onLoadMessage := ""
scalacOptions ++= Seq(
"-deprecation", "-unchecked", "-feature", "-Werror")
@ChristopherDavenport
ChristopherDavenport / LiftF.scala
Created March 17, 2021 16:41
Generic Monad Transformer Lift Toolkit
trait LiftF[M[_[_], _]]{
def liftF[F[_], A](fa: F[A])(implicit ev: cats.Monad[F]): M[F, A]
}
object LiftF {
implicit class LiftOps[F[_], A](private val fa: F[A]){
def liftF[M[_[_], _]](implicit ev1: LiftF[M], ev2: Monad[F]): M[F, A] =
ev1.liftF(fa)
}
@ChristopherDavenport
ChristopherDavenport / BitInsanity.scala
Created March 15, 2021 17:42
Translate Ints to and From Bit Positions
object BitInsanity {
def bitPositions(int: Int): List[Int] = {
val buffer = new scala.collection.mutable.ListBuffer[Int]()
var number = int
var position = 0
while (number != 0){
if ((number & 1) != 0) {
buffer.addOne(position)
}
@ChristopherDavenport
ChristopherDavenport / ThreadSafeBitSet.scala
Last active March 15, 2021 22:26
Scala Translation of a ThreadSafeBitSet
// Credit to Hollow: https://github.com/Netflix/hollow/blob/b2890aab063b530b1cffe1af7b21d1358024c6e7/hollow/src/main/java/com/netflix/hollow/core/memory/ThreadSafeBitSet.java
import java.util.concurrent.atomic.AtomicLongArray
import java.util.concurrent.atomic.AtomicReference
import scala.util.control.Breaks
import scala.util.hashing.MurmurHash3
import scala.collection.BitSet
import scala.collection.mutable
class ThreadSafeBitSet private (
private final val numLongsPerSegment: Int,
@ChristopherDavenport
ChristopherDavenport / ReadWriteLock.scala
Created March 11, 2021 20:49
Wrapping Read-Write Lock
import cats.effect._
trait Lock[F[_]]{
def lock: F[Unit]
def unlock: F[Unit]
}
object Lock {
def fromLock[F[_]: Sync](jlock: java.util.concurrent.locks.Lock): Lock[F] = new Lock[F]{
def lock: F[Unit] = Sync[F].blocking(jlock.lockInterruptibly())
@ChristopherDavenport
ChristopherDavenport / VolatileReadWrite.scala
Last active March 11, 2021 20:12
VolatileReadWriteImpl
import cats._
import cats.syntax.all._
import cats.effect._
import cats.effect.concurrent._
trait VolatileReadWrite[F[_], A]{
def read: F[A]
def set(a: A): F[Unit] = write(_ => (a, ()))
def write[B](f: A => (A, B)): F[B]
}
@ChristopherDavenport
ChristopherDavenport / FakeIO.scala
Last active February 5, 2021 15:44
Simple FakeIO without stack-safety.
import cats.effect._
// import cats._
import cats.syntax.all._
import scala.concurrent.duration._
trait FakeIO[A]{
def unsafeRunAsync(f: Either[Throwable, A] => Unit): Unit
}
object FakeIO {
def delay[A](thunk: => A): FakeIO[A] = Async{cb: (Either[Throwable, A]=> Unit) => cb(Either.catchNonFatal(thunk))}
def async[A](f: (Either[Throwable, A] => Unit) => Unit): FakeIO[A] = Async{cb =>
@ChristopherDavenport
ChristopherDavenport / Bench.md
Created January 28, 2021 00:43
Current Benchmarking January 27th, 2021

Ember Server 0.21.8

hey -z 30s http://localhost:8080/foo

Summary:
  Total:    30.0004 secs
  Slowest:    0.4091 secs
  Fastest:    0.0001 secs
 Average: 0.0015 secs