Skip to content

Instantly share code, notes, and snippets.

View tpolecat's full-sized avatar
🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
View GitHub Profile
# AdAway default blocklist
# Blocking mobile ad providers and some analytics providers
#
# Project home page:
# https://github.com/AdAway/adaway.github.io/
#
# Fetch the latest version of this file:
# https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt
#
# License:
@tpolecat
tpolecat / libs.md
Last active December 17, 2021 16:50
tpolecat library status

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
@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,
@tpolecat
tpolecat / citext.md
Created January 9, 2018 07:53
Meta for citext Postgres type.

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 {
 
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)
}
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)

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
// 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)))
}

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._