Skip to content

Instantly share code, notes, and snippets.

@oxbowlakes
Created November 10, 2010 09:17
Show Gist options
  • Save oxbowlakes/670588 to your computer and use it in GitHub Desktop.
Save oxbowlakes/670588 to your computer and use it in GitHub Desktop.
object OptionGolf {
import scalaz._
import Scalaz._
val moviel = Map("title" -> "South Park",
"user" -> "Terrence",
"rating" -> "3")
val incompleteMovie = Map("name" -> "Jaws")
case class MovieReview(revTitle: String, revUser: String, revReview: String)
/*
* Channing Walton's solution
*/
def rev3(movie: Map[String,String]): Option[MovieReview] =
movie.get("title") <*> (movie.get("user") <*> (movie.get("rating") ∘ MovieReview.curried))
/*
* Slightly different solution!
*/
def rev4(movie: Map[String,String]) =
(movie.get("title") <|**|> (movie.get("user"), movie.get("rating"))) ∘ MovieReview.tupled
def main(args: Array[String]) {
println(rev4(moviel))
println(rev4(incompleteMovie))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment