Partially functional

You can't wait for inspiration. You have to go after it with a club.

Polymorphism and higher kinds

• scala

We’ll talk briefly about parametric, subtype and ad-hoc polymorphism, then focus on ad-hoc polymorphism and its relationship with the type class pattern and higher-kinded types like Functor, then move on to discuss higher-kinded types shipped with the Cats library.

Scala learning resources

• scala

I’ve put together a list of blog, guides, articles, videos and courses I found useful by actually reading them, trying them, watching them, completing them. I’ve gone over a lot of stuff on the internet and these few items deserve a mention and are well worth your time.

Learn you a Cats

• scala

So you have heard of cats and you possibly heard of contravariant functors, monad transformers, applicatives and cartesians, and possibly even stumbled upon the term Kleisli arrows and thought “who needs this?” Well, you don’t need this, but it doesn’t hurt to learn a new thing or two. In this article I will show you a few examples of what you can do by just adding the cats library to your project’s dependencies.

Concurrent evaluation of Futures

• scala

In some cases execution of a Future depends on the outcome of an execution of a previous Future. We would use flatMap and map to sequence the evaluation of our dependent futures.

On the other hand, in cases when futures are independent of each other, it is only reasonable to execute them concurrently and collect their results, one after another, when ready. The simplest of tricks to achieve this is to write a for-comprehension and pull out the creation of Futures outside of it. A Future is submitted to an execution context as soon as it is created. If we create the futures prior to entering our for-comprehension, they are all running (or are at the very least scheduled to run) and we utilise the for-comprehension only to collect their results (future values) in sequential manner. While this approach works, let’s see some other ways to do this before we adopt it.

There are many ways to defur a feline. Also, there is more than one way to execute a number of Futures in parallel. Let’s have a look at some of our options.

Conditional flatMap - tweaking sequences of effects with boolean conditions

• scala

I found myself in a situation when several Futures had to execute in a predefined sequence and one of those Futures was to be executed only under a certain condition. I immediately reached for the cats-provided flatMap syntax (cats.syntax.flatMap._) and my pet ifM:

  _ <- condition.pure.ifM(service.call(params), ().pure)

However, the above doesn’t look good. It’s hard to see what’s going on. So I tinkered with cats and made the more pleasing to the eye:

  _ <- service.call(params) onlyIf condition

After all,

In the original language design great care was taken to ensure that the syntax would allow programmers to create natural looking DSLs.

www.scala-lang.org