Skip to content

Instantly share code, notes, and snippets.

🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
Block or report user

Report or block tpolecat

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tpolecat
tpolecat / citext.md
Created Jan 9, 2018
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 Jan 9, 2019
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._
View gist:599627eac471b36f9a2a
def mergeRanges(ns: List[Int]): List[Range] =
ns.foldRight(List.empty[Range]) {
case (n, r :: rs) if (n == r.min - 1) => (n to r.max) :: rs
case (n, rs) => (n to n) :: rs
}
View freecoyo.scala
import scalaz.{ Free, Coyoneda, Monad, ~>, State, NonEmptyList }
import scalaz.std.function._
import scalaz.syntax.monad._
import scalaz.effect.IO
import scala.util.Random
object freecoyo extends App {
// An algebra of primitive operations in the context of a random number generator
View gist:8812750

Hi, looking for scalac flags?

This gist has been upgraded to a blog post here.

You can’t perform that action at this time.