Skip to content

Instantly share code, notes, and snippets.

View bvenners's full-sized avatar

Bill Venners bvenners

View GitHub Profile
@bvenners
bvenners / gist:7dbc093d36bd973c993e
Last active August 29, 2015 14:05
Adding stricter cons and contain operators to covariant lists via an implicit class
scala> implicit class BVList[A](xs: List[A]) {
| def :=:(x: A): List[A] = x :: xs
| def containz(elem: A): Boolean = xs.contains(elem)
| }
defined class BVList
scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)
scala> 5.0 :: xs
case class Plan(estimate: Int, strategy: String)
case class Concept(idea: String)
case class Project(concept: Concept, plan: Plan)
def workOn(project: Project): String =
project match {
@bvenners
bvenners / gist:60b53647c9031b56b691
Created August 7, 2014 15:57
Disabling implicit conversions around equality comparisons with Scalaz's Equal and Scalactic
scala> import scalaz._
import scalaz._
scala> import Scalaz.{ToEqualOps => _, _}
import Scalaz.{ToEqualOps=>_, _}
scala> import equalitydemo.StrictScalazEquality._ // Import a "stricter" policy
import equalitydemo.StrictScalazEquality._
scala> 1L === 1 // Now implicit conversions aren't used to fix an quality comparison
@bvenners
bvenners / gist:eb746eb23af8f8373d74
Last active August 29, 2015 14:04
Types involved in ScalaTest matcher expressions
//
// Most ScalaTest matcher syntax is enabled by the existence of typeclasses. For example,
// 'have length 3' will work for any type T for which a Length[T] instead is available.
// The result type of 'have length 3' is a MatcherFactory1[Any, Length].
//
scala> val haveLength3 = have length 3
haveLength3: org.scalatest.matchers.MatcherFactory1[Any,org.scalatest.enablers.Length] = have length 3
//
// A MatcherFactory1[Any, Length]'s 'matcher' method can produce a Matcher[T] for any
@bvenners
bvenners / gist:34c6ca9787cc1ee146cf
Created July 29, 2014 04:40
Now playing in ScalaTest master...
//
// This is not yet at a good point for a milestone release, but I've been doing a lot of
// work to try and improve equality and containership checks in ScalaTest and Scalactic. One part of it is
// that I figured out how to get equal, be, and contain matchers to participate in the kind of type checking
// that previously only worked with === and should ===. The difficulty was that equal, be, and contain
// can be used with logical operators and, or, and not (whereas === did not). Here are some examples:
//
scala> 1 shouldBe 1L
@bvenners
bvenners / build.sbt
Created July 8, 2014 03:11
Is there any way to get the Scala compiler to infer that TYPECLASS1 is The[String]#Equaling in dude(egual("hi"))
organization := "com.example"
version := "0.1"
scalaVersion := "2.11.1"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.0"
feature("TV power button") {
scenario(
"User presses power button when TV is off"
Given "a TV set that is switched off"
When "the power button is pressed"
Then "the TV should switch on"
) {
// Given
val tv = new TVSet
assert(!tv.isOn)
@bvenners
bvenners / gist:9405709
Last active August 29, 2015 13:57
ScalaTest's Inspectors and Inspector Shorthands are now more open minded
// In ScalaTest 2.1.0, the inspector and inspector shorthand constructs accept more types.
// Previously they just worked on Scala collections (though through implicit conversions
// you could pass strings and arrays to them). Now they work on any type C for which an implicit
// org.scalatest.enablers.Collecting[C] is available, which out-the-box includes Scala and Java
// collections, strings, and arrays. Here are a few examples:
scala> import org.scalatest._
import org.scalatest._
scala> import MustMatchers._
@bvenners
bvenners / gist:9379784
Last active August 29, 2015 13:57
In ScalaTest 2.1.0, String beauty is no longer just skin deep
// In ScalaTest/ScalaUtils 2.1.0 the Prettifier, which in 2.0 just prettified objects at the top level,
// has been enhanced to prettify objects contained inside other objects, including Scala and Java
// collections, Option, Either, and Try. Here's an example:
scala> val actual = Set(List(Map(1 -> Some("2"), 2 -> Some("3"))))
actual: scala.collection.immutable.Set[List[scala.collection.immutable.Map[Int,Some[String]]]] =
Set(List(Map(1 -> Some(2), 2 -> Some(3))))
scala> val expected = Set(List(Map('1' -> Some(2), '2' -> Some(3))))
expected: scala.collection.immutable.Set[List[scala.collection.immutable.Map[Char,Some[Int]]]] =
@bvenners
bvenners / gist:8814587
Created February 4, 2014 23:30
Simplicity saves time
import org.specs2.ScalaCheck
import org.specs2.mutable._
case class Box(first: String)
class FooSpec extends Specification with ScalaCheck {
"this should compile without obfuscated error messages" in {
check { (left: String, right: String) =>
import java.lang.String
if (false)