Skip to content

Instantly share code, notes, and snippets.

View bvenners's full-sized avatar

Bill Venners bvenners

View GitHub Profile
@bvenners
bvenners / gist:de2a55f749dda22cc622
Last active August 29, 2015 14:07
Safe contains and *indexOf* methods for Seqs and Arrays in Scalactic 3.0
//
// This demos type errors for safe variants of contains, indexOf, lastIndexOf, indexOfSlice, and lastIndexOfSlice
// added to Scala Seqs and Arrays via an implicit conversion. These do rely on the trick of implicitly converting
// away the covariance of the type parameter, because in Scala 2.10 that is necessary. But in Scala 2.11, it is not
// necessary, because the compiler will disinguish beween implicits for covariant and invariant Seqs. Nevertheless
// I think provides value so I've done it this way by commenting out the implicit that doesn't work in 2.10:
//
// https://github.com/scalatest/scalatest/blob/master/src/main/scala/org/scalactic/SafeSeqs.scala#L25
//
// Someday when we drop support for Scala 2.10 I can uncomment that implicit and then this will no longer be
@bvenners
bvenners / gist:d07ca7bf2cf8021aab63
Last active August 29, 2015 14:07
Demo of EquaSets, sets based on alternate equalities, in Scalactic 3.0
//
// This gist demos the EquaSet type coming in Scalactic 3.0, which is a set that
// can use an alternate equality for determining set membership.
//
// The Scaladoc for this is version of Scalactic here:
//
// http://www.artima.com/docs-scalatest-3.0.0-SNAP1/#package
//
// A screencast that includes an EquaSets demo is here:
//
@bvenners
bvenners / gist:eb4d413d007e67f84102
Created October 20, 2014 17:24
Typesafe contains and *indexOf* methods on covariant Seq-like class
//
// This demos type errors for contains, indexOf, lastIndexOf, indexOfSlice, and lastIndexOfSlice
// on org.scalactic.Every, which is a Seq-like object: a covariant ordered collection. (It does
// not extend scala.collection.immutable.Seq because it enforces an invariant of non-emptiness, which
// is incompatible with Seq.) These methods are declared on Every, not added with an implicit conversion.
// For example, here is the declaration of contains:
//
// https://github.com/scalatest/scalatest/blob/release-3.0.0-SNAP1-for-scala-2.11-and-2.10/src/main/scala/org/scalactic/Every.scala#L299
//
// Scaladoc for this version of Every is here:
@bvenners
bvenners / gist:9596bfc5712c8061762e
Last active August 29, 2015 14:06
Using Scalactic Or and Validation
scala> import org.scalactic._
import org.scalactic._
scala> def reasons(x: Int): Int Or ErrorMessage =
| for {
| y <- Good(x) filter { i => if (i > 0) Pass else Fail(s"$i was <= 0") }
| z <- Good(y / 2) filter { i => if (i > 1) Pass else Fail(s"$y / 2 was <= 1") }
| } yield z
reasons: (x: Int)org.scalactic.Or[Int,org.scalactic.ErrorMessage]
@bvenners
bvenners / gist:f038d946adab18b9cb78
Created September 16, 2014 00:28
Type checked contain matcher coming in ScalaTest 3.0
> console
[info] Compiling 1 Scala source to /Users/bv/nobkp/delus/redlady/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
import org.scalatest._
import org.scalactic._
import Matchers._
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
@bvenners
bvenners / gist:ba37a0ff963a0d4a0174
Created August 27, 2014 17:54
Arithmetic conversions in C
main()
{
if (1 == 1L)
printf("In C, 1 == 1L\n");
else
printf("In C, 1 != 1L\n");
if (1L == 1)
printf("In C, 1L == 1\n");
else
@bvenners
bvenners / gist:2e89ef80729cc9881a67
Created August 27, 2014 03:52
Scalactic's === will consistently either allow or not allow both A === B and B === A
scala> import CheckedEquality._
import CheckedEquality._
scala> 1 === "one"
<console>:20: error: types Int and String do not adhere to the type constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.EqualityConstraint[Int,String]
1 === "one"
^
scala> "one" === 1
<console>:20: error: types String and Int do not adhere to the type constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.EqualityConstraint[String,Int]
@bvenners
bvenners / gist:05fe6323a132b1c7d9eb
Last active August 29, 2015 14:05
ScalaTest brings 1/100th the number of implicits into scope compared to specs2
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> def countImplicits(tp: Type): Int = tp.members.count(_.isImplicit)
countImplicits: (tp: reflect.runtime.universe.Type)Int
scala> countImplicits(typeOf[org.specs2.mutable.Specification])
res0: Int = 205
scala> countImplicits(typeOf[org.scalatest.Spec])
@bvenners
bvenners / gist:67cb4e8c0b285ca99fa3
Last active August 29, 2015 14:05
Getting a Scala compiler error when needed typeclass coherency is missing
scala> import org.scalactic._
import org.scalactic._
scala> val lower = EquiSets[String](StringNormalizations.lowerCased.toHashingEquality)
lower: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(lowerCased))
scala> val trimmed = EquiSets[String](StringNormalizations.trimmed.toHashingEquality)
trimmed: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(trimmed))
scala> val hiho = lower.EquiSet("hi", "ho")
@bvenners
bvenners / gist:97023424974b08adcafb
Created August 18, 2014 19:02
Adding stricter cons and contain operators to covariant lists via an implicit class, take 2
scala> implicit class BVList[A](xs: List[A]) {
| def :=:[B >: A](x: B)(implicit ev: B =:= A): List[B] = 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