By Andrew (gus) Gustafson October 13, 2014

Official Beamly Editor

We’ve been building reactive services in Scala for more than 3 years, and found (especially early on) that both standard libraries and those in the community didn’t quite meet our needs. We feel Beamly core fills those gaps that we found, and helps to make Scala an even better language to work with. Over time, we’ve built up a bunch of scala utilities to make our lives a little simpler and we’re releasing the first group of them today. They’re all pretty simple, low-level implicits and wrappers. Over the next weeks and months we’ll open source the more complex (and probably more useful and interesting) components.

It’s currently available in the central maven repo for scala 2.10 and 2.11.

Overview of benefits

The scaladocs provide full details, but here’s a brief summary of some of the key benefits and features.

String utilities

  • isBlank/nonBlank to check if a string is empty or contains only whitespace
  • toSnakeCase which converts a string to snake case, eg: ‘nextTvEpisode’ to ‘next_tv_episode’
  • to<PrimitiveType>Option which returns a primitive type from a string, converting errors into None

Either utilities

  • map/flatMap maps Right values
  • recover/flatRecover/recoverWith converts a Left to a derivation of Right

Option utilities

  • onSome/onNone runs a side-effecting function and returns the original result; useful for logging

AtomicReference utilities

  • update(f: A => A) attempts to rerun a function to update an AtomicReference until the update is successful

PartialFunction utilities

  • andThenWithContext the same as ‘andThen’, except the input object is passed as well as the result
  • composePF the same as ‘compose’ but returns a PartialFunction

Map utilities

  • mergeValues merges 2 maps together, using the provided merge function to create a new map value for a key

Future utilities

  • promising[A](f: Promise[A] => Any) creates a promise, uses the provided function to fulfil the promise and then returns the future from the promise
  • get or get(10 seconds) awaits the result of a future with the default timeout of 5 seconds (Await.result)
  • await or await(10 seconds) awaits the future with the default timeout of 5 seconds (Await.ready)
  • fold/flatFold maps successful or failed values to a new Future
  • join method on a Future[Future[A]], Future[Either[Throwable, A]] or Future[Try[A]] flattens the result to a Future[A]

Handling a Future[Option[A]]

  • mapOpt[B](f: A => B): allows you to simply map a Future[Option[A]] without having to call aFuture map { _ map f }
  • with the far simpler form aFuture mapOpt f

  • flatMapOpt[B](f: A => Future[Option[B]]): allows you to simply flatMap a Future[Option[A]] without having to call
  • aFuture flatMap { _ map f getOrElse futureNone } with the far simpler form aFuture flatMapOpt f

  • orElse[B >: A](other: => Future[Option[B]]) provides an alternative Future[Option[B]] if this result is Future(None)

Handling a Future[Traversable[A]]

  • mapTraversable maps the values in the collection into a new Future without having to call aFuture map { _ map f }