• Scalaz Apply

    Apply is Functor that also has apply method. Scalaz defines Apply[F[_]] trait with ap abstract method.

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

    Having a higher kind A and hight kind of A to B mapping we can get higher kind B. And since Apply is also a functor we also have

    def map[A, B](fa: F[A])(f: A  B): F[B]
    read more
  • Scalaz Functor

    Functor is a mapping from type F[A] hight kind to F[B]. Scalaz defines Functor[F[_]] trait with map abstract method.

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

    Having a higher kind A and A to B mapping we can get higher kind B.

    read more
  • Methods Scalaz Adds To Standard Classes - Integer, String and Boolean

    In my previous article about Semigroup, Monoid, Equal, Order, Enum and Show. I covered what those are, what method they have and what derived and syntax sugar methods scalaz bring for them. Also I showed that there are implicit implementations of those for scala standard classes that once are brought into the scope you’ll have access to those. In this article I’m gonna show those methods in action for integer, string and boolean.

    read more
  • Scalaz

    Scala is an object oriented language with functional aspects. Some people like the object oriented aspect, some are attracted by the functional part. Add to that the fact that it’s sitting on top of JVM and of course it’s influenced by Java a lot, we still have nulls, we still have exceptions, IO and state mutation, etc. And since Scala isn’t purely functional it still allows us to write imperative code, call a Java library that will throw an exception, etc. I personally think that functional code is easier to read and understand, unit test and maintain(not event talking about state and multiple threads). So when it comes to writing purely functional code (or as close to it as possible) Scala sure misses certain things and scalaz has a lot built to help. I decided to write series or posts to explain scalaz. I’ll start with the basics, most common abstractions then explain more real words cases. I’ll put the links to all posts here so it’s easy to navigate.

    read more
  • Scalaz Semigroup, Monoid, Equal, Order, Enum, Show and Standard Scala Classes

    Scalaz has so many abstractions and higher kinds that model lot of algebraic structures, monads, hight level types to generalize common structures and offers lot of tools that work with those abstractions. In this article I wanted to cover the very basics. Ones we see every day in standard scala library. What the anomalies of basic types are in standard scala library, what are the comment things between them that can be made generic, how to use scalaz with standard classes defined in scala library.

    read more
  • Running Akka Applications

    With akka becoming more and more popular, akka kernel being deprecated, docker rocking the word, Typesafe releasing ConductR and more folks getting to scala, I wanted to write a post about how to run akka applications. Akka applications aren’t servlet applications java developers are used to, normally those are main class applications in a jar file and have other jars as dependencies. So it’s no-brainer that we can just use java command and no container like tomcat or jetty is need. I will cover how to run in development while iterating on a task. How to run it in development and tunnel the port and let others point to your code before you even go to production or pre production environment and how to run it inside a docker container in production and pre production environments. Unforunatly I didn’t get hold of ConductR to try it myself, so I won’t be writing about it in this post. Btw, most of this should be to relevant other scala/java main class applications not only akka and akka http applications.

    read more
  • Activator And My Activator Templates

    When activator just came out I wasn’t really excited about it. I’m more of a command line guy and using a web UI to help me build, test, run my code isn’t very useful to me. Activate also allows you to browse the code from web IDE. I usually use Vim, IntelliJ or GitHub for it, but I get the idea, for someone who doesn’t have his or her environment setup and maybe got a project that’s not even in github yet, web IDE can be helpful. I have to give more credit to activator though, because of couple really cool things. Recently my colleague who’s not a scala developer (and of course he doesn’t have his scala environment setup with latest sbt and all that) needed to run one of the projects we have in his local computer and asked me how to do it. And of course first thing that came to my mind was get the latest sbt and do sbt run. Then I remembered that I started that project from one of the activator templates I created, so it must have activator there. So he ran ./activator and magic! It went and downloaded needed sbt version and loaded sbt environment for him. And the reason I started my own activator templates at first place is because it solves another big problem for me.

    read more
  • Testing styles in ScalaTest

    Over time different libraries brought different testing styles, jUnit, xUnit, Rspec, ert. Nowadays since many people have different backgrounds and different taste when it comes to writing unit tests, most test libraries offer many testing styles and ScalaTest isn’t an exception. To me there are three groups of testing styles. One would the traditional jUnit, xUnit style where you define a test class and unit tests are methods in that test class, you also define setUp and tearDown methods that must be called before and after each unit test. Of course there are also setUp and tearDown versions that run once per test suit but I don’t want to talk more about it here. I must admit that this isn’t my favorite style and here is why. I like to unit test aggressively without skipping any business cases instead of just relying on code coverage and saying my other test already is hitting those lines. If I have a class with many methods I write many unit tests for each method, an example would be when a method takes a parameter and based on different values for those parameters it does different things, of course we want to have multiple unit tests for that method to cover all business cases for that method. So this alone groups unit tests in a test class by functions, another thing is that most likely unit tests for function A will share some setup logic. This testing style forces as to put setup code for all unit tests for function A in setup method, and when we have like 10 such functions, setup method will have the setup code for all those function which will make it long, hard to read, also it will be extremely hard to tell which setup code is for which unit test. Another option is to define setup code in each unit test with isn’t really a good idea since one we’ll have duplicated code and unit tests now will be longer than they need to be and hard to understand. I see some people like to solve this problem by moving this common code into private methods. Although this solves code duplication problem, also makes unit tests shorter but it introduced another big problem. When I read a unit test I want to know what the method is doing in 2 seconds, unit tests are documentations right. Now you come across this private method call, you have to find it’s implementation, read the ugly code there and jump back to continue reading what that unit test is trying to test. It’s a nightmare to me, I call that unit test anything but readable and I sure don’t want to be one maintain that code. Before I moved to next type of unit tests, I must mention what styles ScalaTest is offering here. FunSuite and FlatSpec, you can read more about those in ScalaTest documentation. Both are flat as mentioned, one is more like jUnit, other is more like xUnit.

    read more
  • Simple Caching For Akka Actors

    Recently I needed to cache some lightweight data into actor’s local memory to not make unnecessary calls to third party service over the internet. I think what I came up with is a pretty simple implementation (hence I named the article simple caching in akka). But the idea can be expended with small changes if any to use it to cache actor messages. Let’s imagine the word of actors where we have one actor per user and we send messages to user actors to get some info back. Now when we say each user (actor) is responsible to cache it’s own data it means each actor will use even more memory so at some point we’ll need to scale out because we’ll run out of memory (not that we didn’t have that problem without cache at first place). But it’s a good thing that we know we need to scale out from the beginning because I didn’t mention about high availability yet, so we at least need two boxes.

    read more
  • Continuous Integration For Scala Projects

    So what is continuous integration, at the very high level it’s about hey is the code in a good state, no matter by how many people code is being updated, how often code is being changed or of how many building blocks project consists of. When you start working on a feature you’re gonna branch of mainline and start introducing changes. At some point when you find things are broken now you have to spend time to see if it’s because of your changes or project was already in a bad state. And when you find out that it was already in a bad state you would want to start tracing. A who introduced the breaking changes and B when. So to avoid the head each and frustration what you really want to know is if project is in a good state before you even start working on it. One option would be to compile the code before you start changing it. But if code compiles is it enough? Oh you you also wanna know if unit tests are passing. But is this enough? Check for compiler warnings? Style violations? Code coverage? What if you run code coverage and it shows that it’s 85%, does it mean everything is good, what if code coverage was 95% last week and code without proper code coverage got committed and code coverage dropped (do you remember what code coverage was last week, can you even find out without going back in commits and running coverage reports on those commits.) are you sure you want to introduce your changes on top of those changes? Let’s make it even more complex, what if the project consists of many other projects and the project you’re working on brings all those dependencies tougher. Maybe you should checkout all those dependency projects and see if those are in a good state also. Let’s make it even more complex, what if the project isn’t just set of projects that get bundled into one big project. What if the whole code base consists of many micro services that talk to each other via some form of PRC. OK maybe you can manage to do all this to know if code is in a good state before you introduce your changes what about the point B I brought earlier about when code broke, because longer it was in that states longer it’s gonna take to trace the breaking change and longer to fix. Can you go and check every single project every time someone makes a commit to make sure that commit is good? Well actually you can, you just need a build server. So all those points I was bringing was about whatever your integration is it should be continuous and it’s continuous if it runs after somebody changes something (or maybe before is a better option?☺ I’ll get to this later.)

    read more