Parsing Json with the Play Framework

This article discusses different common scenarios for JSON parsing and conversion, useful when working with the Play Framework library. All the examples in the article, use one or more of the following library imports: import play.api.libs.functional.syntax._ import play.api.libs.json.{Format, JsError, JsNull, JsPath, JsResult, JsString, JsSuccess, JsValue, Json, OFormat, Reads} The first import is required to use the special and and or functions found through the examples below. JSON that matches property names The first snippet allows us to parse different basic data types from a json-formatted input.

Scala Lectures

A curated list of Scala lectures on functional programming and design patterns. Functional programming design patterns Wlaschin, Scott. “Functional programming design patterns.” 30 Aug 2015, youtu.be/E8I19uA-wGY In object-oriented development, we are all familiar with design patterns such as the Strategy pattern and Decorator pattern, and design principles such as SOLID. The functional programming community has design patterns and principles as well. This talk will provide an overview of some of these, and present some demonstrations of FP design in practice.

Using Cats Effect IO with the Play Framework

Cats Effect IO operations have to be performed at the highest level possible. So when working with the Play Framework the solution is to execute unsafeToFuture() in the controller methods. def get = Action.async { implicit request => service.get().map { items => Ok(Json.toJson(items)) }.unsafeToFuture() } We can as well write an Action implementation that automatically calls unsafeToFuture: object IOHttp { implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) { import cats.effect.implicits._ def asyncF[F[_] : Effect](cb: R[B] => F[Result]): Action[B] = ab.

Cats Effect IO - Retry with backoff pattern

Scala example for using the retry-with-backoff pattern with Cats Effect IO. import cats.effect.{IO, Timer} import scala.concurrent.duration._ def retryWithBackoff[A](ioa: IO[A], initialDelay: FiniteDuration, maxRetries: Int) (implicit timer: Timer[IO]): IO[A] = { ioa.handleErrorWith { error => if (maxRetries > 0) IO.sleep(initialDelay) *> retryWithBackoff(ioa, initialDelay * 2, maxRetries - 1) else IO.raiseError(error) } } References Example: Retrying with Exponential Backoff

Quick notes on functors

Functors A functor is a design pattern that allows us to apply a function to a contextualized (wrapped) type. This is accomplished by implementing the map function: fmap :: (a -> b) -> f a -> f b The function definition receives two parameters: a function that takes an instance of A and returns an instance of B and a contextualized (wrapped) instance of A (F[A] in scala or f a in haskell), and returns a contextualized instance of B.

Notes on lazy evaluation

Lazy evaluation is a strategy that delays expression evaluation until their value is needed. It also avoids repeated evaluations by returning previously computed results by storing them in a lookup table. Why isn’t lazy evaluation used everywhere? Lazy evaluations are not used everywhere (not used in every software currently produced) because of the following reasons: Lazy evaluation requires book-keeping overhead - you have to know if it’s been evaluated yet and such things.

Generate a random string using the console

Generate a random string using the command line: cat /dev/urandom | tr -cd [:graph:] | head -c 32 On OSX, the previous command might return a tr: Illegal byte sequence error, so try: export LC_ALL=C; cat /dev/urandom | tr -cd [:graph:] | head -c 32

How to Set Up DynamoDB for Local Development

In this article we are going to set up dynamodb on our local development environment. A short description of a couple of easy-to-use management tools is included. DynamoDB Start by downloading the DynamoDB development version. Uncompress the file and create a launch script with the following contents: #!/bin/sh java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -port 8000 The script will launch dynamo on port 8000 and will create a file called shared-local-instance.

Monoid definition and examples

We are going to cite the definition of a Monoid found on this post: Conceptually, a monoid is anything that: - Has a "zero-value": `mempty`, e.g., `0` - Can be "appended" together: `mappend`, e.g., `+` - Has an identity with the zero-value: `x + 0 == x, 0 + x == x` - Is associative: `x + y + z == (x + y) + z == x + (y + z)` This isn’t limited to just arithmetic with operators like +.

Creating an sbt project from scratch

Files and project structure sbt projects use the same structure as Maven. Creating a project from scratch involve the following steps: mkdir some-new-project cd some-new-project mkdir -p src/{main,test}/{resources,scala} mkdir project Create a new file called build.sbt and add the following contents: name := "SomeNewProject" version := "1.0" scalaVersion := "2.13.5" Our final folder structure has to look like this: . ├── build.sbt ├── project └── src ├── main │ ├── resources │ └── scala └── test ├── resources └── scala Done.