This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Semigroup_ { | |
type F | |
def append(a: F, b: => F): F | |
} | |
trait Monoid_ extends Semigroup_ { | |
def zero: F | |
} | |
trait Functor_ { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances #-} | |
import Data.Char | |
import Control.Monad | |
import Control.Exception | |
import Control.Monad.Trans | |
import System.IO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) = |
OlderNewer