Skip to content

Commit

Permalink
Add Forall instance for proper collections
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed May 14, 2015
1 parent f4add39 commit 3ae23ef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ initialCommands := """
import eu.timepit.refined.char._
import eu.timepit.refined.collection._
import eu.timepit.refined.numeric._
import eu.timepit.refined.string._
import shapeless.nat._
import shapeless.tag.@@
"""
Expand Down Expand Up @@ -75,5 +74,5 @@ pomExtra :=

site.settings
site.includeScaladoc()
ghpages.settings
//ghpages.settings
git.remoteRepo := "git@github.com:fthomas/refined.git"
4 changes: 2 additions & 2 deletions project/plugin-ghpages.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven"
//resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven"

addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.5.3")
//addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.5.3")
8 changes: 7 additions & 1 deletion src/main/scala/eu/timepit/refined/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ object collection {
def show(t: T): String = s"isEmpty($t)"
}

implicit def forallPredicate[P, T, A](implicit p: Predicate[P, A], ev: T => TraversableOnce[A]): Predicate[Forall[P], T] =
implicit def forallPredicate[P, A, T[A] <: TraversableOnce[A]](implicit p: Predicate[P, A]): Predicate[Forall[P], T[A]] =
new Predicate[Forall[P], T[A]] {
def isValid(t: T[A]): Boolean = t.forall(p.isValid)
def show(t: T[A]): String = t.toSeq.map(p.show).mkString("(", " && ", ")")
}

implicit def forallPredicateView[P, A, T](implicit p: Predicate[P, A], ev: T => TraversableOnce[A]): Predicate[Forall[P], T] =
new Predicate[Forall[P], T] {
def isValid(t: T): Boolean = t.forall(p.isValid)
def show(t: T): String = t.toSeq.map(p.show).mkString("(", " && ", ")")
Expand Down
13 changes: 13 additions & 0 deletions src/test/scala/eu/timepit/refined/CollectionSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.timepit.refined

import eu.timepit.refined.collection._
import eu.timepit.refined.numeric._
import org.scalacheck.Prop._
import org.scalacheck.Properties
import shapeless.nat._

class CollectionSpec extends Properties("collection") {
property("Exists[Equal[_]]") = forAll { (l: List[Int]) =>
Predicate[Exists[Equal[_1]], List[Int]].isValid(l) == l.contains(1)
}
}

0 comments on commit 3ae23ef

Please sign in to comment.