Functional Programming Principles in Scala


Recently I wrote about staring a class with Coursera. The class I am taking, Functional Programming Principles in Scala is now into its second week and I am pleased with my progress. It hasn’t by any stretch been easy - after 35+ years of imperative programming trying to think in terms of recursion and functions is difficult.

I would love to write about the assignments we’ve been given and about my solutions and how I arrived at them. However, since this course, like many on Coursera, offers a certificate to participants who successfully complete the course work, there is an honor code that I am bound by that includes an injunction against making solutions to assignments available to anyone else. In one of the class forums someone asked if the solutions would be provided after the deadline for the assignment had passed and the staff response was, in a word, no. There are plans to re-offer this course in the future and it would be too great an effort if they had to update the assignments, and the automatic grading, for each class offering.

Having said that there are still some aspects of functional programming I think I can talk about here without violating the honor code. Perhaps the two biggest insights I’ve gained from the first two sets of lectures center on recursion and functions as types.

Recursion is a concept I was already familiar with but not one I’ve made great use of in my programming career. While I was in college one of my assignments was to write a program that would solve the Towers of Hanoi puzzle. This is the game where you have three posts, one of which has a stack of disks on it. The disks are stacked from largest (on the bottom) to smallest (on the top). You have to move the entire stack to a new post with out putting a larger disk over a smaller one. Programmatically the solution makes use of recursion. Recursion simply means a subroutine or function that calls itself. As long as there is a condition that terminates the recursion it works. What I remember most about the Towers problem is that the number of moves can be calculated as the 2 raised to the number of disks minus one. 3 disks = 2**3 - 1, or 7 moves. We were told not to test our solutions with any more than 5 or 6 disks as we could tie up the processor on the mainframe.

One of the tenets of functional programming is immutable variables. Most (all?) imperative languages have mutable variables. You can create an accumulator and increment it every time set condition happens. At the end of the execution you’ll know how many times that condition occurred. While there is nothing wrong with mutable state, it becomes vastly more complicated when you start having multi-core processors. Spreading the computing load of your application out over multiple processors, simultaneously, makes it possible for one instance of the code to be impacted by mutations from another instance of the code. Eliminating mutable variables avoids this complication making it easier to support concurrent execution on multiple processors.

Instead of having mutable variables you have functions, which can recursively call themselves logically mimicking the effect of mutable variables without physically altering state. At first blush this is a difficult concept to wrap your head around, especially if it has been marinating in a mutation happy imperative programming world for several decades. Not only does functional programming make heavy use of recursion, it makes use of functions as types.

Data is described in terms of its type. Character data has one type, while numeric has another. Some times are subdivided: integers and floating point for example. With functional programming you can define a new type that is a function. This week we were given a type definition of template for defining sets of numbers. And using that type you could create a rule for determining if a number was even.

type Set = Int => Boolean
val evens: Set = x => x % 2 == 0

The first line defines a type, called Set as an integer that satisfies some boolean expression. The second line creates a new value, called evens that implements a rule, x % 2 == 0 to determine if the integer is an even number. (In Scala the % indicates modulo arithmetic, which simply means divide and keep the remainder, so x % 2 divides the number by two and if the remainder is 0 we know it’s an even number.)

THe hard part about evens is that is doesn’t contain a single number. It is just the rule for determining if a given integer is an even number. evens(1) would return false as 1 is not an even number, evens(2) would return true as 2 is even. I spent most of the week struggling with the idea that any Set I defined didn’t actually hold any numbers - it just held a rule for determining membership to the set.

Our assignment this week centered around creating some functions to manipulate our newly defined Set type, including intersections, unions, differences, and contains. The hardest of these required determining if the numbers granted membership to the Set also satisfied the membership requirements of a predicate (another Set) either singly or the whole set. All week I kept thinking I could do this with a loop and some tasty mutable variables - but that would be imperative coding and not functional. Once I found the solutions to the problems I was struck with how simple they were - in most cases a single line of code. Simple in implementation but massively dense in concept.

Functional programming has its roots in lambda calculus, and I suspect a healthy does of symbolic logic. Calculus and algebra where not my favorite subjects in school, so I’m not sure I’ll ever be a functional programming convert. I am convinced that the future of programming will depend upon functional concepts as our processors aren’t going to get faster, they are just going to have more cores. And in order to utilize those cores applications will need to be capable of maintaining their internal start without mutable variables.


You Say You Want an Education?


A fascinating article by Adam Fletcher which outlines a four-year computer science curriculum using Coursera offerings. It even leaves room for non-computer science based courses to round out the knowledge base gained. While I don’t think brick-and-mortar schools are over, I do think that in addition to their physical campuses, universities need to develop robust, active, and engaging virtual campuses.


A Toy Train in Space


Simply wonderful.


Tornado Tracks


I’ve lived most of my life under one or more of these tornado tracks.


Coursera


Through a colleague I recently learned about Coursera. Coursera provides over 120 free, online courses from 16 top universities. What sets Coursera apart from other online course offerings is the level of interaction and feedback provided. I have watched a couple of Stanford courses in the past year, working through the assignments on my own. While I have learned from these experiences, there has been no official feedback from the lecturer and no way for me to participate in the class.

Coursera aims to provide an interactive experience, complete with grading and, in most cases, some kind of certificate upon successful completion of the course material. Lectures are broken into short 8 to 12 minute videos about a topic. These videos have embedded questions in them that all students must answer before continuing. Assignments are machine graded making it possible for very large groups of students to receive feedback about their work.

This TED talk by one of the founders of Coursera, Daphne Koller, explains the genesis of Coursera and their goals and ambitions.

I’m signed up for Functional Programming Principles in Scala, which is offered by École Polytechnique Fédérale de Lausanne and taught by Scala’s creator Martin Odersky. Through a forum posting on the course page I understand that there are over 30,000 other registrants for this course. So far I’ve completed the tools setup and example assignment. I’ve also been active in the course forums. The actual course begins this week and runs for the next seven weeks.

I am excited about the potential distributed learning like this offers. That I can take a class about Scala with the language creator - for free - along with 30,000 other people is astonishing. There will always be a need for brick-and-mortar universities, however the ability to augment and enhance one’s education with interactive online education from top educators global represents the future of education. I’m thrilled that I have this opportunity and I look forward to participating in many more Coursera courses.


Display Notification Center Alerts on Remote Machines


During evenings and over weekends my Internet browsing may lead me to items I want to pursue while at work. In the past I have sent my work email account mail with URLs to articles or software that I wanted to look into further while at work. The problem with this practice is that my work email account is read by my personal computer, so I end up with an unread email badge staring me in the face all weekend long.

Matt Gemmell’s recently released Sticky Notification tool allows you to quickly create a sticky notification (one that must be dismissed). This is great for ADD types like myself, to create reminders of what I was working on when I have to leave a task before it is completed for some reason. What would make it even better would be a way to create sticky reminders on remote machines.

One of the hints today on the Mac OS X Hints page is a method for creating Notification Center alerts from the command line. Using that hint, a short shell function, and the ability to shell into my work computer remotely, I am now able to create new notices on my work computer.

##Step 1: Install Display Notification Center Alert action The hint on Mac OS X Hints relies upon an Automator action created by Automated Workflows. Download the Display Notification Center Alert Automator Action and follow the directions for installing it. (Basically double-click on the action file.)

##Step 2: Create a DisplayNotification workflow Fire up Automator and create a new workflow. Locate the freshly installed Display Notification Center Alert action from the list and drag it to your workflow. Follow the directions in the command line Notification Center Alert hint and create three variables (title, subtitle, and message) and associate them with their corresponding fields in the action. Finally save the action. I followed the hint’s suggestion and created a ~/Library/Workflows directory and saved my new workflow there.

##Step 3: Test the workflow From the command line test your new action to make sure it works. Once you tire of creating new alerts move on to Step 4.

$ automator -D title='Testing' -D subtitle='testing' -D message='Is this thing on?' 
~/Library/Workflows/DisplayNotification.wflow

(Note: command should be all on one line, it’s broken into two lines here for readability concerns.)

##Step 4: Create a shell function to call the Automator workflow Rather than have to type the entire command necessary to run the workflow each time I want to create a new alert, I created a short shell function to encapsulate it.

notify() {
  automator -D title=$1 -D subtitle=$2 -D message=$3 ~/Library/Workflow/DisplayNotification.wflow
}

With this function in place all I need to do to create a new alert is

notify 'This is my alert' 'It is a very fine alert' 'Filled with alert goodness'

##Step 5: Profit! Now I can create alerts on any of the (Mountain Lion) computers I use, from any other computer I use simply by secure shelling into them and running one command.

##Update After getting all of this to work, you may wish to visit the Notifications panel of System Preferences. In the list of alerts under “in Notification Center” you’ll find an entry for “Automator Runner”. Changing its display style from the default “Banners” to “Alerts” will cause any command line triggered alert to be sticky, requiring your dismissal. Excellent.


Facebook Email Invasion


Apple’s latest update to iOS, the operating system that powers iPhones and iPads, is expected to be announced (if not delivered) within the next two or three weeks. On anticipated change in the OS is tighter integration with Facebook. I admit to having strong reservations about this turn of events. Ever since the the whole Beacon debacle with Facebook I have had an uneasy, tentative relationship with the service and its privacy (or lack thereof) practices.

This morning I learned that iOS 6 may allow Facebook to spam my contact list with their odious email addresses. This TÚAW article, “[With iOS 6, your address book may be invaded by @facebook.com email addresses](http://www.tuaw.com/2012/09/04/with-ios-6-your-address-book-may-be-invaded-by-facebook-com-em/ “With iOS 6, your address book may be invaded by @facebook.com email addresses””)", outlines what appears to be coming.

I will be waiting to turn on any Facebook integration in iOS 6 to see what others experience first. If what this article is saying proves to be true, and Facebook is spamming address books with their @facebool.com email address, then I will not be enabling Facebook integration on my phone. And I have absolutely no intention of ever using Facebook’s email.


Xcode Project Template Copyright Fixed


The latest version of Xcode, 4.4.1 (4F1003), addresses the issue of organization name and copyright. Previously the copyright information was collected from your address book entry. In my case I had a single address card for myself that included my employer’s name. Every new project I’d create in Xcode spammed my employer’s name into the project’s copyright line. Through an obscure defaults write command I was able to force the organization name to be me.

With the release of version 4.4.1 you no longer need to worry about organization name. The new project creation panel allows you to set an organization name per project. This value is used for the “Created by” attribution line as well as for the “Copyright” line in the header on each file created for your project.

Xcode now lets you set an organization name at project creation time


Hallux Rigidus


More than twelve years ago I started having pain in my left heel. Turns out it was a heel spur. The spur was reduced through a series of ultrasound treatments at my chiropractor’s office. Several months after my left heel was treated I developed the same pain in my right foot. Another round of ultrasound reduced that spur too.

My Left foot

In 2004 I started having constant pain in my right big toe. Massaging the toe at the end of the day helped some, but over time the pain grew worse and worse. I tried different shoes, icing, and over-the-counter pain medicines but got no long lasting relief.

In 2006 I went to see a chiropractor in Overland Park where I was living at the time to see what could be done. As soon as I explained my history of heel spurs and the pain in my toe she told my I had fallen arches. Sure enough, when I stand bare foot my arches are flat.

Since that day I have religiously worn arch supports in all my shoes. I never go bare foot. The arch supports helped but didn’t eliminate the pain. My toe often appears to be swollen and the base knuckle (metatarsophalangeal joint or MPT joint) is enlarged. When I use my fingers to explore the MTP on both my feet I can tell there is a large lump of something in my right foot.

This morning I met with an orthopedic surgeon to confirm that this was in fact arthritis, and to discuss treatment options.

It is arthritis and the specific condition is called hallus rigidus or stiff big toe. X-rays taken today confirm that there are a couple of bone spurs in that joint and one rather large “loose” piece of calcification. Loose in the sense that it isn’t attached to any bone in my foot. It’s held in place by the tissues in my foot so it isn’t moving around. Much.

Note the enlarged knuckle on the right big toe

There are several options for treatment. I can continue to live with it, using over-the-counter pain and anti-inflammatory medicines as needed to manage the pain. Or, I can have a Cheilectomy (kI-lek’-toe-me) to remove the bone spurs, the lose bit of calcification, and enough surrounding bone to return my toe to a “normal” range of motion. Or, I can have a Arthrodesis (are-throw-dee’-sis) to fuse the joint together permanently.

Oblique view of right foot

The surgeon said that my case didn’t scream out a need for the fusion. In the fusion all the cartilage in the effected joint is removed and a metal plate and screws are used to fuse the bones together. That joint is given a slight (30º) upward angle to facilitate walking without a limp. This surgery is done once and the hope is that it eliminates the source of the pain (an arthritic joint with bone spurs) forever.

The cheilectomy removes the osteophytes (bone spurs) that have formed allowing the toe to return to normal range of motion. In my case there is still some cartilage in the joint so it function properly. There is no guarantee that arthritis won’t return to the joint, therefore the surgery may need to be repeated in 5 or 10 years.

Side view showing bone spur and loose calcification

The recovery time varies from patient to patient, but is a minimum of 2-weeks in a wooden sandal to support the toe, followed by a gradual return to normal footgear. Swelling is the major side effect of the surgery, and can persist for several months after the procedure.

Since I’m committed to orchestra this fall, with rehearsals every Saturday and a concert in November, I am planning on having the cheilectomy performed the week after Thanksgiving. Once the pain from the procedure abates I am looking forward to vastly reduced soreness and pain in my toe for the next decade.


Deleting Apple System Logs to Speed Up Zsh


For the past year I have been using the oh-my-zsh framework to configure my zsh prompt and environment. My one complaint about oh-my-zsh is its slowness spawning a new shell. When I use Cmd-T in iTerm to create a new tab I often have to wait 3 or 4 or even 5 seconds for that new shell to become active. This is unacceptable.

Searching for “speed up oh-my-zsh” lead me to the zsh stars incredibly slowly discussion. From there I went to speed up a slow terminal by clearing log files. My /private/var/logs/asl directory did have some *.asl or Apple System Log Files. Using the suggested rm command I deleted all *.asl files in my /private/var/logs/asl directory.

$ sudo rm /private/var/logs/asl/*.asl

Prior to deleting the *.asl files I tested the start time of my shell like this:

$ time zsh -i -c exit
zsh -i -c exit 0.35s user 0.40s system 12% cpu 5.953 total

After deleting the *.asl files the time dropped considerably:

$ time zsh -i -c exit
zsh -i -c exit 0.27s user 0.21s system 96% cpu 0.496 total

NB: As with any low-level mucking about on your computer, proceed at your own risk. I don’t fully understand everything that ASL files are used for. Deleting the set my computer has accumulated seems to have considerably speed ip spawning new shells without any detrimental side effects. Your experience may vary.