Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add Box.forall. #1502

Merged
merged 3 commits into from Mar 16, 2014
Jump to file or symbol
Failed to load files and symbols.
+33 −4
Split
View
@@ -192,4 +192,9 @@ Christopher Webster
### Email: ###
cwebster93 at gmail .. com
+### Name: ###
+Vasya Novikov
+
+### Email: ###
+n1dr+cm3053lift@yaaandex.com (replace "aaa" with "a")
@@ -310,11 +310,17 @@ sealed abstract class Box[+A] extends Product with Serializable{
/**
* Determine whether this Box contains a value which satisfies the specified predicate
- * @return true if this Box's value satisfies the specified predicate
+ * @return true if this Box does contain a value and it satisfies the predicate
*/
def exists(func: A => Boolean): Boolean = false
/**
+ * Determine whether all Box values satisfy the predicate
+ * @return true if the Box is empty, or if Box's value satisfies the predicate
+ */
+ def forall(func: A => Boolean): Boolean = true
+
+ /**
* Creates a Box if the current Box is Full and the value does not satisfy the predicate, f.
*
* @param f the predicate used to test value.
@@ -546,6 +552,8 @@ final case class Full[+A](value: A) extends Box[A]{
override def exists(func: A => Boolean): Boolean = func(value)
+ override def forall(func: A => Boolean): Boolean = func(value)
+
override def filter(p: A => Boolean): Box[A] = if (p(value)) this else Empty
override def foreach[U](f: A => U): Unit = f(value)
@@ -106,12 +106,18 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
"return itself when or'ed with another Box" in {
Full(1) or Full(2) must_== Full(1)
}
- "define an 'exists' method returning true if the Box value verifies the function" in {
+ "define an 'exists' method returning true if the Box value satisfies the function" in {
Full(1) exists {_ > 0} must beTrue
}
- "define an exists method returning false if the Box value doesn't verify the function" in {
+ "define an exists method returning false if the Box value doesn't satisfy the function" in {
Full(0) exists {_ > 0} must beFalse
}
+ "define a forall method returning true if the Box value satisfies the function" in {
+ Full(1) forall {_ > 0} must beTrue
+ }
+ "define a forall method returning false if the Box value doesn't satisfy the function" in {
+ Full(0) forall {_ > 0} must beFalse
+ }
"define a 'filter' method, returning a Full Box if the filter is satisfied" in {
Full(1) filter {_ > 0} must_== Full(1)
}
@@ -239,6 +245,10 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
val empty: Box[Int] = Empty
empty exists {_ > 0} must beFalse
}
+ "define a 'forall' method returning true" in {
+ val empty: Box[Int] = Empty
+ empty forall {_ > 0} must beTrue
+ }
"define a 'filter' method, returning Empty" in {
val empty: Box[Int] = Empty
empty filter {_ > 0} must beEmpty
@@ -304,6 +314,12 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
"create a new failure with a chained message if asked for its status with the operator ?~!" in {
Failure("error", Empty, Empty) ?~! "error2" must_== Failure("error2", Empty, Full(Failure("error", Empty, Empty)))
}
+ "return false for exist method" in {
+ Failure("error", Empty, Empty) exists {_ => true } must beFalse
+ }
+ "return true for forall method" in {
+ Failure("error", Empty, Empty) forall {_ => false } must beTrue
+ }
}
"A Box equals method" should {
@@ -49,7 +49,7 @@ object LoggingAutoConfigurer {
private def findTheFile(files: String*): Box[(java.net.URL)] = {
val namesToTry = Props.toTry.flatMap(f => files.toList.map(file => f()+file))
- first(namesToTry) (name => tryo(getClass.getResource(name)).filter(_ ne null).map(s => s))
+ first(namesToTry) (name => tryo(getClass.getResource(name)).filter(_ ne null))
}
def apply(): () => Unit = () => {