1. Scala at BCN Developers Conference

    As you may probably already know, the BCN Developers Conferece is being held on November, and its a fantastic occasion to meet other developers… and spread the word.

    BCNDevCon chooses the speakers with a voting system, so if you are interested, give your votes to whatever you are willing to assist there. There are 5 papers proposals related to Scala (and you may recognize some names there).

    Here you have a list of those papers that are related to Scala (in no particular order), for your kick reference:

    So if you want more Scala at BCN Dev Con, go vote your favorite papers!

  2. Jordi and Ignasi introducing Scala to newbies at MOB on Sept. 27th

  3. A development environment for #scalabcn Coding Dojos

    This is a short post for assistants to the Scala Developers Barcelona Coding Dojo who may need help or advise installing an starting environment.

    This is my personally preferred toolset (JetBrains IntelliJ + SBT) but there are other options (like Maven or Eclipse). Feel free to bring your own configuration!

    The development environment

    • Install SBT. Manual installation is recommended.

    • Edit or create ~/.sbt/plugins/build.sbt and add the following line:

      addSbtPlugin(“com.github.mpeltonen” % “sbt-idea” % “1.2.0”)

    • Install IntelliJ Community Edition

    • Launch it, go to Preferences, search for “Plugins”, Browse repositories and search for “Scala”

    • Install Scala and SBT plugins

    A blank project

    • Create a directory for your project (e.g. ~/workspace/sample).

    • Create a file sample/build.sbt (the blank lines between lines must be preserved):

      name := “sample”

      scalaVersion := “2.10.0”

      libraryDependencies ++= Seq( “org.scalatest” % “scalatest_2.10” % “1.9.1” % “test”, “org.mockito” % “mockito-all” % “1.9.0” % “test” )

    • Once to start with and every time you modify your build.sbt after that, run sbt gen-idea to create IntelliJ descriptors form your build.sbt.

      • The idea here is to follow the DRY tip and have only one definition for the project: SBT. The definition IntelliJ needs (the IntelliJ modules and project files), are therefore automatically generated from the sbt project definition.
    • Open the project in IntelliJ and… have fun!

  4. Hands on Scala: Writing a REST API with JAX-RS in Scala

    New Barcelona Scala Developers meetup on Wednesday 27 of february at Itnig!

    As you may already know, we, the guys behind funció and agilogy, are also the organizers of the Barcelona Scala Developers meetup. We recently reached 50 members and it is high time we meet again to talk about Scala, program in Scala and have a Scala beer!

    In this talk we (Jordi Pradel and Jose Raya) will develop a little application using JAX-RS, the Java standard for developing RESTful applications, in Scala. And we want to develop it live and from scratch. Attendees will be invited to participate by pair programming with us, asking questions, contributing opinions…

    How will we do it? Well, of course, in a 100% functional style and using agile techniques (like Test Driven Development and Behavior Driven Development). After all, we are agilogy + funció and this is our agile functional style.

    Many, many thanks to the people at Itnig, who will be hosting the event at their wonderful office. And thanks to Moritz for the beers. Didn’t I told you? If you are thirsty, you can grab some beers.

    Information:

    • Language? English.

      • Spanish or Catalan if everybody understands them
    • When? Wednesday, February, 27

    • Timing? From 19.00h to 20.30h

    • Register at the Meetup event

    • Where? itnig HQ office:

    Collaborators:

    Scala Developers Barcelona

    Barcelona Scala Developers meetup

    funció, functional programming for the jvm from Barcelona

    funció

    agilogy, Agile Software Development

    agilogy

    Itnig

    Itnig

  5. Scala REST API with Jetty, Jersey JAX-RS & Jackson

    These days I’ve been studying different alternatives to implement a REST API in Scala. I had used an almost framework-free approach in a previous project, but this time I had some specific requirements in mind:

    • I want to be able to discover the API using reflection, so that (eventually) an API documentation can be generated automatically. I’m thinking Swagger.
    • I want to avoid using unmature frameworks.
    • I want to avoid using frameworks that abuse Scala operators (what I’d call emoticonic frameworks with operaturs such as :>:).
    • I want top Java interoperability. Being able to easily write some methods or resources in the API in plain old Java is interesting, even if I think we won’t need to do so.
    • I want to be able to do Dependency Injection by hand. I would like to avoid adding Guice or Spring to the mix… until they really pay off (if and when they do).
    • In general, I want the solution to be as simple as possible.

    I finally came across a combination that may suit (almost all of) my needs:

    Check it out here:

    https://bitbucket.org/jordipradel/jersey-scala-example

    Ok… in fact, I found some glitches:

    • Jackson Module Scala can’t skip fields with value None when serializing to JSON: It creates a JSON field with null value. Interestingly enough, it works perfectly well the other way arround: Not providing a field in a JSON object is correctly deserialized to None if the Scala property is of type Option.
    • JSON serialization has been not that simple to configure.

    And there are many things that still need more exploration:

    • Error handling using exception mappers
    • Specifically, JSON deserialization error handling so that we can provide infromative messages about what’s wrong with the JSON we receive

    Alternatives

    Some discarded alternatives were:

    • Jerkson: Another Jackson base Scala JSON library… now abandoned.
    • Scala 2.10.0-RC2: That version didn’t play well with Jackson. That is:

      java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps
      

    Some alternatives I didn’t try:

    • Jacks yet another Jackson based Scala JSON library
    • Spray whichs seems a nice alternative… althought I’m not sure how easy it would be to discover the api at runtime and to mix Java and Scala.
    • Play! framework which seems more oriented towards application development (with its views, DB layer, etc)
    • Lift: It seems to use pattern matching for URL routing, and that seems difficult to discover using introspection, to say the least.
    • Blueeyes, Pinky and others
  6. Pure Scala BDD made easy with SBT and Cucumber.

    You may probably already know about BDD and Cucumber. Well, Cucumber allows you to program the steps in your scenario in Ruby… and now in Java and Scala, too.

    Therefore, I evaluated Cucumber as a BDD solution for an upcoming Scala project. And it did really well, compared to other solutions that were quite more difficult to set up.

    This post is almost unnecessary. It is that easy. But, just in case you are unsure what versions to use, or how to do it, here you have a quick recipe.

    If you want the complete picture, check the example project I just created at Bitbucket: https://bitbucket.org/jordipradel/cucumber-scala-example .

    Ingredients

    Directions

    1. Add the SBT Cucumber plguin to your project (file project/build.sbt):

      resolvers += "Templemore Repository" at "http://templemore.co.uk/repo/"
      
      addSbtPlugin("templemore" % "sbt-cucumber-plugin" % "0.7.0")
      
    2. Add Scala version, Scalatest dependency and SBT Cucumber plugin default settings to your project (file build.sbt):

      scalaVersion := "2.10.0-RC2"
      
      libraryDependencies += "org.scalatest" % "scalatest_2.10.0-RC2" % "2.0.M5" % "test"
      
      seq(cucumberSettings : _*)
      
    3. Create your feature (e.g. a file Multiplication.feature in src/test/resources/bdd):

      Feature: Multiplication 
        In order to avoid making mistakes
        As a dummy
        I want to multiply numbers
      
        Scenario: Multiply two variables
          Given a variable x with value 2
          And a variable y with value 3
          When I multiply x * y
          Then I get 6
      
    4. Create a Scala steps definition class (e.g. in src/test/bdd/MultiplicationSteps.scala):

      package bdd
      import cucumber.api.scala.{ScalaDsl, EN}
      import org.scalatest.matchers.ShouldMatchers
      import scala.collection.mutable.{Map => MutableMap}
      
      class MultiplicationSteps extends ScalaDsl with EN with ShouldMatchers {
      
        val vars = MutableMap[String,Int]()
        var result = 0
      
        Given("""^a variable ([a-z]+) with value (\d+)$"""){ (varName:String, value:Int) =>
          vars += varName -> value
        }
        When("""^I multiply ([a-z]+) \* ([a-z]+)$"""){ (var1:String, var2:String) =>
          result = vars(var1) * vars(var2)
        }
        Then("""^I get (\d+)$"""){ (expectedResult:Int) =>
          assert(result === expectedResult)
        }
      }
      

      Note you can use your natural language. Simply import another trait instead of EN. Check the bitbucket repo for an example of a scenario written in catalan.

    5. Launch or reload your sbt, then run cucumber and see the magic happen! And if you forgot to define a step, you’ll get a message that will help in defining it. Something like:

      [info] You can implement missing steps with the snippets below:
      [info] Given("""^a variable x with value (\d+)$"""){ (arg0:Int) =>
      [info]   //// Express the Regexp above with the code you wish you had
      [info]   throw new PendingException()
      [info] }
      

    Conclusions

    There is no excuse to not be using BDD in your Scala projects. Just try it and you’ll never go back.

    Ah! And thanks to skipoleschris, the person behind the Cucumber SBT plugin, for being so helpful in solving a dependency problem I had.

    P.S. Ok… The style is not precisely functional with those BDD steps having state. Any ideas on this stuff are welcome, of course.

  7. http://tapestryjava.blogspot.com.es/2012/07/you-cannot-correctly-represent-change.html

    You Cannot Correctly Represent Change Without Immutability

    A recent comment in one of our past articles made me remember this excellent article on immutability. A must read.

  8. On page two hundred and sixteen, after talking about algorithms, data structures, recursion, iteration, trees, high-order procedures, scoping, local variables, data abstraction, closures, message-passing, and a plethora of other topics — after all that, they introduce assignment!

    And with that elegant coup-de-grace (which is not the last in this book!), they vanquish the concept that programming is about manipulating state. With that one stroke, they force you to look back on all you had done in the previous pages in a new and enlightened way — a functional way.

    "Uncle" Bob Martin, in "Why Clojure", writing about "The Structure and Interpretation of Computer Programs" (and about Functional Programming, LISP and Clojure).

  9. It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures.

    Alan J. Perlis

  10. 1st, doubts about #TDD in Now about domain modelling. @deanwampler: the essential flaw of OOP is representing the domain model in code.

    @agile_jordi (http://bit.ly/L7V6Hx)

Theme: Notes 1.6 by mikedidthis