As part of my resolutions for the year I said I would write more blog posts and also get into different technology. I decided to take a look at Scala. Well it turns out Scala 2.10 was released yesterday. But first what is Scala anyway? From the Scala site:

Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.

Scala is an object oriented, functional, statically typed language. You could probably compare it to F# if you are a .NET programmer.

The first thing you have to do is downloading version 2.10 of Scala, you can download that version here: http://www.scala-lang.org/downloads

After it is downloaded and installed, it is time to get the Eclipse plugin for Scala 2.10

The way you do this is you click on Help followed by Install New Software… from the menu in Eclipse

Click on the Add button

For Eclipse Juno you need to use the following URL in the location box http://download.scala-ide.org/sdk/e38/scala210/dev/site/

For Eclipse Indigo use the following URL http://download.scala-ide.org/sdk/e37/scala210/dev/site/

Give a name for the repository, I named mine Scala 10 for Eclipse Juno. Hit Ok

Hit next

On the Install Details form you will see Scala IDE for Eclipse

You can expand it to reveal the following

Hit next to accept the license, hit finish.

Now Eclipse will ask you to restart. After Eclipse is restarted it is time to create our first Scala application

From the menu go to File and then select New Project. Navigate to Scala Wizards and select Scala Project

Now that the project is created let’s create a simple object

Right click on the Scala Project from the package Explorer and select New–> Scala Object

Give it a name and check public static void main

You should have something like this

object Test2 {

  def main(args: Array[String]): Unit = {}

}

Let’s make it more interesting by generating some output

object Test2 {

  def main(args: Array[String]): Unit = {}

	val (name, site, role) = getSomeInfo()
	println("Name is " + name)
	println("Site is " + site)
	println("Role is " + role)

	def getSomeInfo() = {
    ("SQLDenis", "LessThanDot", "blogger")
	}
}

Run it by selecting Run As–> Scala Application from the Run as button

Your output should be the following

Name is SQLDenis

Site is LessThanDot

Role is blogger

Why don’t we add a simple loop to our code? Here is what needs to be added

for (i <- 1 to 3) {
		print(i + ",")
	}
	println(" Testing 1,2,3.....")

Here is the whole code

object Test2 {

  def main(args: Array[String]): Unit = {}

	val (name, site, role) = getSomeInfo()
	println("Name is " + name)
	println("Site is " + site)
	println("Role is " + role)
	
	for (i <- 1 to 3) {
		print(i + ",")
	}
	println(" Testing 1,2,3.....")

	def getSomeInfo() = {
    ("SQLDenis", "LessThanDot", "blogger")
	}
}

Run it again, here is what the output should be

Name is SQLDenis

Site is LessThanDot

Role is blogger

1,2,3, Testing 1,2,3…..

Here is what my Eclipse window looks like

In case you are interested in all the new stuff in Scala 2.10, take a look at the stuff below.


The Scala 2.10.0 codebase includes the following new features and changes: 

Experimental features

The following exciting — experimental — features are part of 2.10.0:

Edit…….

And I played a little more with this and decided to do one of our Friday the Thirteenths

Here is a solution that someone posted in Java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
 
public class Test
{
   private static final DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy");
   
   public static void main(String... args) {
      GregorianCalendar cal = new GregorianCalendar();
      GregorianCalendar stopDate = new GregorianCalendar();
      stopDate.add(Calendar.YEAR, 10);
       
      // Move ahead to the next Friday
      while (cal.get(Calendar.DAY_OF_WEEK) != 6) cal.add(Calendar.DATE, 1);
       
      while (cal.before(stopDate)) {
         if (cal.get(Calendar.DAY_OF_MONTH) == 13)
            System.out.println(format.format(cal.getTime()));
           
            cal.add(Calendar.DATE, 7);
      }
   }
}

In Scala you don’t have to change that much, you can leave or take out the semicolons, here is the code

object Test2 {
  
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.GregorianCalendar

  def main(args: Array[String]): Unit = {}
  val DateFormat  = new SimpleDateFormat("EEE MMM dd yyyy")
  
  val cal = new GregorianCalendar()
  val stopDate = new GregorianCalendar()
  stopDate.add(Calendar.YEAR, 10);
      
  while (cal.get(Calendar.DAY_OF_WEEK) != 6) cal.add(Calendar.DATE, 1)
	 
  while (cal.before(stopDate)) {
         if (cal.get(Calendar.DAY_OF_MONTH) == 13)
            println(DateFormat.format(cal.getTime()))
           cal.add(Calendar.DATE, 7);
      
   }
		
}

And here is the output

Fri Sep 13 2013

Fri Dec 13 2013

Fri Jun 13 2014

Fri Feb 13 2015

Fri Mar 13 2015

Fri Nov 13 2015

Fri May 13 2016

Fri Jan 13 2017

Fri Oct 13 2017

Fri Apr 13 2018

Fri Jul 13 2018

Fri Sep 13 2019

Fri Dec 13 2019

Fri Mar 13 2020

Fri Nov 13 2020

Fri Aug 13 2021

Fri May 13 2022

With SQL Server, you can just use a number table

SELECT DATEADD(m, number,'1998-01-13')
 FROM  master..spt_values WHERE type = 'P'
and DATENAME(dw,DATEADD(m, number,'1998-01-13')) = 'friday'

That is really it for this post…