Skip to content

Instantly share code, notes, and snippets.

View bvenners's full-sized avatar

Bill Venners bvenners

View GitHub Profile
One way Scalaz's === operator differs from ScalaUtils is that Scalaz does not define default
Equal instances whereas ScalaUtils does provide default Equality and Equivalence instances.
Scalaz provides Equal instances for Int and List[Int], for example, but it doesn't do so for
mutable objects. Thus by default you can compare Ints and List[Int]s with === in Scalaz, but
not arrays:
scala> import scalaz._
import scalaz._
scala> import Scalaz._
@bvenners
bvenners / gist:8477980
Created January 17, 2014 17:47
Applying ScalaUtils' tunable type checks to contains checking of Seqs.
// If you paste this code into the Scala interpretter with ScalaUtils on the path:
List(1) contains 1
List(1) contains "1"
import scala.collection.GenSeq
implicit class Containz[A](xs: GenSeq[A]) {
def containz[B <: A](ele: B): Boolean = xs.exists(_ == ele)
}
@bvenners
bvenners / gist:8469906
Created January 17, 2014 08:03
Type checked contains method for GenSeq
scala> List(1, 2, 3) contains 1
res5: Boolean = true
scala> List(1, 2, 3) contains "1"
res6: Boolean = false
scala> import scala.collection.GenSeq
import scala.collection.GenSeq
scala> implicit class Containz[A](xs: GenSeq[A]) {
@bvenners
bvenners / gist:8407660
Last active January 3, 2016 04:18
Recommended way to get type-checked === in ScalaUtils & ScalaTest
// TEST 1 - FAIL
package foo
import org.scalautils._
import org.scalatest._
import Matchers._
import TypeCheckedTripleEquals._
class Work {
1 should === ("aoeu")
// Questions about \/ and Validation
object scalaz {
/*
Thanks for the great explanation, Tony. This explanation as well as the answers
that came back from Runar and Mark nudged my thinking in a different direction.
The feeling I get is that there's a monad inside of Validation trying to get out.
It looks like Validation would be monadic just from its shape, and users seem to
want to use them in for expressions. I now see that a Validation monad would need
@bvenners
bvenners / gist:8242691
Last active January 2, 2016 03:18
An example of giving one type multiple Applicative personalities
// Here is code that can be pasted into the REPL
import scalaz._
import Scalaz._
import org.scalautils._
def all[F[_]: Applicative, A](s: String, parsers: List[String => F[A]]): F[List[A]] = parsers.traverse(p => p(s))
implicit class BadDog[G, B](or: Or[G, B]) {
def badMap[C](bToC: B => C): G Or C = or.swap.map(bToC).swap
}
trait LowPriorityImplicit {
@bvenners
bvenners / gist:8242014
Created January 3, 2014 17:17
First attempt at an applicative instance for accumulating Ors
// Here's code that can be pasted into an interpreter sesssion:
import scalaz._
import Scalaz._
import org.scalautils._
def all[F[_]: Applicative, A](s: String, parsers: List[String => F[A]]): F[List[A]] = parsers.traverse(p => p(s))
implicit class BadDog[G, B](or: Or[G, B]) {
def badMap[C](bToC: B => C): G Or C = or.swap.map(bToC).swap
}
def toIntE(s: String): Int Or ErrorMessage = attempt(s.toInt).badMap(_.getMessage)
def one: String => (Int Or List[ErrorMessage]) = s => toIntE(s).badMap(List(_))
@bvenners
bvenners / gist:8029009
Created December 18, 2013 20:06
Adding a few &&'s to get a pure variant of an asynchronous test
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.cluster
import scala.collection.immutable.SortedSet
import com.typesafe.config.ConfigFactory
import akka.remote.testkit.MultiNodeConfig
import akka.remote.testkit.MultiNodeSpec
import akka.testkit._
@bvenners
bvenners / gist:8025646
Created December 18, 2013 16:49
Chaining side effect checks in a test with a result type
{
// do first step
// pure assert first side effect
} && {
// do second step
// pure assert second side effect
} && {
// do third step
// pure assert third side effect
}
@bvenners
bvenners / gist:7965105
Created December 14, 2013 21:28
How to override tags to tag scopes in ScalaTest 2.0
import org.scalatest._
import Matchers._
import tagobjects._
class ExampleSpec extends FreeSpec {
override lazy val tags: Map[String, Set[String]] =
super.tags.transform {
case (testName, tagsSet) if testName startsWith "scope1" => tagsSet + "Scope1"
case (testName, tagsSet) if testName startsWith "scope2" => tagsSet + "Scope2"