Skip to content

Instantly share code, notes, and snippets.

$ cabal build
Building validation-examples-0.4.0...
Preprocessing executable 'validation-examples' for
validation-examples-0.4.0...
[1 of 1] Compiling Main ( Main.hs, dist/build/validation-examples/validation-examples-tmp/Main.o )
Main.hs:59:29:
Found hole ‘_AccValidation’
with type: AnIso
(Either Int String)
@purefn
purefn / gist:5519380
Created May 5, 2013 01:45
scalac 2.10.1 crash report
// This code
def update_(e: E, f: E => E)(implicit M0: Monad[M], E: Entity[E]): StateT[M, S, Unit] = {
def mod(e1: E) = (f(e1), ())
update[Unit](e, mod) >| ()
}
def update[A](e: E, f: E => (E, A))(implicit M0: Monad[M], E: Entity[E]): StateT[M, S, Option[(E, A)]] =
/* is causing scalac 2.10.1 to crash
@purefn
purefn / FunctorOpsExample.scala
Created May 3, 2013 21:16
FunctorOps broken on 2.10 when importing `syntax.traverse` and `syntax.monad._`
package purefn.playground
import scalaz._
import std.list._
object FunctorOpsExample {
object TraverseWorks {
import syntax.traverse._
List(1, 2, 3) >| 1
@purefn
purefn / gist:5257660
Last active December 15, 2015 12:08
// This character generator is similar to the default one from ScalaCheck, but it filters out:
// 1. Unprintable control characters. These cause problems in Mongo queries, and should not able
// to get into our database since they will not be present in inputs parsed from HTTP text.
// 2. Characters that do not exist in the default platform encoding. We can exclude a big range
// of these right away (which ScalaCheck does) because they're in the range used for two-
// character UTF-16 sequences, but others are only detected by checking Character.isDefined(_).
// The symptom of using a bad character in a string is that when you encode it to UTF-8 and
// back again, you get a different string, so any of our web and database tests that check the
// input against the output may fail-- and you'll see a question mark somewhere in the data,
// since that's how bad characters are displayed.
[error] found : scalaz.package.ValidationNel[String,scalaz.NonEmptyList[A]]
[error] required: ?F[?A]
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
[error] and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
[error] are possible conversion functions from scalaz.package.ValidationNel[String,scalaz.NonEmptyList[A]] to ?F[?A]
@purefn
purefn / gist:5118763
Created March 8, 2013 18:45
Empty promises or promises that share an actor

I have a situation in which I have a non-thread safe resource which performs computations. So I need to serialize usage of this resource. In this particular case, it is an embedded Scala interpreter.

I do not want to block threads waiting for access to this resource, so I want to use Promise. Currently the way I'm doing this is to create a single threaded Strategy for each interpreter. This sucks because it means there are as many threads as there are interpreters, and they will be idle when the interpreter is not being used.

So far I've thought of 2 possible solutions that allow me to still use Promises

  1. Introduce an emptyPromise constructor. This was done in scalaz 6, but either didn't get ported to scalaz 7 or it was purposefully dropped (otherwise I'd just add it back in and be done with it). I could then cre
java.lang.StackOverflowError
at scala.tools.nsc.symtab.Types$SimpleTypeProxy$class.boundSyms(Types.scala:189)
at scala.tools.nsc.symtab.Types$SingletonType.boundSyms(Types.scala:1040)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3560)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3527)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3122)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3562)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3527)
at scala.tools.nsc.symtab.Types$TypeMap.mapOver(Types.scala:3108)
at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3562)
scala> import scalaz._
import scalaz._
scala> import effect._
import effect._
scala> import kleisliEffect._
import kleisliEffect._
scala> import scalaz.syntax.effect.all._
scala> val o: Option[Int] = Some(5)
o: Option[Int] = Some(5)
scala> val l: List[Int] = List(10)
l: List[Int] = List(10)
scala> l flatMap (_ => o)
res1: List[Int] = List(5)
scala> o flatMap (_ => l)
trait Option[+A] {
@inline final def cata[Z](some: A => Z, none: => Z) = this match {
case Some(a) => some(a)
case None => none
}
}
object Option {
private[Option] case class Some[+A](a: A) extends Option[A]
private[Option] case object None extends Option[Nothing]