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
object SJ extends App {
trait Getter[T] {
def get(s: String): T
}
object Getter {
implicit val getInt = new Getter[Int] {
def get(s: String) = s.toInt // unsafe
@tpolecat
tpolecat / gist:4743224
Last active December 12, 2015 08:18
Inversions exercise from fayimora
import scala.io.Source
import System.{ currentTimeMillis => now }
object Inv extends App {
// An int set that knows how many members are greater than a given number.
sealed trait GSet {
def ngt(n: Int): Int // number greater than `n`
def +(n: Int): GSet
}
@tpolecat
tpolecat / gist:4755770
Created February 11, 2013 17:05
Map wrapper can easily return Some(null)
import scala.collection.convert.Wrappers.JMapWrapper
import java.util.Collections
object MapTest extends App {
val m = new JMapWrapper(Collections.synchronizedMap(new java.util.HashMap[String,String]))
new Thread {
override def run {
while (true) {
@tpolecat
tpolecat / gist:4771973
Created February 12, 2013 18:15
Example usage of scalaz7's State monad.
import scalaz.State // the State[S,A] type itself
import scalaz.State._ // primitives like get and put are here
object StateExample extends App {
// A computation that produces a String
val action1: State[Int, String] =
for {
n <- get
_ <- put(n + 1)
import annotation.implicitNotFound
@implicitNotFound(msg = "This message can never appear!")
trait ~>[A, B] { self =>
def apply(a: A): B
def invert(b: B): A = inverse.apply(b)
def inverse: B ~> A = new ~>[B, A] {
def apply(b: B) = self.invert(b)
override def invert(a: A) = self(a)
}
object Tuples {
trait HT {
type H
type T
def hd: H
def tl: T
}
implicit class T3[A, B, C](t: (A, B, C)) extends HT {
object Nafg {
object Tuples {
trait HT[A] {
type H
type T
def hd(a: A): H
def tl(a: A): T
}
implicit def mv[F] = new Monad[({ type λ[a] = Validation[F, a] })#λ] {
def bind[A, B](fa: Validation[F, A])(f: A => Validation[F, B]): Validation[F, B] = fa.flatMap(f)
def point[A](a: => A): Validation[F, A] = a.success
}
object stuff {
import scala.util.parsing.combinator._
class Parser extends RegexParsers {
def parseTop(s: String): Stuff = parse(top(""), s) match {
case Success(value, _) => value
case _ => throw new RuntimeException("...")
}
import language.higherKinds
object FooExample extends App {
// Slice of comonad is where this came up
trait Foo[F[_]] {
def coflatMap[A, B](f: F[A] => B): F[A] => F[B]
}
// A non-empty list