Info

Programmer by day, artist by night

Apple’s latest language Swift provides some nifty behaviors and characteristics. Three things we are loving about Swift so far are:

  1. Awesome OOP features: Structs, Enums, Classes can all have methods, optional binding, type safety
  2. Cool Functional language features: Closures (the shorthand syntax is a delight to use), nested functions and types
  3. Interesting Cocoa binding behaviors, as if the language was written to build beautiful Cocoa apps

We are getting solid feeling of how Swift was designed to create powerful and elegant programs. Experience will tell more down the line.

Here is a fun experiment with prefix operators. We wrote the following two custom global functions to mimic Lisp’s + prefix operator:

@prefix func + (ints: Int[]) -> Int {
    var sum = 0
    for int in ints {
        sum += int
    }
    return sum
}
@prefix func + (strings: String[]) -> String {
    var sum = ""
    for string in strings {
        sum += string
    }
    return sum
}

+[100, 200, 200]
// returns 500

+["comp", "uter"]
// returns "computer"

Comments

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS