Skip to content

Instantly share code, notes, and snippets.

View ChristopherDavenport's full-sized avatar

Christopher Davenport ChristopherDavenport

View GitHub Profile
@ChristopherDavenport
ChristopherDavenport / LiftedRef.scala
Created January 6, 2021 20:24
Lifted Ref Representation
/**
* Operates with default and anytime default is present instead information is removed from underlying ref.
**/
class LiftedRefDefaultStorage[F[_]: Sync](
val ref: Ref[F, Option[String]],
val default: String
) extends Ref[F, String]{
def get: F[String] = ref.get.map(_.getOrElse(default))
def set(a: String): F[Unit] = {
@ChristopherDavenport
ChristopherDavenport / RedisRef.scala
Created January 6, 2021 02:45
cats-effect Ref backed by Redis key location
package io.chrisdavenport.rediculous.ratelimiter
import cats.syntax.all._
import io.chrisdavenport.rediculous._
import cats.effect._
import cats.effect.concurrent._
import io.chrisdavenport.rediculous.RedisTransaction.TxResult.{Aborted, Success, Error}
import cats.Applicative
import scala.concurrent.duration._
import cats.data.NonEmptyList
@ChristopherDavenport
ChristopherDavenport / RateLimiter.scala
Last active January 6, 2021 00:22
Simple Redis Based Rate Limiter Implementation
package io.chrisdavenport.rediculous.ratelimiter
import cats.syntax.all._
import io.chrisdavenport.rediculous.{RedisConnection, RedisTransaction}
import io.chrisdavenport.rediculous.RedisCommands.{zremrangebyscore, zadd, zcard, zrange, pexpire, ZAddOpts}
import cats.effect.Concurrent
import io.chrisdavenport.rediculous.RedisTransaction.TxResult.{Aborted, Success, Error}
import cats.Applicative
import scala.concurrent.duration._
@ChristopherDavenport
ChristopherDavenport / CaseClassFun.scala
Last active December 10, 2020 22:13
Generic Show Derivation of Case Classes using Scala 3
package io.chrisdavenport.shapelessless
import cats._
import cats.syntax.all._
import cats.effect._
import scala.deriving._
import scala.compiletime._
import org.tpolecat.typename._
@ChristopherDavenport
ChristopherDavenport / DynamicRegion.scala
Last active December 4, 2020 17:15
Dynamic Monadic Region
// Based on a question from Julian Peeters https://github.com/julianpeeters
import cats._
import cats.syntax.all._
import cats.effect._
object Test {
case class DynamicRegion[F[_], A](allocate: F[(A, F[Unit])]){
def close(implicit F: FlatMap[F]): F[A] =
allocate.flatMap{case (a, shutdown) => shutdown.as(a)}
@ChristopherDavenport
ChristopherDavenport / gist:0a91881f6b771dde5f24774ff29ec40d
Created November 19, 2020 17:36
Check Chunk for Streaming Responses
import fs2._
object CheckChunk {
def checkChunk[F[_]: Sync, A](s: Stream[F, A]): F[Option[Stream[F, A]]] = {
s.pull
.uncons
.flatMap {
case None => Pull.output1(None)
case Some((c, s)) => Pull.extendScopeTo(Stream.chunk(c) ++ s).flatMap(s => Pull.output1(Some(s)))
}
@ChristopherDavenport
ChristopherDavenport / Libraries.md
Last active February 1, 2024 11:38
A Current Listing of Libraries
[error] java.lang.StackOverflowError
[error] at cats.effect.internals.IORunLoop$.liftedTree1$1(IORunLoop.scala:98)
[error] at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:97)
[error] at cats.effect.internals.IORunLoop$RestartCallback.signal(IORunLoop.scala:366)
[error] at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:387)
[error] at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:330)
[error] at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:141)
[error] at cats.effect.internals.IORunLoop$RestartCallback.signal(IORunLoop.scala:366)
[error] at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:387)
[error] at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:330)
@ChristopherDavenport
ChristopherDavenport / HeadersParser.scala
Last active July 28, 2020 03:25
Possible Header Parser Implementation
object HeaderP {
def parseHeaders[F[_]: MonadError[*[_], Throwable]](
s: Stream[F, Byte],
maxHeaderLength: Int,
acc: Option[ParseHeadersIncomplete])
: Pull[F, Nothing, (Headers, Boolean, Option[Long], Stream[F, Byte])] =
s.pull.uncons.flatMap {
case Some((chunk, tl)) =>
val nextArr = acc match {
@ChristopherDavenport
ChristopherDavenport / PerformanceMetals.md
Created July 25, 2020 16:40
Metals VsCode Improvement

Quick PSA on Metals with vscode

Since scala changes a lot of files in build and vscode watches those files. It actually has quite a delay. Adding this to settings will improve performance by an enormous degree. Especially on larger projects.

"files.exclude": {
        "**/.bloop": true,
        "**/.metals": true,
 "**/target": true