• Moving My Activator Templates To Gitter8

    I had few activater templates for a while now that I use to start different types of Scala projects. I open sourced those and made available via Github and activator website. There was lots of traction from the community and since Activator is deprecated now I decided to take those and convert to Gitter8 templates so sbt new can be used to start projects. The new templates work with sbt 1 and all the dependencies, including sbt plugins in the projects, are upgraded.

    read more
  • Writing Aws Lambdas In Scala

    AWS Lambda supports writing handlers in Nodejs, Python and Java. The first two are really easy - if you don’t have any external dependencies and script is easy to write, you can even write your lambda in AWS console. However when it comes to Java, things get a little more complicated than that (not even talking about Scala yet.) So at the very least you’ll need to create a new project, know what jar dependencies to add for lambda, implement the handler method in a class that potentially implements some interface, know how to generate a deployment package (fat jar or a zip file for java projects), upload to S3 (since that jar would have a decent size), etc. If I want to implement my lambda in Scala now, I would have to deal with all those complexities for Java projects and more - such as more dependencies, how to write an actual Scala code (not Java in Scala syntax), interoperability, immutability, built in features like case classes, futures, etc. I thought it’s not fair for Scala developers and things can be much simpler (maybe not Nodejs simple) but at least it has to be simpler than doing in Java, since we all know Scala has more things to make developer’s job easier.

    read more
  • Akka Http Request Throttling

    Akka Http is a great framework for rest microservices and here at Ad Hoc Labs we’re slowly migrating from Spray to Akka Http. Since we use Spray extensively we ended up having utility classes to complement some functionality here and there or shared in a common jar with other utility classes. With Akka Http we want to keep things little more clean and reduce boilerplate with high level constructs. We decided to start a new project named akka-http-contrib that will host all akka http functionality that we’re building that’s not already in akka-http project. We recently open sourced it since other community members will find it helpful and hopefully will contribute also.

    First functionality we needed for a new project we started with Akka Http was to introduce throttling for some endpoints. We didn’t want to write boilerplate for each endpoint we want to introduce throttling for but instead we wanted to be able to introduce throttling for any endpoint by adding it to typesafe config. Basically we wanted a directive that will wrap the routes and when request comes in, depending on the config and thresholds, either let the route execute or complete the request with 429 - Too Many Requests and prevent the route from being executed. This at high level, which should handle most of the use cases but we also wanted to have it extensible enough that whenever there is a special usecase it still can be handled by writing little code.

    read more
  • Ammonite Modules

    Ammonite is such a great project, especially Ammonite-REPL. It brings many great things to scala console: syntax highlighting, better editing, dynamically loading dependencies, loading scripts, etc. It comes very handy if you want to play with a some library without creating an sbt project. Or you want to try something out without necessarily loading IntelliJ. I happen to do it often and decided to start an open source project called ammonite-modules. The idea is to have pre-defined modules you can load in Ammonite Repl. Modules will have all the dependencies needed at latest versions and do some basic setup.

    read more
  • Scalaz Plus, PlusEmpty, IsEmpty, ApplicativePlus and MonadPlus

    read more
  • Scalaz Monad

    Monad is Applicative and Bind. With that we deal with:

    def ap[A,B](fa:  F[A])(f:  F[A  B]): F[B]
    def map[A, B](fa: F[A])(f: A  B): F[B]

    from Apply

    def point[A](a: => A): F[A]
    override def map[A, B](fa: F[A])(f: A => B): F[B] = ap(fa)(point(f))

    from Applicative

    def bind[A, B](fa: F[A])(f: A => F[B]): F[B]
    override def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] = bind(f)(map(fa))

    from Bind.

    Note that in Applicative, map method is implemented using ap method and in Bind, ap is implemented using bind method. With that, two methods that are left to be implemented for a Monad are point and bind.

    read more
  • Scalaz Foldable

    Scalaz provides foldable to define all fold operations. It has Foldable[F[_]] trait with two foldMap and foldRight abstract methods:

    def foldMap[A,B](fa: F[A])(f: A  B)(implicit F: Monoid[B]): B
    def foldRight[A, B](fa: F[A], z:  B)(f: (A, => B) => B): B
    read more
  • Scalaz Comonad

    Comonad is a Cobind that also has copoint method. Scalaz defines Comonad[F[_]] with copoint abstract method.

    def copoint[A](p: F[A]): A

    Note that when point method of Monad turns A to F[A], copoint method of Comonad turns F[A] to A.

    read more
  • Scalaz Cobind

    Cobind is a Functor that also has cobind method. Scalaz defines Cobind[F[_]] trait with cobind abstract method.

    def cobind[A, B](fa: F[A])(f: F[A] => B): F[B]

    Having F[A] and mapping F[A] to B it returns F[B]. Note that unlike bind method in Bind functor, cobind takes F[A] => B instead of A => F[B] function.

    Since we have cobind method we can define cojoin method bases on it. Unlike to join in Bind, it turns F[A] to F[F[A]] instead of F[F[A]] to F[A].

    def cojoin[A](fa: F[A]): F[F[A]] = cobind(fa)(fa => fa)
    read more
  • Scalaz Bind

    Bind is an Apply that also has bind method. Scalaz defines Bind[F[_]] trait with bind abstract method.

    def bind[A, B](fa: F[A])(f: A => F[B]): F[B]

    Having F[A] and mapping A to F[A] it returns F[B].

    Since Bind is Apply it inherits all the methods that Apply offers plus it needs to provide implementation for map and ap methods. Note that Bind implements ap using bind and map methods:

    override def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] = {
        lazy val fa0 = fa
        bind(f)(map(fa0))
    }
    read more