Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Enterprise Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Review of The Well-Grounded Java DeveloperAuto-Verifying Moqs »
    comments

    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

    1. object Test2 {
    2.  
    3.   def main(args: Array[String]): Unit = {}
    4.  
    5. }

    Let's make it more interesting by generating some output

    1. object Test2 {
    2.  
    3.   def main(args: Array[String]): Unit = {}
    4.  
    5.     val (name, site, role) = getSomeInfo()
    6.     println("Name is " + name)
    7.     println("Site is " + site)
    8.     println("Role is " + role)
    9.  
    10.     def getSomeInfo() = {
    11.     ("SQLDenis", "LessThanDot", "blogger")
    12.     }
    13. }

    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

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

    Here is the whole code

    1. object Test2 {
    2.  
    3.   def main(args: Array[String]): Unit = {}
    4.  
    5.     val (name, site, role) = getSomeInfo()
    6.     println("Name is " + name)
    7.     println("Site is " + site)
    8.     println("Role is " + role)
    9.    
    10.     for (i <- 1 to 3) {
    11.         print(i + ",")
    12.     }
    13.     println(" Testing 1,2,3.....")
    14.  
    15.     def getSomeInfo() = {
    16.     ("SQLDenis", "LessThanDot", "blogger")
    17.     }
    18. }

    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:



    That is all for this post, Scala is just one of the languages I will explore in my quest to be more of a polyglot this year. Have you looked at Scala or some other functional language like F#?

    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

    1. import java.text.DateFormat;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Calendar;
    4. import java.util.GregorianCalendar;
    5.  
    6. public class Test
    7. {
    8.    private static final DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy");
    9.    
    10.    public static void main(String... args) {
    11.       GregorianCalendar cal = new GregorianCalendar();
    12.       GregorianCalendar stopDate = new GregorianCalendar();
    13.       stopDate.add(Calendar.YEAR, 10);
    14.        
    15.       // Move ahead to the next Friday
    16.       while (cal.get(Calendar.DAY_OF_WEEK) != 6) cal.add(Calendar.DATE, 1);
    17.        
    18.       while (cal.before(stopDate)) {
    19.          if (cal.get(Calendar.DAY_OF_MONTH) == 13)
    20.             System.out.println(format.format(cal.getTime()));
    21.            
    22.             cal.add(Calendar.DATE, 7);
    23.       }
    24.    }
    25. }

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

    1. object Test2 {
    2.  
    3. import java.text.DateFormat
    4. import java.text.SimpleDateFormat
    5. import java.util.Calendar
    6. import java.util.GregorianCalendar
    7.  
    8.   def main(args: Array[String]): Unit = {}
    9.   val DateFormat  = new SimpleDateFormat("EEE MMM dd yyyy")
    10.  
    11.   val cal = new GregorianCalendar()
    12.   val stopDate = new GregorianCalendar()
    13.   stopDate.add(Calendar.YEAR, 10);
    14.      
    15.   while (cal.get(Calendar.DAY_OF_WEEK) != 6) cal.add(Calendar.DATE, 1)
    16.      
    17.   while (cal.before(stopDate)) {
    18.          if (cal.get(Calendar.DAY_OF_MONTH) == 13)
    19.             println(DateFormat.format(cal.getTime()))
    20.            cal.add(Calendar.DATE, 7);
    21.      
    22.    }
    23.        
    24. }

    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

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

    That is really it for this post...

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2605 views
    InstapaperVote on HN

    1 comment

    Comment from: Patroklos Papapetrou [Visitor] Email · http://scala-topics.org
    Patroklos Papapetrou Hi Denis
    Let me introduce myself. My name is Patroklos Papapetrou and I recently started the scala-topics.org project.
    My intention is to create a community with the best content about scala, functional programming, akka etc.
    I believe that your articles are really valuable so I invite you to take a look at our partner program (http://scala-topics.org/join-us/) and get in touch with me if you're interested.
    Looking forward to hearing from you
    Regards
    Patroklos
    01/09/13 @ 03:12

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)