Skip to content

Instantly share code, notes, and snippets.

View fthomas's full-sized avatar

Frank Thomas fthomas

View GitHub Profile
@cb372
cb372 / gist:66bc0ffdfec87744c473355a34ac61f3
Created January 3, 2019 21:26
Pushing commits to someone else's PR
# Terminology
their_username = the GitHub username of the contributor who submitted the PR
their_branch = the name of the branch they used to submit the PR
repo_name = the name of your GitHub repository
# First add their fork as a remote
git remote add $their_username [email protected]:${their_username}/${repo_name}.git
# Then fetch it
git fetch $their_username

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

import scala.reflect.ClassTag
object Test {
type Flags = Flags.Type
trait Flags1 {
import Flags._
type Type <: Int with Tag$$1
}
object Flags extends Flags1 {
trait Tag$$1 extends Any

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

import buffer.Buffer
import org.scalajs.dom
import dom.document
import eu.timepit.crjdt.circe.RegNodeConflictResolver.LWW
import eu.timepit.crjdt.circe.syntax._
import eu.timepit.crjdt.core.{Operation, Replica}
import eu.timepit.crjdt.core.syntax._
import upickle.default._
import scala.scalajs.js
@pvillega
pvillega / doobie-refined-meta-composite.scala
Created February 22, 2017 19:36
Allow Refined types on case classes used by Doobie
// Credit to Kevin Horlick @beefyhalo fir the Meta implementation
// See https://gitter.im/tpolecat/doobie/archives/2015/12/22
// It wasn't straightforward to find the answer, my Google-foo is weak, so I hope
// this helps people looking for this answer
// allows generation of doobie Meta objects from Refined types
implicit def refinedMeta[T: Meta, P, F[_, _]](implicit tt: TypeTag[F[T, P]],
ct: ClassTag[F[T, P]],
validate: Validate[T, P],
refType: RefType[F]): Meta[F[T, P]] =

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@benhutchison
benhutchison / AmmoniteReplSession
Created July 14, 2016 06:26
Any simpler way to write Refined Higher-kinded types?
Welcome to the Ammonite Repl 0.6.2
(Scala 2.11.8 Java 1.8.0_51)
@ load.plugin.ivy("org.spire-math" %% "kind-projector" % "0.8.0")
@ load.ivy("org.typelevel" %% "cats" % "0.6.0")
@ import cats._; import implicits._
import cats._;
import implicits._
@ load.ivy("eu.timepit" %% "refined" % "0.5.0")
@larsrh
larsrh / list.scala
Last active July 22, 2016 07:50
safe head/tail on lists using covariance
// Initial idea courtesy of @dwijnand
// Improved by @fthomas
sealed trait List[+T] {
def head: Option[T]
}
final case class Cons[T](h: T, t: List[T]) extends List[T] {
val head: Some[T] = Some(h)
}