From Java to Scala…

This week I have started a course about Scala teached by Martin Odersky (the creator of this programming language). The course is basically focused on Functional programming with Scala and it reminds me my first steps in this paradigm when I was student of Labra‘s course “Functional and Logic Programming” in which I learnt some portions of Prolog and Haskell.

After some years using Java and encouraged by Labra I enrolled in this new language (I had performed my first steps in Scala some time ago but without specific objectives). The learning methodology is very easy: you have to follow and watch some video lectures and after that you have some assingments to be completed in the a fixed schedule, if you complete all assignments you can obtain a certificate!

The purpose of this post is to show (my own experience) how the same function can be coded in Java or Scala (this is the typical example of moving from an imperative paradigm to a functional one). Imagine that you have to calculate the sum of an integer list (1, 3, 5)

  • In Java, you will use something like…
int sum = 0;
for (int i = 0; i<l.size();i++){
 sum = sum + l.get(i);
return sum;
  • In Scala, using a Java style…
  def sum(xs: List[Int]): Int = {
	var sum = 0
	for (x <- xs)
		sum = sum + x
	return sum
  • In Scala, using recursion and Java style…
def sum(xs: List[Int]): Int {
    if (xs.isEmpty) return 0
    else return xs.head + sum(xs.tail)
  • In Scala, using a more functional approach…
def sum(xs: List[Int]): Int = xs match{
    case _ if xs.isEmpty => 0
    case _ => xs.head + sum (xs.tail)
  • In Scala, pure functional approach
def sum(xs: List[Int]): Int = xs.foldLeft(0)(_+_)
This is only to demonstrate how a good use of tools and programming techniques can ease the coding!


As part of my work in the different projects I have developed some tools (open source).

  • MOLDEAS (Methods On Linked Data for E-procurement Applying Semantics). It is a Java-based library to ease the access to public procurement notices.
  • ONTOSPREAD. It is a Java API for the development, configuration, customization and execution of the Spreading Activation techniques in the field of the Semantic Web. These techniques have been used for the efficient exploration of knowledge bases built on semantic networks in Information or Document Retrieval problems.
  • ROCAS. The Reasoning On the Cloud Applying Semantics project covers developments related to the National Spanish Project entitled by the same name and partially funded by the Spanish Ministry of Science (2011-2014).
  • RIFLE. I was the initial developer of this tool with my colleague in CTIC Luis Polo. Now it is maintained by CTIC. It is a suite of tools for the Rule Interchange Format (RIF). It is focused on RIF-Core and RIF-PRD and their combination with other Semantic Web standards such as RDF, OWL and SPARQL.
  • LODIN. The Linked Open Data Generator of Index Data is a development to generate RDF statistics from different sources such as WorldBank, ITU, etc. Currently this library is under development and is able to generate about 1M of RDF triples following the Linked Data principles. Its main use case is the webindex.
  • SIFROTS. The Semantically Interlinked Frame Online Tagging System is an ongoing development to deliver a system for tagging video frames using the Web of Data entities and taking advantage of the existing HTML5 specification to manage videos.