Skip to content

Instantly share code, notes, and snippets.

View arjanblokzijl's full-sized avatar

Arjan Blokzijl arjanblokzijl

  • Zürich, Switzerland
View GitHub Profile
//some nice but perhaps not so useful list operations
object ListOps {
//translated to Scala: http://www.haskell.org/pipermail/libraries/2010-July/013842.html
def select[T](l: List[T]): List[(T, List[T])] = l match {
case Nil => Nil
case x::xs => (x, xs) :: (for {(y, ys) <- select(xs)} yield (y, x::ys))
}
def separate[T](l: List[T]): List[(List[T], T, List[T])] = l match {
def tails[T](l: List[T]): List[List[T]] = {
if (l.isEmpty) List(Nil)
else l :: tails(l.tail)
}
def comb[T](n: Int, l: List[T]): List[List[T]] = {
if (n == 0) List(Nil) else {for (xs <- tails(l) if !xs.isEmpty; ys <- comb(n - 1, xs.tail)) yield (xs.head :: ys)}
}
@arjanblokzijl
arjanblokzijl / depenciesArtifactFiles
Created August 4, 2011 18:33
sbt all artifacts
import sbt._
import Keys._
import Defaults._
//adding the Artifacts.settings to you project settings will create and publish the artifact defined as 'theMainArtifact'
object Artifacts {
val allTheArtifacts = TaskKey[Seq[(sbt.Artifact, java.io.File)]]("all-artifacts")
val theMainArtifact = TaskKey[File]("zip-artifact", "Create zip with all library dependencies")
val artifactConf = config("Artifact") hide //use this to scope the create artifact to this config
@arjanblokzijl
arjanblokzijl / gist:1141567
Created August 12, 2011 06:24
sbt10 - generated sources
import sbt._
import sbt.Keys._
object build extends Build {
lazy val generate = Project(
id = "source-gen",
base = file("."),
settings = Defaults.defaultSettings ++ generateSourceSettings
)
@arjanblokzijl
arjanblokzijl / gist:1278729
Created October 11, 2011 17:20
most basic monad state transformer example
scala> for (s <- stateT[Int, Option, String](i => Some(("aaa", i + 1)))) yield(s + "bbb")
res0: scalaz.StateT[Int,Option,java.lang.String] = scalaz.StateTs$$anon$1@4f837a5b
scala> res0.runT(10)
res31: Option[(java.lang.String, Int)] = Some((aaabbb,11))
"FingerTree" should {
"apply effects in order" in {
import std.string._
import Id._
type StringWriter[A] = Writer[String, A]
val s: Writer[String, FingerTree[Int, Int]] = streamToTree(intStream.take(5)).traverseTree[StringWriter, Int, Int](x => Writer(x.toString, x))
val (w, a) = s.runT
(w, a.toStream) must be_===("12345", streamToTree(intStream.take(5)).toStream)
}
}
@arjanblokzijl
arjanblokzijl / gist:1779556
Created February 9, 2012 12:07 — forked from einblicker/gist:1402318
encoding data families by using dependent method types
trait GMap[+V] {
type P[+_]
val content: P[V]
}
trait GMapKey[K] {
type GM[+V] <: GMap[V]
def empty[V]: GM[V]
def lookup[V](k: K, m: GM[V]): Option[V]
def insert[V](k: K, v: V, m: GM[V]): GM[V]
}
@arjanblokzijl
arjanblokzijl / type-class-type-member.scala
Created February 9, 2012 18:19 — forked from retronym/type-class-type-member.scala
Type Classes using type members, rather than type parameters
trait Semigroup_ {
type F
def append(a: F, b: => F): F
}
trait Monoid_ extends Semigroup_ {
def zero: F
}
trait Functor_ {
@arjanblokzijl
arjanblokzijl / main.hs
Created February 26, 2012 13:11 — forked from danielwaterworth/main.hs
Error conscious, pure iteratee library (based on pipes)
{-# LANGUAGE FlexibleInstances #-}
import Data.Char
import Control.Monad
import Control.Exception
import Control.Monad.Trans
import System.IO
@arjanblokzijl
arjanblokzijl / fix-sbt-compiler-interface.sh
Created March 6, 2012 06:15 — forked from gkossakowski/fix-sbt-compiler-interface.sh
Fix sbt's compiler interface for 2.10.0-M1
#!/usr/bin/env bash
# This is very hacky remedy to following error people using sbt
# and 2.10.0-M1 release of Scala are going to see:
#
# API.scala:261: error: type mismatch;
# found : API.this.global.tpnme.NameType
# (which expands to) API.this.global.TypeName
# required: String
# sym.isLocalClass || sym.isAnonymousClass || sym.fullName.endsWith(LocalChild)
#