Skip to content

Instantly share code, notes, and snippets.

View ChristopherDavenport's full-sized avatar

Christopher Davenport ChristopherDavenport

View GitHub Profile
@johnynek
johnynek / typed_record.scala
Created June 25, 2021 19:05
An example of zero cost addition of types on top of `Map[String, Any]` to model generic records in scala 3
package example
object RecMap {
object Record {
// use this scope to bound who can see inside the opaque type
opaque type Rec[A <: Tuple] = Map[String, Any]
object Rec {
type HasKey[A <: Tuple, K] =
@tpolecat
tpolecat / JcaKeyBullshit.scala
Created July 22, 2020 18:35
GPG ASCII-amored exported text to JCA keys
import java.io.ByteArrayInputStream
import java.security.PrivateKey
import java.security.PublicKey
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.openpgp.operator.jcajce._
import org.bouncycastle.openpgp._
import scala.util.control.NonFatal
/**
* Methods to turn GPG ASCII-amored exported text into JCA keys. Many thanks to Twitter friends,
@olafurpg
olafurpg / DownloadSources.scala
Last active May 24, 2019 19:47
Script to download all library sources into a target directory
import sbt._, Keys._
object DownloadSources extends AutoPlugin {
override def trigger = allRequirements
override def requires = sbt.plugins.JvmPlugin
override def projectSettings =
List(Compile, Test).flatMap { config =>
inConfig(config)(
TaskKey[Seq[File]]("downloadSources") := Def.task[Seq[File]] {
val out = baseDirectory.in(ThisBuild).value / "target" / "sources"
@Daenyth
Daenyth / ImmutableLRU.scala
Created March 12, 2019 17:36
LruRef / LRU for cats-effect
package teikametrics
import scala.collection.SortedMap
/**
* Immutable implementation of an LRU cache.
*
* @author Twitter
*
* Copy pasted from the version previously in twitter-util v19.1.0 at
@mpilquist
mpilquist / philosophers.scala
Last active April 28, 2023 23:31
Dining Philosophers with FS2
/*
scalaVersion := "2.12.7"
resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies += "co.fs2" %% "fs2-core" % "1.0.1-SNAPSHOT"
*/
import cats._
import cats.implicits._
import cats.effect._
@SystemFw
SystemFw / Ex.scala
Last active November 3, 2018 17:41
Encoding existentials via abstract types (1) and higher-ranks (2)
// This is the traditional encoding, using abstract type members
// to encode existentials
object AbstractExistentials {
trait E {
type X
val x: X
def f(p: X): String
}
@SystemFw
SystemFw / Ex.scala
Created October 26, 2018 20:39
Get instances of a typeclass `TC[_]` for all cases of a Coproduct [shapeless]
import shapeless._
@annotation.implicitNotFound("Make sure all cases of ${C} have an instance of ${TC}")
trait CopTCs[C <: Coproduct, TC[_]] {
type Out <: HList
def result: Out
}
object CopTCs {
type Aux[C <: Coproduct, TC[_], O <: HList] = CopTCs[C, TC] { type Out = O }
def apply[C <: Coproduct, TC[_]](implicit ev: CopTCs[C, TC]) = ev

Quick Tips for Fast Code on the JVM

I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So… here's my word of mouth.

This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea

@mrkgnao
mrkgnao / Eftee.hs
Last active September 23, 2019 15:43
An "optimized" version of ElvishJerricco's free arrow type
{-# LANGUAGE Arrows #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
@jkachmar
jkachmar / 1README.md
Last active December 24, 2021 15:06
Small Docker images with Alpine, Haskell, and Stack

All actions should be performed in the root directory of a Haskell project that uses stack. The following lines should be present in the project's stack.yaml file:

docker:
  enable: true

Additionally, the BaseImage and Dockerfile files from this gist should also be present in the project's root directory.