 
 import UIKit
 
+// Adapted from
+// http://www.raywenderlich.com/82599/swift-functional-programming-tutorial
+typealias Entry = (Character, [String])
+
+func buildIndex(words: [String]) -> [Entry] {
+    func distinct<T: Equatable>(source: [T]) -> [T] {
+        var unique = [T]()
+        for item in source {
+            if !contains(unique, item) {
+                unique.append(item)
+            }
+        }
+        return unique
+    }
+    
+    func firstLetter(str: String) -> Character {
+        return Character(str.substringToIndex(advance(str.startIndex, 1)).uppercaseString)
+    }
+
+    let letters = words.map { word -> Character in
+        firstLetter(word)
+    }
+    let distinctLetters = distinct(letters)
+    
+    return distinctLetters.map { letter -> Entry in
+        return (letter, words.filter { word -> Bool in
+            firstLetter(word) == letter
+        })
+    }
+}
+
 
 class MasterViewController: UITableViewController {
 
     var detailViewController: DetailViewController? = nil
     var selectedMovieTitle : String? = nil
-    var movies = [String]()
+    var movies = [Entry]()
+    var sections = [String]()
     var diskSpaceAvailable = ""
 
     override func awakeFromNib() {

                movie = notificationMovie
            }
            else if let indexPath = self.tableView.indexPathForSelectedRow() {
                movie = movies[indexPath.row]
            }
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.currentMovieName = movie

    func movieListLoaded(notification: NSNotification) {
        if let userInfo = notification.userInfo,
            let remoteMovies = userInfo["data"] as? [String] {
                movies = buildIndex(remoteMovies)
                tableView.reloadData()
        }
    }
