How to have a single test spec for many different implementations (in git)

I’m running Scala workshops regularly in Warsaw, Poland and for the exact purpose I’m developing the scalania project.

The past workshops have been quite successful with around 15 attendees every session. They clone the git repo from GitHub and solve the programming assignments on their own. It’s going tremendously well.

There’s however an issue how to collect the many implementations people are developing during the sessions under the git repository and have them all be verified by a single spec so there’s no need to change it when a new implementation shows up.

Let’s put the issue in a context.

I’ve got a single test spec, say euler.P01Spec.

package pl.japila.scalania.euler

import org.specs2.mutable
import Euler_P01.findSumOfMultiplies

class P01Spec extends mutable.Specification {
  "P01 solution" should {
    "Find the sum of all the multiples of 3 or 5 below 1000." in {
      findSumOfMultiplies(3, 5, 1000) == 233168

The spec verifies Euler_P01.findSumOfMultiplies method’s correctness.

package pl.japila.scalania.euler

object Euler_P01 {
  def findSumOfMultiplies(a: Int = 3, b: Int = 5, limit: Int = 1000): Int = ???

To provide an implementation of the findSumOfMultiplies method is the programming assignment. When you run the spec it fails because of the missing implementation (the ??? method throws scala.NotImplementedError). Once an implementation’s done, it’d be quite useful to push it to the repository.

The project’s under git (on GitHub as the scalania project).

Here goes the question – how to keep the many solutions/implementations from people in the git repository without the euler.P01Spec be changed when a new implementation shows up (once a pull request gets committed to the repo)?

Languages 2 Comments

Scala Days 2013 Notes from Expanding eta-expansion: shapeless polymorphic function values meet macros

What could be better to master Scala and presentation skills than to watch people who do them better? Since the recordings of the presentations from Scala Days 2013 are available on Parleys at no charge I thought I’d spend some time and watch a few.

I did really watch a few, but just recently I decided to review them to better understand the topics and perhaps absorb their wisdom in a more effective way.

Quite recently I was watching the recording from the session Expanding eta-expansion: shapeless polymorphic function values meet macros by Miles Sabin. It was a quite informative presentation, but to be honest just the first part before Miles switched to the vim editor and explained the shapeless project was the most valuable. I seemed to not have been able to follow along later (when he talked about shapeless and its purpose). The speaking style was very engaging and despite my lack of understanding of the 2nd part I could listen to the end with ease.

Here are some sentences from the presentation that I really liked and a few comments about it (the presentation). The excerpts comprise the time and what Miles said (so trying to write the manuscript I had to rewind a few times and eventually remembered the most).

00:01:37 What is eta expansion?
“eta expansion is a mechanism whereby Scala tries to gloss over the distinction between methods which are intrinsic components of JVM that are a sort of artifacts which are attached to classes and interfaces – the lowest level native representation of types on the JVM. Functions, on the other hand, are Scala-specific artifacts. They’re represented as first-class values and eta expansion is a mechanism which allows to in many circumstances completely seemlessly slide (?) between the two.”

00:02:30 “Show very quickly what that actually means in practice.”

I was wondering why Miles didn’t use the following code to populate a list of lists. I also think it’d have been better if the code had been already available (in a git tag perhaps) so there had not been necessary to type it in.

scala> (1 to 3).map(List(_)).toList
res5: List[List[Int]] = List(List(1), List(2), List(3))

00:06:48 “Scala does a very good syntactic job of hiding a transformation that’s been formed on the arguments of the map and done in a such seamless way that it makes it appear that the method headOpt[T] is in fact an entity of the same category as a function value of either List[Int] or List[String] to Option[Int] or Option[String], but in fact it’s not.”

It’d be definitely much better when he’d used :paste and tab key in REPL.

00:07:48 “If headOpt is a function, it should be perfectly possible to redefine headOptInt in terms of a function type.”

00:08:47 “The thing is that we actually have now something which is…although the method we started with is monomorphic the value that we have here is…”

00:09:08 “Before I go anywhere further I want to illustrate that the original headOptString is a value and it’s a value in the JVM’s sense of being an object that will have for example hashCode whereas headOpt is not a first-class value and doesn’t have hashCode.”

00:09:41 “It [headOpt] is clearly not a kind of thing which we can treat as being a Java value.”

00:09:58 “So what happens under the hood [with headOptInt assignment] is that seamlessly the Scala compiler had inserted this magic trailing underscore. And the magic trailing underscore is something which allows us to explicitly ask for a method value to be lifted to a function value. What actually happened is that under the hood the Scala compiler has manufactured a brand new value of Function1 type with its argument type instantiated to a list of Int result type instantiated to Option[Int].”

00:14:05 “Any is almost as useless as Nothing in this case.”

00:16:35 “In this case we do have something that’s clearly a value => headOpt returns headOpt.type.”

00:18:00 The speaker should’ve used map over hl to convert hl to a list of lists and then done the rest. But :paste would’ve been even better.

00:28:17 “I dearly would like to write that…just directly use a polymorphic and oridinary vanilla Scala polymorphic method and have that worked directly. Oh, we can. It worked! How is that possible? smiling…”

00:29:35 “Perhaps an unsurprising trick involved here is an implicit conversion from a function type to a polymorphic function value type. And a sneaky and evil trick involved here is to actually in effect consume this rather useless function from a List[Nothing] to Option[Nothing]. Inspect it at compile time var macro, extract out the useful part which I guess we will see very shortly…”

Then Miles went on to demonstrating code of Shapeless that I didn’t find much useful to understand eta-expansion. Although I watched till the very end, I found no information I meant to put here to remember. It was worth to watch the 2nd part, anyway.

Languages Leave a comment

Reversing number without resorting to String in Scala

There is no better way to learn as to try new things without fear of making mistakes.

I wish I’d follow it more often. When I do, I also do my best to let others know so they hopefully let me know where I may have made mistakes.

I’ve recently been exposed to some algorithmic problems and since I’ve never been good at solving them and promised myself to improve I made an attempt to solve one – check whether a number is a palindrome without resorting to using strings.

Here is my solution – a method to reverse a number without using java.lang.String. With the method, when an input number equals the output the number may be a palindrome.

def reverse(n: Integer): Integer = {
  (Stream.continually(n) zip Stream.iterate(10) {_ * 10})
    .takeWhile { case (n,tens) => n * 10 > tens }
    .map { case (n, tens) => n % tens }
    .map { case (n,i) => n / math.pow(10, i).toInt }
    .foldLeft(0){ case (res, (n, i)) => res + n * math.pow(10, i).toInt }

I humbly ask you to improve the code so in the end I could.

p.s. When a number ends with 0‘s, the reverse method is not sufficient.

Languages 8 Comments

Studying scalaz to learn Scala (and perhaps category theory) with sbt

I’m studying the source code of the scalaz project as a way to introduce myself to Scala (and category theory in general). Reading a source code has always been considered a constructive approach to learn a language provided the project is of importance to the community (behind the language) and is developed with care. I think scalaz is of great importance to the Scala community and can be quite useful to learn the language.

I’ve found the question Good scalaz introduction [closed] on StackOverflow where I could find some pointers to articles and videos about scalaz. That’s how I stumbled upon the video Scalaz Presentation – Nick Partridge. It’s quickly turned out very useful. I highly recommend watching it.

At 00:38:07 Nick shows the slide with two imports:

import scalaz._
import Scalaz._

It took me a while to understand their meaning, even after Nick explained it well (but it became so after I spent a while to think about it).

The first import (with scalaz written with the lowercase s) is to import all the members of the scalaz package while the other (with Scalaz with the uppercase S) is to import the members of the scalaz.Scalaz object. Two different namespaces and different “artifacts” loaded. It might be quite surprising for newcomers to Scala (it certainly was for me).

See the below sample session with Java’s java.util.concurrent package to learn about “import stacks” in Scala, e.g. how one import can shorten the other (given they’re in scope).

Welcome to Scala version 2.10.2 (Java HotSpot(TM) Client VM, Java 1.7.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import java.util._
import java.util._
scala> import concurrent._
import concurrent._

I needed an environment where I could do the same with scalaz.

But I didn’t mean to spend hours completing CLASSPATH. I bet you don’t, either.

So if you’re keen to give scalaz a try without having to worry about transitive dependencies to feed them in to Scala REPL (by expanding its CLASSPATH), use sbt. Scala Build Tool (sbt) can manage dependencies for you.

I assume you’ve got sbt installed already. Unless you have, follow the official Installing sbt.

With sbt available, create a project directory and then build.sbt with the following content:

name := "scalaz-sandbox"
version := "1.0"
scalaVersion := "2.10.2"

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0-M1"

Please note that the empty lines between sbt properties are required.

You may replace the version of scalaz from 7.1.0-M1 to 7.0.2 if you need the latest stable one.

Run sbt console, do import scalaz._, Scalaz._ and have fun studying scalaz (and perhaps category theory, too). Let me know how it goes. There’s the series learning Scalaz if you need some further reading.

p.s. I’m on my way to learn what the concept of functor means to functional programmers. I’m yet to find it out.

Languages 4 Comments

Importing sbt-based project to IntelliJ IDEA 13 (with sbt-idea, Scala 2.11 and sbt 0.12)

Just as in How to create SBT project with IntelliJ Idea? on StackOverflow I needed to import a sbt-managed project – scalaz – into IntelliJ IDEA.

With my configuration – Scala 2.11.0-M3 and sbt 0.12.4the answers to the question on StackOverflow were often misleading and hence the blog post.

$ sbt gen-idea
[info] Loading global plugins from /Users/jacek/.sbt/plugins
[info] Updating {file:/Users/jacek/.sbt/plugins/}default-b0cfcf...
[info] Resolving com.github.mpeltonen#sbt-idea;1.0.0 ...
[warn]      module not found: com.github.mpeltonen#sbt-idea;1.0.0
[warn] ==== typesafe-ivy-releases: tried
[warn] ==== sbt-plugin-releases: tried
[warn] ==== local: tried
[warn]   /Users/jacek/.ivy2/local/com.github.mpeltonen/sbt-idea/scala_2.9.2/sbt_0.12/1.0.0/ivys/ivy.xml
[warn] ==== sbt-idea-repo: tried
[warn] ==== public: tried
[info] Resolving org.scala-sbt#precompiled-2_10_1;0.12.4 ...
[warn]      ::::::::::::::::::::::::::::::::::::::::::::::
[warn]      ::          UNRESOLVED DEPENDENCIES         ::
[warn]      ::::::::::::::::::::::::::::::::::::::::::::::
[warn]      :: com.github.mpeltonen#sbt-idea;1.0.0: not found
[warn]      ::::::::::::::::::::::::::::::::::::::::::::::
[warn]      Note: Some unresolved dependencies have extra attributes.  Check that these dependencies exist with the requested attributes.
[warn]           com.github.mpeltonen:sbt-idea:1.0.0 (sbtVersion=0.12, scalaVersion=2.9.2)
sbt.ResolveException: unresolved dependency: com.github.mpeltonen#sbt-idea;1.0.0: not found
[error] (*:update) sbt.ResolveException: unresolved dependency: com.github.mpeltonen#sbt-idea;1.0.0: not found
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? q

It didn’t take long to turn out that I should’ve read the website of the sbt-idea plugin first before looking for answers elsewhere. The system-wide build.sbt (in to ~/.sbt/plugins) configuration file should have a different repository instead and use the most recent stable (1.4.0) or 1.5.0-SNAPSHOT releases, too.

$ cat ~/.sbt/plugins/build.sbt
resolvers += "Sonatype snapshots" at ""

addSbtPlugin(dependency="com.github.mpeltonen" % "sbt-idea" % "1.5.0-SNAPSHOT")

With the change the command sbt gen-idea generated the necessary files with no errors.

$ sbt gen-idea
[info] Loading global plugins from /Users/jacek/.sbt/plugins
[info] Updating {file:/Users/jacek/.sbt/plugins/}default-b0cfcf...
[info] Resolving org.scala-sbt#precompiled-2_10_1;0.12.4 ...
[info] downloading ...
[info]      [SUCCESSFUL ] com.github.mpeltonen#sbt-idea;1.5.0-SNAPSHOT!sbt-idea.jar (2579ms)
[info] Done updating.
[info] Loading project definition from /Users/jacek/oss/scalaz/project
[info] Updating {file:/Users/jacek/oss/scalaz/project/}default-b1c5e5...
[info] Resolving org.scala-sbt#precompiled-2_10_1;0.12.4 ...
[info] Done updating.
[info] Set current project to scalaz (in build file:/Users/jacek/oss/scalaz/)
[info] Creating IDEA module for project 'xml'
[info] Created /Users/jacek/oss/scalaz/.idea_modules/typelevel.iml
[info] Created /Users/jacek/oss/scalaz/.idea_modules/scalaz-build.iml

In the latest version of IntelliJ IDEA 13 EAP 130.1179, codenamed Cardea, I could easily import the project pointing at the scalaz main directory where the project was cloned into.


The scalaz project set up IntelliJ IDEA-specific files to have them .gitignore‘d that I consider as a positive argument for the results of my experiments with sbt-idea.

$ git status
# On branch scalaz-seven
nothing to commit (working directory clean)
Languages, Tools , , Leave a comment

Scalania.PL – Warszawa JUG’s meetings to practise Scala by examples

I’m still very new to Scala if case class is to me only to combine attributes in a class and have the class and the attributes available in pattern matching.

Just ran across the course by Tony Morris, more specifically the case class Id[A] with two methods map and flatMap that I would never have thought could be a part of this or any case classes (!) That’s amazing how small things can influence one’s thinking about what’s possible.

Since I’m on my journey to practise and use Scala on a daily basis (aka being paid to do it professionally) I thought I’d run a series of meetups to make the learning pleasant and collaborative. That’s how I set up Scalania – a series of meetings (under Warszawa JUG‘s umbrella) to learn and pracise Scala by examples.

I hope not to be surprised with the features of Scala anymore…soon. Come and join us to make the initiative successful. I’m for one to appreciate your efforts greatly.

Languages Leave a comment

New adventures with functional languages in financial sector

It’s a bit over a year when I wrote “p.s. I felt in love with functional programming with Clojure and F# so much that I could very likely consider a job as a team leader or a lead developer, too.”. It’s amazing how time flies by quickly and how much I should watch my words to say what I really want or…it becomes reality.

It has.

Quite recently I’ve got a job offer to lead a technical team to develop applications with functional languages, Clojure and Scala, in an international financial institution. It appears that my wishes are about to turn into reality – I accepted the position.

I’ve been working in IBM for the past 7 years now and the time for the giant has been passing by extremely quickly. I have built my comfort zone in IBM WebSphere product family and I enjoyed it so much. I could get a hands-on experience with the products, speak on conferences, blog, tweet, write articles, record screencasts and in general be a part of the WebSphere community inside and outside of IBM. I lack words to describe the extreme awesomeness of the working environment in IBM (but be warned that it’s only available when you ask for it, else you’re doomed to stay in limbo).

I have however always felt a need to improve my skills with a close eye on a specific industry and the financial industry has been drawing my attention for some time. Moreover, I’ve always been wondering what it is to me to be on the other side of the “fence” – to be a customer (at least externally) not an IT provider (again, considering the main industry my employer operates in). Although I am still a part of a technical team in the financial institution, it’s now a part of a non-IT company where the lingua franca is a financial language. It’s a complete departure from what I’ve been doing for years while working for IT corporations like HP and IBM.

And the people who work in the team or soon will be. It’s gonna be a very uneasy environment at times where geeks will have their voice, and work needs to be done anyway. I expect many technical challenges with many people I’ve always admired for technical excellence and a passion for achieving what may seem unachievable. I think it will have a tremendous impact on what I shall become in the months coming. I believe it’s going to be the place to let my development wishes flourish.

I should now work with Clojure and Scala more often and what’s more important be paid for doing it on a daily basis. I will finally be able to learn about their application to problems in the financial sector, right from the trenches. I’m now jumping onto a bandwagon to develop the functional programming skills of mine while tackling real-life problems!

For you, my reader, it might be a good refresher as well – the job change shall improve my future blog posts here. Finger crossed.

Languages , 5 Comments

@ktosopl about Value classes and :javap in Scala 2.10.x

Thanks @ktosopl for the useful tip about using the command :javap in your blog post Scala 2.10 – or, why you will love implicit value classes. Not only :javap, but the main topic itself – value classes, made the blog post very useful and informatory!

Just a few days ago I tweeted about the release of Scala 2.10.2 and read through the changes. There was one that caught my attention – Value Classes (perhaps because of “it behave like a struct type” that I didn’t understand and couldn’t find a use case for).

With the aim to figure out how and where to use value classes in Scala, I read Value Classes and Universal Traits and then SIP-15 – Value Classes. I enjoyed the reading, but was a bit uncertain I can find the right place for the concept. I’m still quite new to Scala after all.

It was a few weeks back when I learnt about the command :javap in Scala REPL. I really liked the idea of being able to decompile Scala’s constructs and see their equivalents in the bytecode. That was something I’d needed, but didn’t know it existed or might’ve been even possible in REPL. It’s likely not something you will use often, but when it’s needed, it will be at your fingertips.

Just today, while skimming over the tweets I ran across a tweet about @ktosopl’s blog post about…value classes! In the blog post Konrad mentioned the magic :javap command, I could use to see a value class. What a coincidence, isn’t it? Almost too much good in a single blog post (or shall I say a tweet)?

$ scala
Welcome to Scala version 2.11.0-M3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class Wrapper(val underlying: Int) extends AnyVal
defined class Wrapper

scala> :javap -v Wrapper
  Size 981 bytes
  MD5 checksum 10fc333924ca75a0f1420b7bffb93654
  Compiled from "<console>"
public final class Wrapper
  SourceFile: "<console>"
       public static #43= #40 of #42; //=class  of class $line4/$read
       public static #43= #45 of #40; //=class  of class
       public static #46= #2 of #45; //Wrapper=class Wrapper of class
       public static #47= #15 of #45; //Wrapper$=class Wrapper$ of class
    Scala: length = 0x0

  minor version: 0
  major version: 50
Constant pool:
   #1 = Utf8               Wrapper
   #2 = Class              #1             //  Wrapper
   #3 = Utf8               java/lang/Object
   #4 = Class              #3             //  java/lang/Object
   #5 = Utf8               <console>
   #6 = Utf8               underlying
   #7 = Utf8               I
   #8 = Utf8               ()I
   #9 = NameAndType        #6:#7          //  underlying:I
  #10 = Fieldref           #2.#9          //  Wrapper.underlying:I
  #11 = Utf8               this
  #12 = Utf8               LWrapper;
  #13 = Utf8               hashCode
  #14 = Utf8               Wrapper$
  #15 = Class              #14            //  Wrapper$
  #16 = Utf8               MODULE$
  #17 = Utf8               LWrapper$;
  #18 = NameAndType        #16:#17        //  MODULE$:LWrapper$;
  #19 = Fieldref           #15.#18        //  Wrapper$.MODULE$:LWrapper$;
  #20 = NameAndType        #6:#8          //  underlying:()I
  #21 = Methodref          #2.#20         //  Wrapper.underlying:()I
  #22 = Utf8               hashCode$extension
  #23 = Utf8               (I)I
  #24 = NameAndType        #22:#23        //  hashCode$extension:(I)I
  #25 = Methodref          #15.#24        //  Wrapper$.hashCode$extension:(I)I
  #26 = Utf8               equals
  #27 = Utf8               (Ljava/lang/Object;)Z
  #28 = Utf8               equals$extension
  #29 = Utf8               (ILjava/lang/Object;)Z
  #30 = NameAndType        #28:#29        //  equals$extension:(ILjava/lang/Object;)Z
  #31 = Methodref          #15.#30        //  Wrapper$.equals$extension:(ILjava/lang/Object;)Z
  #32 = Utf8               x$1
  #33 = Utf8               Ljava/lang/Object;
  #34 = Utf8               <init>
  #35 = Utf8               (I)V
  #36 = Utf8               ()V
  #37 = NameAndType        #34:#36        //  "<init>":()V
  #38 = Methodref          #4.#37         //  java/lang/Object."<init>":()V
  #39 = Utf8
  #40 = Class              #39            //
  #41 = Utf8               $line4/$read
  #42 = Class              #41            //  $line4/$read
  #43 = Utf8
  #44 = Utf8
  #45 = Class              #44            //
  #46 = Utf8               Wrapper
  #47 = Utf8               Wrapper$
  #48 = Utf8               Code
  #49 = Utf8               LocalVariableTable
  #50 = Utf8               LineNumberTable
  #51 = Utf8               SourceFile
  #52 = Utf8               InnerClasses
  #53 = Utf8               Scala
  public int underlying();
    flags: ACC_PUBLIC
      stack=1, locals=1, args_size=1
         0: aload_0
         1: getfield      #10                 // Field underlying:I
         4: ireturn
        Start  Length  Slot  Name   Signature
               0       5     0  this   LWrapper;
        line 7: 0

  public int hashCode();
    flags: ACC_PUBLIC
      stack=2, locals=1, args_size=1
         0: getstatic     #19                 // Field Wrapper$.MODULE$:LWrapper$;
         3: aload_0
         4: invokevirtual #21                 // Method underlying:()I
         7: invokevirtual #25                 // Method Wrapper$.hashCode$extension:(I)I
        10: ireturn
        Start  Length  Slot  Name   Signature
               0      11     0  this   LWrapper;
        line 7: 0

  public boolean equals(java.lang.Object);
    flags: ACC_PUBLIC
      stack=3, locals=2, args_size=2
         0: getstatic     #19                 // Field Wrapper$.MODULE$:LWrapper$;
         3: aload_0
         4: invokevirtual #21                 // Method underlying:()I
         7: aload_1
         8: invokevirtual #31                 // Method Wrapper$.equals$extension:(ILjava/lang/Object;)Z
        11: ireturn
        Start  Length  Slot  Name   Signature
               0      12     0  this   LWrapper;
               0      12     1   x$1   Ljava/lang/Object;
        line 7: 0

  public Wrapper(int);
    flags: ACC_PUBLIC
      stack=2, locals=2, args_size=2
         0: aload_0
         1: iload_1
         2: putfield      #10                 // Field underlying:I
         5: aload_0
         6: invokespecial #38                 // Method java/lang/Object."<init>":()V
         9: return
        Start  Length  Slot  Name   Signature
               0      10     0  this   LWrapper;
               0      10     1 underlying   I
        line 7: 0
Languages Leave a comment

Twitter stats of mine: 4680 tweets, 360 following and 900 followers

jacek_laskowski_twitter_statsToday’s a round-number-only day on Twitter. I’ve managed to attract 900 followers to let them hear what’s important to me (4680 tweets) by following 360 people (as well as using some other means of self-learning). With 900 followers I’m tempted to think that people think I’ve got something to say. I wish it could always be true!

I vaguely remember the very first days on Twitter, but I do remember I was sceptical about using it as a communication tool. I was mostly listening as I didn’t believe I’ve got something to say (with such short messages).

A couple of months later I learnt how to use it to poke people (with short messages) and I feel pretty comfortable with the tool. I could “talk” to people I may never have met in person and been guided to articles/sites I would not likely have found otherwise. I’m glad I could find out how to use Twitter and I’m a strong proponent of using it as a vehicle of mass-collaboration and self-education.

Thanks everybody for your support and patience!

Tools Leave a comment

Final score from Scala course at Coursera: You earned 79.8%

scala-logoFinal score from the course Functional Programming Principles in Scala at Coursera is now available. There were 7 weeks of extensive study time for many of us who participated in the how and why of functional programming in Scala with the author – Martin Odersky.

I’ve been blogging about the past experiences and must admit it was my first massive open online course (MOOC) for which I was doing my best to accomodate additional time in my schedule and finish assignments on time. Alas, I missed one or two.

It wasn’t easy and kudos to people who scored 100% in total. I couldn’t and quite often suffered a great pain trying to find some time to fit it in and study the material fully. With a bit of time and task shuffling I could reach the day where I can “announce” my score: 79.8%.


I found the course very enjoyable and I’m repeating myself inviting you to give it a try if you haven’t already. It’s worth your time!

They say that Martin Odersky and his team are working on another advanced course about programming in Scala. With the past experiences I believe it should be much easier to work it out in a more smooth way – time- and assignment-wise.

The self-learning of functional programming in Scala is certainly not over and think I should now be developing a few applications in Scala to master the concepts from the course and a couple of books I could read in the meantime. I’ve got ideas and with the course over I’ve got a bit more time, too. Follow me on GitHub to know when they’re available in public. Later.

Languages Leave a comment