• 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
  • Linting Docker Files With From Latest Tool

    Recently folks at Replicated created an oper source tool called FROM:latest, which is a linting tool for docker files. You put the content of your docker file and it tries to find issues you have in it. Such a cool idea, I never came across a tool like this for docker files before. I decided to try it with the docker files I wrote in past to find potential issues in them and it actually found few issues.

    read more
  • Scalaz Applicative

    Applicative is Apply that also has point or pure methods. Scalaz defines Applicative[F[_]] trait with point abstract method.

    def point[A](a: => A): F[A]

    point method lifts A to F[A]. Since Applicative is Apply it inherits all the methods that Apply offers plus it needs to provide implementation for map and ap methods. Note that Applicative implements map using point and ap methods:

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