avatar

Andres Jaimes

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: 1import play.api.libs.functional.syntax._ 2import 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. 1def get = Action.async { implicit request => 2 service.get().map { items => 3 Ok(Json.toJson(items)) 4 }.unsafeToFuture() 5} We can as well write an Action implementation that automatically calls unsafeToFuture: 1object IOHttp { 2 3 implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) { 4 5 import cats.

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:

1fmap :: (a -> b) -> f a -> f 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.

Future Either

This implementation is based on the work of Malcolm, and exists because it provides the cleanest use of similar monads. The base monad wraps an Either class in a Future class: 1import scala.concurrent.{ExecutionContext, Future} 2import scala.util.{Failure, Success, Try} 3 4final case class FutureEither[+E, +A](value: Future[Either[E, A]]) { 5 6 def map[B](f: A => B)(implicit ec: ExecutionContext): FutureEither[E, B] = 7 FutureEither(value map { 8 case Right(a) => Right(f(a)) 9 case Left(e) => Left(e) 10 }) 11 12 def flatMap[EE >: E, B](f: A => FutureEither[EE, B])(implicit ec: ExecutionContext): FutureEither[EE, B] = 13 FutureEither(value flatMap { 14 case Right(a) => f(a).

AES Encryption with Base64 encoding

We start by adding our imports. 1import java.nio.charset.StandardCharsets 2import java.security.Key 3import java.util.Base64 4import javax.crypto.Cipher 5import javax.crypto.spec.SecretKeySpec For this example we’re going to use the CBC (Cipher Block Chaining) variation of AES. 1val Algorithm: String = "AES" 2val Transformation: String = "AES/ECB/PKCS5Padding" 3 4def aeskey: String => Key = key => 5 new SecretKeySpec(key.getBytes, Algorithm) Our encrypt function receives a key and some text to encrypt. The function will return a base64-encoded string if everything goes ok.

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: 1#!/bin/sh 2 3java -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 +.