Apple WWDC Keynote 2015

Some quick thoughts after today’s WWDC Keynote, which I “watched” mostly through Twitter reactions and the first half hour of live streaming…

  • The split screen functionality they showed on Mac OS X will work great on the new larger iPads, won’t it? Can’t wait! I hope they make a larger screen iPad — I just want to see comic books at an even bigger size. The color and reproduction are already better digitally, but if the page size effectively gets larger than print, I’ll be very happy.

  • Swift 2 is going Open Source? Awesome! I’m busy with Elixir again, but I still really want to learn Swift, too. I even want to try my hand at a Mac app someday. Someday

Things we learned only after the event, but which are more exciting to me than all the bullet point feature lists:

  • There’s no separate developer’s program for iOS and Mac OS X. Now, it’s $99 to program for everything. That includes the Watch OS and, one might guess for the future, the Apple TV App Store. As Apple continues to spread the operating systems out and make them more interconnected, this is a smart move.

  • You can now load your apps on your iPhone without a developer’s license. But you could always do that before, with the exception of certain APIs which wouldn’t work — most notably for me, the Media stuff, like playing music from iTunes. Sounds like you can load everything now, which is pretty cool. It basically means you only need to pay the $99 when you’re read to ship an app to the App Store.

  • Apple believes nobody uses a case on their iPhone or iPad. The sight of 10 year old kids walking down the street with their unprotected iPhones made me laugh in this video. No parent is going to let that happen. I think the only case I saw in the whole video was on the iPad being used by the deaf boy near the end, and that’s just to help prop the iPad up. It is, of course, the official Apple iPad case…

I need to watch the rest of the event to see what I missed next…

Swift Factorial

When learning a new language, it’s a good time to go back to the computer programming classics and see what you can do with them.

The first thing I went for was Factorial. Here’s my first script:

    func factorial(var value: Int, var result: Int = 1) -> Int {

        if (value == 0) {
                return result
        }

        result *= value
        value--

        return factorial(value, result: result)
    }

    > factorial(3)
    6

There’s some stuff to cover in here. Take the function declaration for starters:

    func factorial(var value: Int, var result: Int = 1) -> Int {

Coming from Perl and Elixir and Ruby, it feels crowded. It is, however, useful. It defines a lot of stuff ahead of time for the compiler and IDE. Xcode can sense it a mile away now when you’re using the function and auto-completes stuff for you. (Unlike Objective-C, you don’t need a separate header file now.) The compiler can test stuff along the way, too, like making sure you’re sending in the correct type, number of parameters, etc.

So, you start with func as the function keyword. Each parameter has a name and type specified. I’m using the ‘var’ in front of their names because I’m mutating their state inside the function. I need to define them as new variables. Without that var, Swift assumes these are constants. An error message will tell you that these are ‘let’ values, which is Swift-ese for “constants”, which is to say immutable.

With the second parameter, I thought I’d make it easy on the user and define a default value. result is the accumulator. Its state will change along the way as you multiply the numbers together. Since this is planned to be a recursive function, we can’t ignore it completely. It has to be there. So it gets both the var tag and the = 1 part at the end to define a default value.

When you call the function this way, you only need to supply one value, like so:

    > factorial(3)
    6

(The Swift playground displays the results off on the right sidebar of the window, not below it. But I don’t want to insert a bunch of screen grabs here, so I’ll stick to a more commmand line-looking style to show things off like this. The Xcode REPL is pretty cool, though, and worth using.)

You can be more explicit, of course, and get the same answer:

    > factorial(3, 1)
    6

And you can be flat out silly:

    > factorial(3, 0)
    0

    > factorial(3, 2)
    12

Getting back to the code, the if statement is pretty obvious. I didn’t even look at the Swift book to program this part. It’s fairly standard across multiple modern languages. No semi-colons. Use curly braces. Double equals for testing equality. This is the base case. Once we’ve counted back to 0, we return the value.

After that, we mutate our variables. Again, standard coding stuff. Double minus for decrement and star-equals to multiply a variable by some other value.

Finally we have the recursive call, where we need to specify the accumulator now. If your function provides a default value, you have to name that parameter. Xcode is pretty good about pointing that out, but it’s worth emphasizing here.

This works, but it’s not elegant. First, there’s mutable state in there. That’s bad. Second, the user should never have to type in the default value or even have the option to. This is Factorial. The accumulator should always start at 1. I’m also not a big fan of naming the parameters if you don’t have to or in very simple examples like this one. (For the rest of the Apple frameworks, they’re very handy.) So let’s hide all that stuff separately. In Swift, functions are defined by their name and arity. (Arity means the number of parameters.) This is just like in Elixir. This makes me happy and makes for even cleaner code:

    func factorial(value: Int) -> Int {
            return factorial(value, 1)
    }

    func factorial(value: Int, result: Int) -> Int {

        if (value == 0) {
            return result
        }

        return factorial(value-1, result * value)
    }

    > factorial(3)
    6

    > factorial(3, 1)
    6

So the user has the option of putting in a value for the accumulator, but they don’t have to. I haven’t read all the documentation yet, so I’m not sure if there’s an option to make a function private. If there is, I’d hide the factorial/2 function in there, and only allow the user to use the one parameter version of the function (factorial/1), which would then make the call to the two parameter version.

The function is now also immutable. No variable state is ever changed. This is enforced by creating the function with parameters that are constants. That’s a default and unseen let before each in there. There’s no assignment operator (=) anywhere to be seen inside the code, either. That will make testing much easier as well as making my code more portable.

The more I look at the code, the more it bothers me that there’s an if statement inside the function at all. But since there’s no pattern matching for the parameters in Swift, we have to live with it. This is the extra code I’d love to write to get rid of that if statement all together:

    func factorial(0, result: Int) -> Int {
        return result
    }

Place that between the two functions and it’ll match the base case and take care of returning the final value for you, if your language defaults to the first-matching function it finds, top down. Elixir does that. I like it.

New Beginnings with Swift

Various thoughts after spending a little time with Swift:

  • Ruby Motion is putting on a brave face and saying they’re not worried. There are enough differences between Apple’s Ruby-like scripting language for iOS/OS X programming to distinguish it from their own Ruby scripting for iOS/OS X. Plus, they have Android support. This is true, but how many Android developers can afford to pay for Ruby Motion? I think a lot of people won’t be updating their copy of Ruby Motion in the next year or so. Others are so through-and-through Ruby that that’s more important and they’ll stick around. But I wouldn’t put bets on Ruby Motion being a growing platform anymore.

  • Swift’s function definitions annoyed me at first. They’re very verbose, defining all their specific arguments and types along with more syntax to define a return type right at the top. I understand that structure is good and takes a lot of guess work and errors out of the game, but it struck me as being too wordy. Then I remembered that this is a language meant to work with Apple’s frameworks like Cocoa. I pictured how Objective-C worked and instantly realized what a necessity this format is. It’s not the square brackets that’ll drive you nuts in Objective-C; it’s all the ridiculously long names in function calls and parameters. That’s why XCode is a necessity, and such a valuable tool.

  • Swift is the oddest looking Perl variant I’ve ever seen. Of course, the kids these days will compare it to Rust and Ruby and Python — all of which are next generation Perl wannabes, anyway. So, perhaps Swift is just not your grandfather’s scripting language of choice.

  • It’s funny to see the Open Source zealots complaining that Swift isn’t open source while using their Android phones, which are Open Sourced long after their programmed and pretty much useless to anyone else anyway.

  • I’m glad to have been doing a lot of functional programming lately, because Swift is very functional. I recognize a lot of the same tricks here. It’s funny because I was learning Objective-C and Ruby at around the same time and learned a lot of object oriented programming lessons from doing two different OO languages at the same time. Now, I have Elixir and Swift both working on similar concepts. Maybe the two will feed into each other for me. Here’s hoping…

  • In the end, the most important thing about Swift is the same as with Objective-C: It’s just syntax to deliver the frameworks. The real knowledge you need isn’t in how to set up a loop or increment a variable. It’s in knowing what all those APIs do, and which ones are available to you, and how to efficiently look them up when you need the help.

  • It still blows my mind that they spent four years developing Swift and word never leaked out about it. And they even have the bible of the programming language written up and available on the first day of its release. It’s a well-written guide. I wish it had a couple tutorials in there, but I know one of those is available already on the Developers site, and I’m sure more will come. It’s impressive.