Skip to content

Instantly share code, notes, and snippets.

@gseitz
gseitz / build.scala
Created February 19, 2013 22:52
SBT task for running all main classes
import sbt._
import Keys._
import Build.data
object build extends Build {
lazy val runAll = TaskKey[Unit]("run-all")
lazy val standardSettings = Seq(
runAllIn(Compile)
)
@gseitz
gseitz / FSMSupervision.scala
Created August 23, 2012 21:12
Akka FSM Supervision
package akka.actor
import collection.mutable
import akka.actor.FSM.{ CurrentState, Transition, UnsubscribeTransitionCallBack, SubscribeTransitionCallBack }
import akka.routing.{ Deafen, Listen }
case object PreRestart
case object PostRestart
trait ParentNotification { thisActor: Actor ⇒
@gseitz
gseitz / PatMatCrash_2.10.0-M7.scala
Created August 22, 2012 15:08
Crash in pattern matcher in 2.10.0-M7
class PatMatCrash {
def foo[T](pattern: PartialFunction[T, Unit]) = ???
// CRASHES
foo {
case "foo" => ()
}
// does NOT crash
foo[String] {
@gseitz
gseitz / gist:3190574
Created July 27, 2012 21:34
Edward "The Lens Machine" Kmett on his take on MultiLenses
[22:34] < mightybyte> But data-lens isn't up to date with it.
[22:34] < edwardk> ghci> :t sumOf folded
[22:34] < edwardk> sumOf folded :: (Num c, Data.Foldable.Foldable f) => f c -> c
[22:35] < mightybyte> Hah! I was wondering how long you'd be able to resist.
[22:35] < edwardk> but we can use sumOf with any Lens, Traversal or Fold, etc.
[22:35] < edwardk> for instance: sumOf (traverse.traverse):: (Num c, Traversable t1, Traversable t) => t (t1 c) -> c
[22:35] < edwardk> sumOf (traverse._2), ec.
[22:36] < edwardk> toListOf (traverse.traverseLeft) :: Traversable t => t (Either c c1) -> [c]
[22:36] < edwardk> basically traverse is a valid Traversal, but there are lots of others
[22:38] < edwardk> so multilenses become 'Traversals' and you can do things like: productOf (traverseValueAt 4), etc.
@gseitz
gseitz / sbt_complete.scala
Created July 18, 2012 08:52
SBT command for extracting completions
package sbtcomplete
import sbt._
import complete.JLineCompletion
import Keys._
object SbtComplete {
private val CompletionsCommand = "completions"
private val CompletionsBrief = ("<arg>", "The string to complete.")
@gseitz
gseitz / MirroredLens.scala
Created July 5, 2012 15:21
Edward Kmett's new lens trickery in scala
package scalaz.lens
import scalaz.Functor
case class Store[C, D, B](f: D => B, c: C)
object Store extends StoreInstances
trait StoreInstances {
implicit def storeFunctor[C, D]: Functor[({type l[b]=Store[C, D, b]})#l] = new Functor[({type l[b]=Store[C, D, b]})#l] {
def map[A, B](fa: Store[C, D, A])(f: A => B): Store[C, D, B] = {
@gseitz
gseitz / gist:2588577
Created May 3, 2012 19:34
ascii tree of scalaz-tests test dependencies
> tests/test:print-ascii-graph
[info] org.scalaz:scalaz-tests_2.9.2:7.0-SNAPSHOT
[info] +-org.scala-lang:scala-library:2.9.2
[info] +-org.scala-tools.testing:scalacheck_2.9.1:1.9
[info] | +-org.scala-tools.testing:test-interface:0.5
[info] |
[info] +-org.scalaz:scalaz-concurrent_2.9.2:7.0-SNAPSHOT
[info] | +-org.scala-lang:scala-library:2.9.2
[info] | +-org.scalaz:scalaz-core_2.9.2:7.0-SNAPSHOT
[info] | +-org.scala-lang:scala-library:2.9.2
@gseitz
gseitz / ApplicativePlus.scala
Created April 29, 2012 18:51
trying to unbork ApplicativePlus.{some, many}
// original, broken version
def many[A](a: F[A]): F[List[A]] = {
lazy val y: Free.Trampoline[F[List[A]]] = z map (plus(_, point(Nil)))
lazy val z: Free.Trampoline[F[List[A]]] = y map (map2(a, _)(_ :: _))
y.run
}
// trying to make sense of trampoline
@gseitz
gseitz / gist:2427142
Created April 20, 2012 08:33
Some /== Option leads to a compiler error
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> Option(17) match {
| case opt @ Some(_) if opt /== Option(42) =>
| }
<console>:48: error: type mismatch;
trait PolyParse[F[_]] {
def F: Functor[F]
def APP: Applicative[F]
def ALT: Alternative[F]
def C: Commitment[F]
def M: MonadFail[F]
}
def oneOf[FA](fas: List[FA])(implicit U: Unapply[PolyParse, FA]): U.M[U.A] = fas match {
case Nil => U.TC.M.fail[U.A]("failed to parse any of the possible choices".toList)