Skip to content

Instantly share code, notes, and snippets.

@OleTraveler
Created November 23, 2011 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OleTraveler/1389344 to your computer and use it in GitHub Desktop.
Save OleTraveler/1389344 to your computer and use it in GitHub Desktop.
def lineToOffer(line: String, headers: List[String]) : Validation[NonEmptyList[String], Offer] = {
({commaSplit(_)} andThen {extractFields(_: List[String], headers)} apply line) :->
{(v) => Offer(v._1, v._2, v._3, v._4)}
}
def commaSplit(l: String): String => List[String] = l.split(",").toList
def extractFields(x: List[String], headers: List[String]): Validation[NonEmptyList[String], (String, String, String, String)] = {
notEmpty(x(headers.indexOf("offer_name")), "No offer name specified on line:" + x.mkString(","))) <|***|> (
notEmpty(x(headers.indexOf("email")), "No email specified on line:" + x.mkString(",")),
notEmpty(x(headers.indexOf("uuid")), "No uuid specified on line:" + x.mkString(",")),
notEmpty(x(headers.indexOf("reward_name")), "No uuid specified on line:" + x.mkString(","))
)
}
case class Offer(offerName: String, email: String, uuid: String, rewardName: String)
@LeifW
Copy link

LeifW commented Nov 23, 2011

You can leave off some of that syntax - e.g. write (g _ andThen f)(x)
Maybe write extractFields in curried form, with args reversed, e.g. extractFields(headers:List)(x:List) = ..., and then you could write (g _ andThen extractFields(headers))
Also, if you declare functions with val f = (x:...) =>... instead of def f(x:...) = ..., you can leave off the trailing _ in those cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment