Skip to content

Instantly share code, notes, and snippets.

@mpilquist
mpilquist / gist:3658856
Created September 6, 2012 17:39
Scalaz Karaf Commands
bundle:install mvn:org.scala-ide/scala-library/2.9.2.v20120330-163119-949a4804e4-vfinal
bundle:install mvn:org.scalaz/scalaz-core_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-effect_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-iteratee_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-concurrent_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-typelevel_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-iterv_2.9.2/7.0.0-M3
bundle:install mvn:org.scalaz/scalaz-xml_2.9.2/7.0.0-M3
@mpilquist
mpilquist / gist:3667605
Created September 7, 2012 16:32
Trying to combine Lens[A, B] and Lens[A, C] in to Lens[A, (B, C)]
scala> def fanOut[A, B, C](f: Lens[A, B], g: Lens[A, C]): Lens[A, (B, C)] = Lens.lensu[A, (B, C)]({ case (a, (b, c)) => f.set(g.set(a, c), b) }, a => (f.get(a), g.get(a)))
fanOut: [A, B, C](f: scalaz.package.Lens[A,B], g: scalaz.package.Lens[A,C])scalaz.package.Lens[A,(B, C)]
scala> case class Foo(x: Int, y: Int)
defined class Foo
scala> val XL = Lens.lensu[Foo, Int]((f, x) => f.copy(x = x), _.x)
XL: scalaz.package.Lens[Foo,Int] = scalaz.LensTFunctions$$anon$5@3dfc8f84
scala> val YL = Lens.lensu[Foo, Int]((f, y) => f.copy(y = y), _.y)
@mpilquist
mpilquist / gist:3679896
Created September 8, 2012 21:26
Template approach to avoiding explicit type annotations
import scalaz._
/**
* Provides type aliases and utilities for working with state disjunctions.
*
* A state disjunction is a function `S => F[(S, L \/ R)]` wrapped in the StateT and EitherT monad
* transformers.
*
* See `StateDisjunctionT` and `StateDisjunction` for more information.
@mpilquist
mpilquist / gist:3909128
Created October 18, 2012 00:09
Scala Maven Plugin settings
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala.plugin.version}</version>
<configuration>
<recompileMode>incremental</recompileMode>
<useZincServer>true</useZincServer>
<args>
<arg>-target:jvm-1.5</arg>
<arg>-deprecation</arg>
@mpilquist
mpilquist / gist:3909703
Created October 18, 2012 03:25
Don't mutate stuff while iterating
scala> val s = MSet() ++ (1 to 20)
s: scala.collection.mutable.Set[Int] = Set(5, 9, 15, 16, 18, 14, 19, 2, 20, 6, 1, 13, 4, 12, 7, 3, 8, 17, 11, 10)
scala> s.size
res16: Int = 20
scala> s foreach { n => if (n % 2 == 0) s -= n }
scala> s.size
res18: Int = 11
@mpilquist
mpilquist / Foo.scala
Created December 6, 2012 19:16
Scala 2.10.0-RC2 compilation bug?
import scala.concurrent.duration._
trait Foo { implicit val timeout = 1 second }
trait Bar extends Foo { implicit override val timeout = 2 seconds }
class Age private (val v: Short) extends AnyVal {
def isInfant = v < 2
def isToddler = v >= 2 && v <= 4
def isSenior = v >= 50
}
object Age {
def apply(v: Short): Age = {
require(v >= 0 && v < 200, "Must be between 0 and 200")
new Age(v)
@mpilquist
mpilquist / Fib.scala
Last active December 14, 2015 04:08
Fibonacci with Scalaz's State monad (uses Scala 2.10 and Scalaz 7.0.0.M8)
import scalaz._
import Scalaz._
object Fib {
type Memo = Map[Int, Int]
def stFib(n: Int): State[Memo, Int] = n match {
case 0 => State.state(0)
case 1 => State.state(1)
case n =>
@mpilquist
mpilquist / State.scala
Created February 24, 2013 21:57
Example of computing Fibonacci sequence with Scala 2.10 and memoizing State monad
trait State[S, A] {
val run: S => (S, A)
def apply(s: S): (S, A) =
run(s)
def eval(s: S): A =
apply(s)._2
@mpilquist
mpilquist / innerTypeTags.scala
Created March 17, 2013 14:14
Getting type tags for type parameters to type constructors
import scala.reflect._
import scala.reflect.api._
def innerTypeTags[A: TypeTag]: List[TypeTag[_]] = {
val typeParams = typeTag[A].tpe match {
case TypeRef(_, _, args) => args
}
typeParams map { tpe =>
TypeTag(typeTag[A].mirror, new TypeCreator {