Steve Levine

Archive for the ‘Development’ tag

Scala reduceLeft

without comments

This post was updated on November 19, 2009.

As you know, during the past few weeks (time permitting), I have been spending time studying the Scala programming language. After reading the first few chapters of Programming In Scala, I have come across the first feature of Scala that would have been totally useful for me on one of my Java programming tasks a few months ago.

The actual programming task was quite complex, but for the purposes of this post, we will work with a dramatically simplified example. The simplified task is: given a list of Stocks figure out which one has the highest earnings per share.

Here is one way the problem can be solved in Scala:

class Stock(val ticker: String, var price: Double, var earnings: Double, var shares: Int) {
  override def toString = ticker + " has eps of: " + eps + " with a price of " + price
  def eps = earnings / shares
  def >(that: Stock): Boolean = {eps > that.eps}
}

val portfolio = List(
  new Stock("aapl", 203, 4.73E9, 878876000),
  new Stock("ibm", 66, 1.23E10, 1384331000),
  new Stock("goog", 465, 4.19E9, 314754113)
)

println(
  portfolio.reduceLeft(
    (s1, s2) => if (s1 > s2) s1 else s2
   )
)
// goog has eps of: 13.31

Download Source: Stock.scala, reduceLeft.scala

If you have never seen Scala code before, this code will look quite foreign to you because there are a lot of concepts in Scala that are not present in Java. In a few weeks (again time permitting), will begin writing a series on Scala for Java programmers, but for right now, please bear with me, and try to follow along.

The most interesting things to me about this code are:

  • Line 04: You are reading it correct, that is operator overloading!
    • Correction: Scala doesn’t actually support “operator overloading”, in fact, “operators are not special language syntax”. In Scala any method can be an operator. What makes an operator an operator is how you use it. Example:

      1 + 2  //operator
      1.+2   //not operator (but returns wrong type)
      (1).+(2) // Correct return type
      
  • Line 07: It is great to be able to populate a list so easily without having to mess around with add methods.
  • Line 14: This is the coolest feature of them all so far, the ability to reduce a list of elements down to one based on a binary operator.

This is just a small demonstration of the very basics of the Scala language. If you find this at all interesting, you can find out much more at scala-lang.org.

Note: obviously, this is not the most optimal Scala solution so if your a Scala guru, criticisms are welcome. I hope to come back to this example every few weeks, continually improving it as my knowledge of Scala grows.

References

  • The code found in this post is hosted at github.com along with other sample Scala code.

This entry was posted by Steve on Thursday, November 19th, 2009 at 5:52 pm and is filed under: , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Simple Applescript For The Traveling Mac

without comments

The Problem

You have a MacBook and a nice Apple Cinema display (this doesn’t sound like a problem so far), and you travel with the MacBook every day.  When you open the lid of your MacBook you like to have the Dock on the left side of the screen giving you the most top to bottom space, but when you come home and connect the MacBook to your Cinema display and set up dual monitors, you want the dock on the bottom of the Cinema, not on the left side of the MacBook.

You don’t want to have to go in to preferences every time to switch the location. (or maybe you do?)  For me it was becoming a very tedious task, so I began researching ways to automate it.

The Solution

Leverage Apple’s “Language of Automation”, Applescript to handle the task.  Applescript has lots of useful hooks in to OS X.

The requirements for the script are quite simple:

  • Obtain the current resolution of the primary monitor
  • if the resolution is > 1900 (Cinema Display) configure the Dock for large display
  • else configure the Dock for laptop display

For the moment, that is my goal, simple, yet time saving.

Step 1: Open AppleScript Editor

Step 2: Paste the following code in

Step 3: Run it to make sure it works as expected, if so, save the script as application, so you don’t need to open AppleScript Editor each time you want to run it.



Future requirements

  • Figure out how to hook the script in to sleep/wake events in Snow Leopard.
  • Customize more than just the Dock.
  • Migrate it in to a startup script that brings up all necessary applications based upon current mood (reading/blogging/coding).
  • Others???

Overall, I find Applescript a very easy way to automate things in Snow Leopard.

All of the source for this post can be found on github.com.  Please feel free to fork and improve.

Enjoy.

This entry was posted by Steve on Thursday, November 12th, 2009 at 10:46 pm and is filed under: , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

iPhone 3.0 upgrade from beta 5 to final

with one comment

For the past few months, I have been running the iPhone OS 3.0 betas on my iPhone without too many issues.  Thus I assumed that when the final version of OS 3 was released, it would be easy to upgrade to it. This has not been the case.  The current beta on my iPhone was beta 5, and then yesterday when the final version was announced, I assumed after a simple plug in to iTunes, I would be upgraded to the released version.  Nope, iTunes kept telling me that I have the latest version of iPhone OS 3.  It seems as though it is not checking the build number, only the OS level.  The next thing I tried was doing a restore, this didn’t work either because it gave me an error saying it could not connect to the site to download the file.

Finally, after some Googling around, I found that others with the same problem circumvented it by putting their iPhone in recovery mode and then upgrading from there.

If you are currently running beta 5, you were supposed to upgrade to the GM version posted last week.  I skipped it thinking their would be another release in the next few days with the final version.  I have now learned that the GM version is the same as the final version.  Unfortunately, now it is too late to download the GM from the developer website, as Apple has taken it down, and according to their documentation, you should obtain the final version through iTunes.  Thus if you are in the same boat as me, on beta 5, you will have to install the latest version via recovery mode.

This entry was posted by Steve on Thursday, June 18th, 2009 at 12:40 pm and is filed under: , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Easy Upgrades

without comments

Who doesn’t like easy upgrades? Easy upgrades are great when implemented correctly. There are two speciifiic easy upgrades I have been (enjoying) using recently, namely, Wordpress and Ubuntu Server.

Wordpress

This blog has been powered by Wordpress for a long time, and has been upgraded many times along the way.  Download the latest tgz file, unzip it, save your wp-content directory, upgrade your database, and then you are good to go.  Keep in mind this was a very simple procedure, as they call it their “famous 5-minute installation”, which it typically was.

Once upgrading to 1.7, the procedure became as easy as clicking a link on the admin page, and stepping through a wizzard  This blog is now running 1.8, so it has been through several automatic upgrades already, and each one of them has been completely boring, which when talking about upgrading an environment, is a very good thing.  This magic does not only apply to upgrading the entire platform, as you can automatically upgrade your plugins as well.

Ubuntu

Many years ago I set up several Ubuntu Gutsy Gibbon (7.10) Servers in my basement to serve different purposes on my internal network.  These servers have been running without issue for the past 2 years.  They would probably run seamlessly for another 2 years, but unfortunately, I needed to install a new package that didn’t have a deb available for the current version of Ubuntu.  At first I was trying to find ways around this, installing the sources instead, but that didn’t work well for me, as it needed a newer version of a dependent library.   Even considered trying a different software package, but it seemed like all of the software packages I was trying to install, required a newer version of Ubuntu.

What was I left to do?  Either upgrade or rebuild from scratch.  Based on past experiences with other distros, upgrades have been nothing but headaches for me.  Since, I really didn’t want to invest the time to rebuild the server from scratch, decided to try my first Ubuntu upgrade.  Again, based on my past experiences, I assumed I needed to download the media, and then hook up a monitor to the server, and finally go through a set of upgrade screens.

To my delight, after a little bit of research I found that you can do the entire upgrade from the command line, no user interface required, no media download required, no monitor required!  This was too good to be true right?  Well, no.  I ran the do-release-upgrade command, and now my server is running Hardy Heron (8.04). Couldn’t be any easier.  Ubuntu’s upgrade infrastructure is very impressive.

What is the moral of the story?  If you want to build a loyal user community, treat the members of the community well by providing them with an easy upgrade path.

This entry was posted by Steve on Monday, June 15th, 2009 at 5:49 pm and is filed under: , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Fork me on GitHub Fork me on GitHub