Skip to content

Instantly share code, notes, and snippets.

View arjanblokzijl's full-sized avatar

Arjan Blokzijl arjanblokzijl

  • Zürich, Switzerland
View GitHub Profile
"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)
#
@arjanblokzijl
arjanblokzijl / conduits_pipes.hs
Created March 28, 2012 07:35 — forked from sjoerdvisscher/conduits_pipes.hs
Bijection between Twan van Laarhoven's Pipe and the data types from Conduit
-- http://www.reddit.com/r/haskell/comments/rbgvz/conduits_vs_pipes_using_void_as_an_input_or/
import Control.Monad
import Data.Void
data Pipe m i o r =
NeedInput (i -> Pipe m i o r) (Pipe m Void o r)
| HaveOutput (Pipe m i o r) (m r) o
| Finished (Maybe i) r
| PipeM (m (Pipe m i o r)) (m r)
@arjanblokzijl
arjanblokzijl / hashmap-bench.scala
Created April 10, 2012 06:07 — forked from erikrozendaal/hashmap-bench.scala
Scala TreeMap benchmarks
import collection.mutable.ArrayBuffer
import collection.immutable.HashMap
object HashMapTest {
val random = new util.Random(1234)
def time(block: => Unit): Double = {
val start = System.nanoTime()
block
@arjanblokzijl
arjanblokzijl / gist:2651683
Created May 10, 2012 07:29 — forked from xeno-by/gist:2559714
Mixing in a trait dynamically
Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically.
Compile as follows:
scalac Common_1.scala Macros_2.scala
scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation>
Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API.
However the principles will remain the same in the final release, so the concept itself is okay.
===Common_1.scala===
@arjanblokzijl
arjanblokzijl / example.scala
Created July 5, 2018 07:49 — forked from SystemFw/example.scala
Running fs2 streams in parallel and collect their result in sequence, with queues
object Example {
import cats._, implicits._
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// start N streams concurrently, and stream their results in order
// e.g. download a file from a server in N parts concurrently, and stream it
// this approach is good for showcasing usage of concurrent abstractions,
// but a lower level implementation using Pull is likely to be much more performant
@arjanblokzijl
arjanblokzijl / ParallelFoldMonoid.scala
Created July 9, 2018 08:38 — forked from SystemFw/Test.scala
Parallel foldMonoid with fs2
object Test {
import cats.implicits._
import cats.kernel.CommutativeMonoid
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// same as parallelFoldMonoid, but with some logging to show the parallelism
def loggedParallelFoldMonoid[F[_], A: CommutativeMonoid](n: Int)(
input: Stream[F, A])(implicit F: Effect[F], ec: ExecutionContext) =