Learning Swift and the Best of Both Worlds play


Well, having suffered through Objective C and liked XCode compiler, it has sure been interesting to see what Apple has come up next with Swift. It's the brain child of Chris Lattner. He's had a pretty incredible career, developing LLVM (a compiler backend) and then Clang (a front end) and finally XCode that's been used for Apple's development tools.
The Swift uses LLVM and runs in the same environment as Objective C, but has scripting like constructs built in as well as being strongly typed, so it is in the middle ground between scripting simplicity and the power of C. The iBook is actually a pretty good description of everything that it can do with native support for sequences, lists, ranges, it is pretty neat. If we were doing a marketingplaybook.com essay, it would be about how Swift is the best of compiled and interpreted languages:

  • It has the really nice abbreviated syntax of a Python but has the strict type checking of Objective C (sometime even more so)
  • It has the power of classes and because it uses the LLVM backend, you can mix and match it with Objective C

And Playgrounds let's you make changes and insert code and see what is happening on the right pane. Pretty cool because Swift incrementally compiles so it really is the best of both worlds.
Others are doing the same thing with Google Go and Facebook Hack, so there's so much more for programmers to learn. How fun is that!
Check this out for coolness where a data structure as a series of sequences and dictionaries and this looks at all the numbers in the sequences to find the largest number:

let? ?interestingNumbers? = [
? ?"Prime"?: [?2?, ?3?, ?5?, ?7?, ?11?, ?13?],
? ?"Fibonacci"?: [?1?, ?1?, ?2?, ?3?, ?5?, ?8?],
? ?"Square"?: [?1?, ?4?, ?9?, ?16?, ?25?],
?var? ?largest? = ?0
?for? (?kind?, ?numbers?) ?in? ?interestingNumbers? {
? ?for? ?number? ?in? ?numbers? {
? ?if? ?number? > ?largest? {
? ?largest? = ?number
? }
? }

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
This thing definitely has it's subtleties, for instance the use of question mark to say that the variable may have a legitmate value or a nil is pretty cool but different syntax. Touch-magazine explains it pretty well though.
First there is this cool idea of a data type type that applies to all types, so this means you can make any type Optional:

enum Optional : NilLiteralConvertible {
case None
case Some(T)

This interesting syntax means that there are two cases, with a nil for case and then Some(T) means just return the type. So it allows something like OptionalInt which says it can be a nil or an integer value. So that Int? just maps to Optional<Int> 
There is a different use of the question mark, where it is syntactic sugar that says if the object is a nil then don't execute it.  Finally, there is the ability to cast things as needed, so there is both "upcasting" and "downcasting", so you there is this idiom that says takes something and cast it down to its more basic component. For instance, if a string is an object, then the "if let" gives you a nice temporary constant to refer to it as that "downcast" item.

var str : String
if let object=str is NSObject {
   println("I'm an object at my core")

Similarly you can upcast something, so for instance with the as? it means that if you can see if it is the higher level object or through a forced conversion

var floatValue : Float
if let intValue=floatValue as? Int {

println("I can treat this object (floatValue) as a integer (intValue)by a forced cast which cuts off the decimal part for now")

Some other amazing things about it are the way it uses closures to supply functions to things. That's incredibly useful and concise. For instance, to supply a sort function, instead of having to send it a pointer to a function in C, you just give it some code as an argument. The syntax is interesting, the function isOrderedBefore takes two types and returns a boolean, if it is a closure, you don't parentheses to enclose it. Also closures like this can use $0, $1 like shell scripts so you don't have to name the incoming arguments:

// function definition
mutating func sort(isOrderedBefore: (T, T) -> Bool)
// usage
var array = [ 5, 3, 2, 7 ]
array.sort { $0 < $1 }


It also directly supports the Hadoop style map/reduce where map basically does things in parallel and reduce takes an entire array and turns it into a single number

// definition says, you map with type U and you supply a transform which takes as an argument any type T and returns the type U, the map itself returns something of type U
func map(transform: (T) -> U) -> [U]
var array = [ "Brick", "Bar", "Yellow" ]
let found = array.map { $0 == "Brick" }
// Should return [ true, false, false ]
// definition of reduce looking for any trues in the string
func reduce
(initial: U, combine: (U, T) -> U) -> U
// usage is a little strange, the first argument is enclosed in parentheses, but the closure is out of the parentheses
let inArrayAtAll = found.reduce(false) { $0 || $1 }

Related Posts