Our Common Scala Library
Vlad Patryshev, HealthExpense, 2016
Useful Scala Features
Packages
Important Classes
Result[T]
It’s a monad, and an applicative functor.
val myResult: Result[A] = for {
x <- Result.valueOf(suspiciousEvaluations) orCommentTheError “oops…”
(y,z) <- Result(optionY) andAlso Result(optionZ)
} yield someCalculation(x,y,z)
Props
Smart and nutritious Map[String, String].
Money (dollars only)
dollars(“$-123.46”): Result[BigDecimal]
dollarsOrZero(“$$$”): BigDecimal
DateAndTime, DateOfBirth
Linguist
Linguist Samples
firstNameOf _ named "firstNameOf" maps(
" Juan Antonio Samaranch\n the third " -> "Juan",
"Anna-Maria Kegelmeister," -> "Anna-Maria",
"Patryshev, Vlad" -> "Vlad",
" X\t\t" -> "X",
"the end" -> "the"
)
lastNameOf _ named "lastNameOf" maps(
"Patryshev, Vlad" -> "Patryshev",
" The rest is silence." -> "Silence",
"\n\n\t\fAnna-Maria Kegelmeister," -> "Kegelmeister",
"...Bond, James Bond!" -> "Bond",
"Martin Luther King Jr." -> "King",
"Bill Gates the 3rd" -> "Gates",
" X\t\t" -> ""
)
fullNamesMatch _ passesOn(
("Martin Luther King Jr.", "MARTIN KING"),
("JOSE ARCADIO BUENDIA", "Jose-Arcadio Buendia"),
("JOSE BUENDIA", "Jose-Arcadio Buendia"),
("ARCADIO BUENDIA", "Jose-Arcadio Buendia"),
("JOSE ARCADIO BUENDIA", "José Buendìa"),
("SAUL Babilona", "Saúl Babiloña"),
("Saúl Babiloña", "SAUL Babilona"),
("KADAR JANOS", "Kádár János"),
("Stephen Ng", " stephen ng "),
("JULIAXYGREK", "Julia X Ygrek"),
("Do Young O", " do o"),
("Bill Gates the 3rd", "BILL GATES"),
("Martin Luther King Jr.", "MARTIN JR")
)
Strings
Types
Ops
IO
val summary = for {
sourceScript <- IO.find("aws_parse.sh")
content <- Result.forValue(Source.fromFile(sourceScript).getLines().toList)
} yield ...
"""object main {
def main(args) { println("Hello") }
}""" #> "main.scala"
Reference
https://github.com/HealthExpense/Backend2/tree/master/src/main/scala/com/healthexpense/common
(and there’s more: see com.healthexpense.Backend2.data, com.healthexpense.Backend2.tools, etc)