Skip to content

Instantly share code, notes, and snippets.

View markhibberd's full-sized avatar

markhibberd markhibberd

View GitHub Profile
import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
main = simpleHttp "http://www.haskell.org/haskellwiki/Haskell" >>= L.putStr
@markhibberd
markhibberd / scala-2.10.0-match-on-nothing
Created February 4, 2013 09:51
Pattern matching on `Nothing`
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x: Option[Nothing] = None
x: Option[Nothing] = None
scala> x match {
| case Some(a) => a
| case None => None
@markhibberd
markhibberd / X.scala
Last active December 12, 2015 03:59
Implement terminating operation on (Boolean, A).
case class X[A](ok: Boolean, value: A) {
def map[B](f: A => B): X[B] =
X(ok, f(a))
def flatMap[B](f: A => X[B]) X[B] =
f(value)
def thenOr[B](f: A => X[B], otherwise: A => B) =
if (ok) then flatMap(f) else map(otherwise)
}
@markhibberd
markhibberd / gist:4957015
Created February 14, 2013 22:29
Argonaut json building combinators.
scala> ("asd" := 1)
res10: (String, argonaut.Json) = (asd,"1")
scala> ("qwe" :=? Some(1))
res11: Option[(String, argonaut.Json)] = Some((qwe,"1"))
scala> res10 ->: res11 ->?: jEmptyObject
res12: argonaut.Json = {"asd":"1","qwe":"1"}
scala> ("zxc" := "value") ->: ("nothing" :=? none[String]) ->?: jEmptyObject
@markhibberd
markhibberd / Chunk.scala
Created February 15, 2013 01:15
Applicative for Chunk[A] (A \/ StreamT[Future, A])
import scala.concurrent.{Future, ExecutionContext}
import scalaz._, Scalaz._
object Chunk {
type Chunk[A] = A \/ StreamT[Future, A]
implicit def ApplicativeChunk: Applicative[Chunk] = new Applicative[Chunk] {
def point[A](a: => A) = a.left
override def map[A, B](fa: Chunk[A])(f: A => B): Chunk[B] =
@markhibberd
markhibberd / gist:4981354
Created February 18, 2013 22:28
StackOverflow, type alias, higher kinds.
java.lang.StackOverflowError
at scala.tools.nsc.symtab.Types$SimpleTypeProxy$class.boundSyms(Types.scala:189)
at scala.tools.nsc.symtab.Types$SingletonType.boundSyms(Types.scala:1040)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3565)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3532)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3113)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3567)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3532)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3158)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3567)
@markhibberd
markhibberd / scala-syd.org
Created February 24, 2013 09:23
Scala-syd abstract.

Patterns in Types: Reader, Writer, State in Scala

Developers are often very good at spotting repetition in their programs at the value level, but for some reason struggle, or are reluctant to, apply that same careful analysis at the type level.

This talk aims to build up an intuition for spotting when you can factor your types, examining what this gains you, and looking at the practicalities for doing so in scala. We will use an example of

@markhibberd
markhibberd / rant.txt
Created March 8, 2013 05:36
version rant
Why do I believe this distinction is important?
Versions are virtual, the express intent not reality. Artifacts are real. How
they are idenitified is up for debate but I am of the belief it is not and
never can be versions.
As a concrete example, if I am using cabal and make the statement that I depend on
version 1.7.* of library barney, I am not stating that I think I work with all
versions of barney that start with 1.7, I am merely stating that my expectations
are that I am compatible with version 1.7 of barney and *believe* that barney intends
@markhibberd
markhibberd / applicative.scala
Last active December 14, 2015 18:38
Applicative builder to accumulate validation errors (tweaking http://paste.lisp.org/display/135932)
def makeAp(s : String) : ValidationNEL[Error, Uppercase] =
(notNull(s).toValidationNEL |@|
longerThan8(s).toValidationNEL |@|
uppercase(s).toValidationNEL).apply((_, _, _) => Uppercase(s))
@markhibberd
markhibberd / BrokenLaws.scala
Created March 12, 2013 10:14
The consequences of broken laws. Sensible code reviewer, realises you should not use foldRight in scala, so refactors to foldLeft. Mwhahaha.
// Don't try this at home
object BrokenLaws {
trait Monoid[F] {
def zero: F
def append(a: F, b: F): F
}
implicit def DoubleMonoid: Monoid[Double] = new Monoid[Double] {
def zero = 0.0
def append(a: Double, b: Double) = a + b