Skip to content

Instantly share code, notes, and snippets.

View ChristopherDavenport's full-sized avatar

Christopher Davenport ChristopherDavenport

View GitHub Profile
@ChristopherDavenport
ChristopherDavenport / SocketPain.scala
Created August 11, 2021 22:29
Example of Asynchronous Socket Channel Pain
import cats.effect._
import java.nio.channels.AsynchronousSocketChannel
import java.nio.ByteBuffer
import java.net.InetSocketAddress
import java.nio.channels.{AsynchronousSocketChannel, CompletionHandler}
import java.nio.{Buffer, ByteBuffer}
object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
@ChristopherDavenport
ChristopherDavenport / Leak.scala
Last active August 4, 2021 00:24
Purposeful Cached Resource Leak - Like Memoize But can Move into SyncIO
object ResourceLeak {
def leak[F[_]: Sync, G[_]: Async, A](resource: Resource[G, A]): F[G[A]] = {
Ref.in[F, G, Option[Deferred[G, A]]](None).map{ref =>
def initiateOrGet(withDef: Option[Deferred[G, A]] = None): G[A] = Concurrent[G].uncancelable(poll =>
ref.modify{
case s@Some(a) => (s: Option[Deferred[G, A]], a.get)
case None => withDef match {
case s@Some(deferred) => (s, poll(resource.allocated.flatTap{ case (a, _) => deferred.complete(a).void}.map(_._1)).onCancel(ref.set(None)).onError{ case _ => ref.set(None)})
case None => (None, poll(Deferred[G, A].map(_.some).flatMap(initiateOrGet(_))))
@ChristopherDavenport
ChristopherDavenport / SetOnceCache.scala
Last active October 11, 2021 13:06
Set Once Cache
import cats._
import cats.implicits._
import cats.effect._
import cats.effect.implicits._
import scala.concurrent.duration._
import io.chrisdavenport.mapref._
trait SetOnceCache[F[_], K, V]{
def get(k: K): F[V]
@ChristopherDavenport
ChristopherDavenport / Http4sDirective.scala
Created July 10, 2021 04:43
Http4s Directive Baseline
package io.chrisdavenport.http4s.directive
import cats._
import cats.data._
import org.http4s._
import cats.effect._
import org.http4s._
import org.http4s.implicits._
import cats.syntax.all._
import cats.effect.std._
import cats.effect._
trait FiberLocal[F[_], A]{
def get: F[A]
def set(value: A): F[Unit]
def reset: F[Unit]
@ChristopherDavenport
ChristopherDavenport / releases.md
Last active August 13, 2021 19:15
Davenverse Library status

Latest releases and supported Scala versions for Davenverse org.typelevel stuff

Release 2.12 2.13 3.0.0
Log4cats (CE2) 1.3.1
Log4cats (CE3) 2.1.1
Keypool (CE2) 0.3.5
Keypool (CE3) 0.4.5
Unique (CE2) - CE3 is built-in 2.1.5
Vault (CE2) 2.1.13
@ChristopherDavenport
ChristopherDavenport / Main.scala
Last active May 20, 2021 18:05
XLSX to Cormorant
import java.nio.file.Paths
import cats.effect.IOApp
import cats.effect.{ExitCode, IO}
import cats.syntax.all._
object Main extends IOApp {
// Will be noticeably cleaner on fs2 3
def run(args: List[String]): IO[ExitCode] = {
import cats.effect._
Blocker[IO].use(blocker =>
@ChristopherDavenport
ChristopherDavenport / Opt.scala
Last active May 20, 2021 03:09
Zero Allocation Option Alternative
/**
* Note `Opt[Opt[A]]` is always a lie, and can and should be flattened to `Opt[A]`
*
* Any layer of `Opt` is equivalent to `| Null`, which in terms of union types
* means that more than 1 layer of `| Null` is the same as the first, so don't
* attempt to use this with more than 1 layer,
*
* If you want more than 1 layer ever, you should probably use Option.
* You should probably use Option regardless, Some(None) is a meaningful type, and there is
* no way to represent that with this type.
@ChristopherDavenport
ChristopherDavenport / DoFoo.scala
Last active May 11, 2021 23:41
Exercise on clean shutdown
import cats._
import cats.syntax.all._
import cats.effect._
import cats.effect.syntax.all._
import org.http4s._
import org.http4s.implicits._
import org.http4s.ember.server._
import scala.concurrent.duration._
object Main extends IOApp {
@ChristopherDavenport
ChristopherDavenport / addI.scala
Created May 8, 2021 05:47
Add Ith Value, either early or late
object AddI {
def addIEarly(x: Int, i: Int): Int = {
require(x >= 0, "addIEarly: x must be positive")
require(i >= 0, "addIEarly: i must be positive")
val reset = i - 1
@scala.annotation.tailrec def go(x: Int, pos: Int, acc: Int): Int = {
println(s"go: x-$x acc-$acc, pos:$pos")
(x, pos) match {
case (0, _) => acc
case (other, 0) => go(other-1, reset, acc + other)