GithubHelp home page GithubHelp logo

scala-sugar's Introduction

Scala Sugar

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.

Implicits

Where possible, implicits are implemented using value classes to reduce overhead.

Generics

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

Operators

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

Regular expressions

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")

Scaladoc

The latest API Scaladoc is here.

License

scala-sugar is Copyright © 2015 Simeon Simeonov and Swoop, Inc. It is free software, and may be redistributed under the terms of the LICENSE.

scala-sugar's People

Contributors

edgecaseberg avatar nermin avatar ssimeonov avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

scala-sugar's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.