Skip to content

Instantly share code, notes, and snippets.

Avatar
🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
View GitHub Profile
@tpolecat
tpolecat / libs.md
Last active December 17, 2021 16:50
tpolecat library status
View libs.md

Latest releases and supported Scala versions for org.tpolecat stuff

Release 2.12 2.13 3.0.0 Notes
Atto 0.9.5
Doobie (CE2) 0.13.4
Doobie (CE3) 1.0.0-M5
Natchez (CE2) 0.0.26 EOL
Natchez (CE3) 0.1.3
View austin-vaccine.md
@tpolecat
tpolecat / JcaKeyBullshit.scala
Created July 22, 2020 18:35
GPG ASCII-amored exported text to JCA keys
View JcaKeyBullshit.scala
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,
@tpolecat
tpolecat / citext.md
Created January 9, 2018 07:53
Meta for citext Postgres type.
View citext.md

Ok, so to map CITEXT, which is not a standard type JDBC knows about, we need a wrapper class and need to move the value back and forth via the generic PGobject data type.

import org.postgresql.util.PGobject

case class CIText(s: String)

object CIText {
 
View hm.scala
sealed trait MaybeNumeric[A]
case class YesNumeric[A](n: Numeric[A]) extends MaybeNumeric[A]
case class NoNumeric[A]() extends MaybeNumeric[A]
object MaybeNumeric {
implicit def instance[A](implicit ev: Numeric[A]): MaybeNumeric[A] =
YesNumeric(ev)
}
View EndoT.scala
import scalaz._, Scalaz._
/** Abstraction over functions of the shape `A => F[A]`. */
case class EndoT[F[_], A](run: A => F[A]) {
def apply(a: A): F[A] =
run(a)
def andThen(e: EndoT[F, A])(implicit ev: Bind[F]): EndoT[F, A] =
e.compose(this)
View nope.md

It's not good enough to abstract Future to M[_]: Monad

Let's get our imports out of the way.

import scala.util.Random.nextInt
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
@tpolecat
tpolecat / listt.scala
Last active October 10, 2021 06:51
A demonstration in Scala of failed associativity of ListT. Example taken from https://wiki.haskell.org/ListT_done_right
View listt.scala
// addCompilerPlugin("com.milessabin" % "si2712fix-plugin" % "1.2.0" cross CrossVersion.full)
import scalaz._, Scalaz._
implicit def f2k[F[_], A, B](f: A => F[B]) = Kleisli(f)
val a: Int => ListT[List, Int] = {
case 0 => ListT(List(List(0, 1)))
case 1 => ListT(List(List(0), List(1)))
}
View stuff.md

EDIT: This gist has been promoted and is now a blog post.

Kinda-Curried Type Parameters

Scala methods can have multiple lists of value parameters but only one list of type parameters, which is occasionally irritating when some are inferable and others are not. Consider this method which has two type parameters, one inferable and one not.

import scalaz._, Scalaz._
View gist:b5ec288eca42140b6286

doobie experiment - postgres NOTIFY as scalaz-stream

One of the cool things PostgreSQL gives you is a simple notification system that you can use to let clients know that something interesting has happened. For instance you can set up rules that broadcast notifications when a table is updated, and client applications can update displays in response. The JDBC driver provides access to this API so I thought I would see what it would look like in doobie.

The program below constructs a Process[ConnectionIO, PGNotification] that registers for events, polls for them periodically (this is the best we can do with current driver support), and unregisters when the stream terminates for any reason. We use a Transactor to replace ConnectionIO with Task which gives us something we can actually run.

package doobie.example

import doobie.imports._