Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Thursday, July 12, 2012

Ruby was shiny before compared to Java, Tcl and Perl. Isn't as shiny when its beside Scala now.

Just looking at Ruby again.  My first encounter with Ruby like most people is through ROR, it was a few years back (era 1.x or maybe before that).  It was a very nice language and at that time, it wasn't that popular.  I did however convince a colleague of mine to push ROR with the help of Bruce Tate's From Java To Ruby book.  He was successful in getting ROR into a Java EE environment.  I had a quick glance of Ruby again today and in tutorial there was this code:
class MegaGreeter
  attr_accessor :names

  # Create the object
  def initialize(names = "World")
    @names = names
  end

  # Say hi to everybody
  def say_hi
    if @names.nil?
      puts "..."
    elsif @names.respond_to?("each")

      # @names is a list of some kind, iterate!
      @names.each do |name|
        puts "Hello #{name}!"
      end
    else
      puts "Hello #{@names}!"
    end
  end

  # Say bye to everybody
  def say_bye
    if @names.nil?
      puts "..."
    elsif @names.respond_to?("join")
      # Join the list elements with commas
      puts "Goodbye #{@names.join(", ")}.  Come back soon!"
    else
      puts "Goodbye #{@names}.  Come back soon!"
    end
  end

end


if __FILE__ == $0
  mg = MegaGreeter.new
  mg.say_hi
  mg.say_bye

  # Change name to be "Zeke"
  mg.names = "Zeke"
  mg.say_hi
  mg.say_bye

  # Change the name to an array of names
  mg.names = ["Albert", "Brenda", "Charles",
    "Dave", "Englebert"]
  mg.say_hi
  mg.say_bye

  # Change to nil
  mg.names = nil
  mg.say_hi
  mg.say_bye
end
It's nice and concise, however it doesn't stand out as much as before.   My comparison was Java, Tcl and Perl a few years ago.  I then did a quick copy and paste, translated it to Scala.

class MegaGreeter(var names: Any = List("World")) {
 
  def say_hi {
    names match {
      case null => println("...")
      case listOfNames: List[String] => listOfNames foreach (n => println("Hello " + n)) 
      case _ => println("Hello " + names)
    }
  }

  def say_bye {
    names match {
      case null => println("...")
      case listOfNames: List[String] => println("Goodbye " + listOfNames mkString(",") + ".  Come back soon!") 
      case _ => println("Goodbye " + names + ".  Come back soon!")
    }
  }
}

object MegaGreeter {
  def main(args: Array[String]) {
    val mg = new MegaGreeter
    mg.say_hi
    mg.say_bye

    // Change name to be "Zeke"
    mg.names = "Zeke"
    mg.say_hi
    mg.say_bye

    // Change the name to an array of names
    mg.names = List("Albert", "Brenda", "Charles",
            "Dave", "Englebert")
    mg.say_hi
    mg.say_bye

    // Change to nil
    mg.names = null
    mg.say_hi
    mg.say_bye
  }
}
Above is not really Scala idiomatic, but close to the original Ruby code especially how the class is created and used.  Ruby still looks nice, but not as shiny as a few years ago.  I am not here to say Scala is better than Ruby, but more of what is shiny before may not be as shiny today.  Just like Java was shiny when I first time used it about 1996, it seems these days people see Java as the wart language.  Scala will loose its shiny appeal in the next few years too.

I also been using the term "shiny", as its really about appeal.  I still use Java and occasionally Perl, even if the are not shiny anymore.  These older languages are still great in their own right.  C is still cool these days.. I think.

Thursday, June 28, 2012

Explaining C++, Java and Scala to my son

Last night my son was asking about computers again.  I think the conversation came about, what language is on my screen.  I told him its Java and Scala.  I talked about what is assembly, C, C++ and Objective-C and how it relates to Scratch and Kojo.

He asked about why it was named as C++, then I showed him a Java typical loop which is based C where we increment a counter.

int c = 0;
while (c <= 10) {
 System.out.println(c);
 c++;
}



He asked how the loop will be done in Scala.  I started w/ a for comprehension, as he is familiar w/ loops on Scratch and Kojo.

for (c <- 0 to 10) {
 println(c)
}
Then he asked me about the for, <-, etc.

I then changed the code into

0 to 10 foreach println


His reaction "that is so awesome it's like english!"

Wednesday, August 18, 2010

Looking at Scala again

I have been look at scala and lift (web framework built on scala), on and off about a year now. I have revisited it again for the past few weeks. Scala as a language seems to be promising. For me it adds something new to the main stream languages today, like pattern matching (not regexp), functional programming, actors library. There are some languages that have these properties but I think scala has got some transitional properties giving some balance. Using scala you get to access java libraries natively, despite its terse syntax it is still a static language, this would mean tool developers will have an easier.

Scala
Lift

I have also been re-using emacs again. Some interesting scala emacs some stuff.

Scala emacs tools