beamly.core.lang
The Beamly core scala language enhancements. This module only depends on the core Scala libraries.
Getting beamly.core.lang
Add this sbt dependency:
"com.beamly" %% "beamly-core-lang" % "0.5.0"
or maven dependency:
<dependency>
<groupId>com.beamly</groupId>
<artifactId>beamly-core-lang_${scala.binary}</artifactId>
<version>0.5.0</version>
</dependency>where the scala.binary variable is either 2.10 or 2.11
Maven Central Repository details
Maturity
We are rapidly approaching 1.0, feel free to provide feedback or raise issues through the github issue tracker.
Quick start
The scaladocs provide full details, but here's a brief summary of some of the key benefits and features.
String utilities
isBlank/nonBlankmethods to check if a string is empty or contains only whitespacetoSnakeCasewhich converts a string to snake case, eg: 'nextTvEpisode' to 'next_tv_episode'to<PrimitiveType>Optionwhich returns a primitive type from a string, converting errors intoNone
Either utilities
map/flatMapmaps Right valuesrecover/flatRecover/recoverWithconverts a Left to a derivation of Right
Option utilities
onSome/onNoneruns 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 anAtomicReferenceuntil the update is successful
PartialFunction utilities
andThenWithContextthe same as 'andThen', except the input object is passed as well as the resultcomposePFthe same as 'compose' but returns a PartialFunction
Map utilities
mergeValuesmerges 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 promisegetorget(10 seconds)awaits the result of a future with the default timeout of 5 seconds (Await.result)awaitorawait(10 seconds)awaits the future with the default timeout of 5 seconds (Await.ready)fold/flatFoldmaps successful or failed values to a newFuturejoinmethod on aFuture[Future[A]],Future[Either[Throwable, A]]orFuture[Try[A]]flattens the result to aFuture[A]
Handling a Future[Option[A]]
mapOpt[B](f: A => B): allows you to simply map aFuture[Option[A]]without having to callaFuture map { _ map f }with the far simpler formaFuture mapOpt fflatMapOpt[B](f: A => Future[Option[B]]): allows you to simply flatMap aFuture[Option[A]]without having to callaFuture flatMap { _ map f getOrElse futureNone }with the far simpler formaFuture flatMapOpt forElse[B >: A](other: => Future[Option[B]])provides an alternativeFuture[Option[B]]if this result isFuture(None)
Handling a Future[Traversable[A]]
mapTraversablemaps the values in the collection into a newFuturewithout having to callaFuture map { _ map f }
License
This software is licensed under the Apache 2.0 License.
Issue Reporting
To report issues with this software, please raise them via the github reporting mechanism.
Contributions
Currently we are not able to accept pull requests, if you wish to contribute please contact us via github.