Skip to content
Missing methods on (T => Boolean) functions.
Scala
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
project
src
.gitignore
README.md

README.md

Build Status

predicates

Wouldn't it be so nice if we can do something like (validUserName and validPassword and validCaptcha) thenDo Success where validUserName, validPassword and validCaptcha are functions of type T => Boolean. predicates helps you do exactly that. It provides simple wrapper methods to pimp your T => Boolean functions with and, or and not so that we can write more expressive[subjective] code.

Dependencies

predicates is published for the following scala versions: 2.11, 2.10, 2.9.3, 2.9.2, 2.9.1.

For Maven

<dependency>
  <groupId>in.ashwanthkumar</groupId>
  <artifactId>predicates_${scala.version}</artifactId>
  <version>0.0.2</version>
</dependency>

For SBT,

libraryDependencies += "in.ashwanthkumar" %% "predicates" % "0.0.2"

Usage

I prefer to use predicates while writing data validators.

import in.ashwanthkumar.predicates._

case class Model(id: Int, name: String)

class ModelValidator {
    def hasId = (model: Model) => model.id > 0
    def hasName = (model: Model) => model.name != null && !model.name.isEmpty

    def all = hasId and hasName
    def any = hasId or hasName

    def validate = (all thenDo Success) orElse (not(all) thenDo Failure)
}

Dev

$ git clone https://github.com/ashwanthkumar/predicates.git
$ cd predicates
$ sbt test

License

Apache 2.0

You can’t perform that action at this time.