Compared to coding in Java, coding in Scala is pretty sweet. Compared to coding in highly expressive languages such as Python and Ruby, Scala holds its own most of the time but not always. This library is a collection of useful extensions to the core Scala libraries (implicit and otherwise) that make programming in Scala even sweeter.
This project uses semantic versioning.
Where possible, implicits are implemented using value classes to reduce overhead.
import com.swoop.scala.sugar.Implicits._
// ap() allows arbitrary function chaining. The name comes from ap(plying) a function.
// This is similar to one use of Ruby's Object#try (the other being taken care of by Option).
// It is also convenient for injecting logic into a method chain, similar to Ruby's Object#tap.
5.ap(x => x + x)
// res0: Int = 10
5.ap{x => println(s"I got ${x}"); x}.toFloat
// I got 5
// res1: Float = 5.0
import com.swoop.scala.sugar.Implicits._
// The |> (pipe) operator is borrowed from F# (inspired by Unix pipes).
// It allows function composition to become akin to method chaining.
// x |> f |> g is equivalent to g(f(x))
def f(x: Int) = x * 10
def g(x: Int) = x + 10
g(f(5))
res0: Int = 60
5 |> f |> g
res1: Int = 60
import com.swoop.scala.sugar.Implicits._
// regex.test(str) checks for a partial match and returns a Boolean (inspired by JavaScript)
// This is considerably simpler than !regex.findFirstIn(str).isEmpty and semantically less
// awkward than regex.findFirstIn(str).isDefined.
"a+b+".r.test("xyz aabbb")
// res0: Boolean = true
// regex.matchIn(str) returns the first match in an object that makes optional extraction easy.
// str.extract(regex) is sugar for regex.matchIn(str)
val m = "aabb".extract("a+(b+)".r)
// m: com.swoop.scala.sugar.regex.MatchedInString = Some(aabb)
m.group(1).get
// res1: String = bb
"no match here".extract("a+(b+)".r).group(1).getOrElse("nope")
// res2: String = nope
// Compare the readability & convenience of method chaining above with the usual approach
"a+(b+)".r.findFirstMatchIn("no match here") match {
case Some(m) => m.group(1)
case None => "nope"
}
"a+(b+)".r.findFirstMatchIn("no match here").map(_.group(1)).getOrElse("nope")
The latest API Scaladoc is here.
scala-sugar
is Copyright © 2015 Simeon Simeonov and Swoop, Inc. It is free software, and may be redistributed under the terms of the LICENSE.