avatar

Andres Jaimes

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

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: import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} final case class FutureEither[+E, +A](value: Future[Either[E, A]]) { def map[B](f: A => B)(implicit ec: ExecutionContext): FutureEither[E, B] = FutureEither(value map { case Right(a) => Right(f(a)) case Left(e) => Left(e) }) def flatMap[EE >: E, B](f: A => FutureEither[EE, B])(implicit ec: ExecutionContext): FutureEither[EE, B] = FutureEither(value flatMap { case Right(a) => f(a).

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

AES Encryption with Base64 encoding

We start by adding our imports. import java.nio.charset.StandardCharsets import java.security.Key import java.util.Base64 import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec For this example we’re going to use the CBC (Cipher Block Chaining) variation of AES. val Algorithm: String = "AES" val Transformation: String = "AES/ECB/PKCS5Padding" def aeskey: String => Key = key => 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: #!/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.

Sending a message using telegram and scala

In this article we are going to use scala to send a message via Telegram. The process is very simple and pretty much involves setting up a bot and making a GET request to Telegram’s api. The Telegram app on the phone has more features than its desktop counterpart. I recommend you to use the phone version to make sure you have access to all the required features. On Telegram 1.

How to create a self-contained jar file (fat jar) using sbt

A fat jar is a jar file that contains any dependencies required by a Java or Scala program. Deployment becomes an easy task if we only have to deal with one single file. In this article we’ll go through the steps required to create a fat jar using Scala and sbt. The following procedure has been tested using Scala 2.12.8, sbt 1.2.8 and jdk 11. 1. project/assembly.sbt Create project/assembly.