Conventional wisdom has it that using too many functional abstractions in Scala is detrimental to overall program performance.

Yet, these abstractions are an immense help if you want to write clean and abstract code. So, should practitioners of FP drown in guilt for writing inefficient code? Should they give way to less functional code?

Let’s find out!

The question I’ve been hearing a lot recently is:

I have used EitherT all through my code base because it helps with concise error handling. But I heard it is very slow. So, should I abandon it and write error handling myself? But…


With the recent boom in the adoption of so-called final tagless encoding in Scala land, which in turn seems to be addressing the shortcomings of the Free monad approach, the testability of programs is better than ever. The general consensus is that one of the main benefits of the Free / tagless style is that it allows for easy unit testing programs without the tedious process of setting up dependencies etc…

class TaglessService[M[_]: Monad](taglessRepository: TaglessRepository[M]) {
def getList(limit: Int): M[Seq[Item]] = {
taglessRepository.getList(limit)
}
}

You simply bind to the ID monad (or swap interpreters if you’re a Free fan)…


Why?

A common need in type-safe code is the ability to tell various values apart. This is done to have the high degree of certainty that a value is used in a context where it is supposed to be used. On the most basic level types serve this exact purpose — val x: A and val y: B may be the most obvious example of separating values by labeling them with a type. But once programs start to get bigger, the inevitable thing happens — the types get re-used. This is especially a problem with common types like String or Int


One of the reasons people use Scala is to have access to vast amount of Java libraries.

While it’s very beneficial to have a library for almost everything, these libraries are written in Java, which means you let in all the limitations of Java you have been escaping from — greatly restricted type-safety, lots of verbosity etc. Fortunately, it’s not very hard to mask these deficiencies with Scala wrapping that will ease the pain of consuming such an API.

But sometimes these Java APIs utilize an object-is-just-like-a-hash-map technique which you can recognize by having lots of methods either operating on…


‘Since their release as an experimental feature of Scala 2.10, macros have brought previously impossible or prohibitively complex things to the realm of possible’ — Eugene Burmako

One day we took on the problem of where our developers were losing time due to mindless, recurring boilerplate they were forced to write, over and over in each and every project. We went through various Scala code-bases we had been writing or maintaining, trying to identify those repetitive patterns. We were bent on freeing everyone from having to write it again and again. Ideally, we wanted to push as much of these to a common library that we could reuse throughout existing and future projects. …


Knowing how things works internally, enables you to fully the potential of code you write.

Sometimes it’d be nice to put some additional information into RequestContext to have it available at all times during request processing. Ideally, one could write custom directives that access this information without any need for external parameters, just like, for example, extractHost works. I came across one such use-case when I was validating JWT tokens. I had written a custom directive that decodes the JWT token and authorizes the user:

In a nutshell, it decrypts the JWT token passed in the HTTP header into an instance of Decoded containing the usual stuff — current user’s claims and issuer, and…


How to use Free Monads in Scala web applications

At Iterators we’ve had many ideas on how to build a well-structured REST application and we are constantly searching for improvement and better ways to get the job done. Recently we built a system centered around free monads. We want to share this bit of knowledge in the hope that you’ll find it inspiring, or maybe show us how to do it better. While there are lots of tutorials on Free itself, we didn’t find many that explain how to use it as a kind of glue layer in your system. All in all, your comments are more than welcome.

DISCLAIMER:


In part I I showed how Kleisli arrows could be used to implement domain modeling. Arrows serve as a foundation for a ‘DSL’ in which one can implement typical scenarios that arise in business-logic code: decoupling flow control from domain code, dealing with errors etc. Much to the spirit of Railway Oriented Programming but implemented in more generic terms.
In this part I’ll fill in the missing parts of this ‘framework’ — taking care of side-effects, conditional execution and mixing different monads in a single ‘pipeline’.

There is one more part planned where I’ll introduce kind-projector and arrow transformers. …


During Scala Days Amsterdam I came across a concept of arrow. It was called a general interface to computation and it looked like a technique that could bridge code that is imperative in nature with proper functional programming. Later on I read about Railway Oriented Programming and found it really neat. I was compelled to reinvent the wheel and check all this in practice. I dug up some old business logic Scala code, that could really be Java code with slightly different keywords, and tried to refactor it using all I’d learned about arrows. BTW, you can find code used…


Modeling algebraic structures has never been easier

Let’s start with small refreshment of what actually the dependent typing is. Using Wikipedia as a source:

In computer science and logic, a dependent type is a type that depends on a value.

Neat, huh? But what does it mean in practice? If you take a look at the Wikipedia’s list of languages that implement dependent typing you might get strange sensation that it is something really obscure and esoteric. Languages mentioned there range from “never heard of” (Guru/Matita anyone?) to “yeah, I recall reading about it on Reddit once or twice” (Coq/Idris). Oh wait, there is F#. Ooops, sorry…

Marcin Rzeźnicki

Using Scala to build most composable and concise business logic @ iterato.rs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store