03
Jun 14

“Swift” is Syntactic Sugar

download The “new” Swift programming language from Apple is nothing more that syntactic sugar on top of Javascript.

A few examples from Swift Welcome page and Swift Tour page

Example 1:

Swift code:

============================
let people = ["Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25]
for (name, age) in people {
println("\(name) is \(age) years old.")
}
==============================

JS code ( http://jsfiddle.net/SSD9Y/1/ ) :

============================
var people = {"Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25}

for ( person in people ) {
alert( person + " is " + people[person] + " years old.")
==============================

Example 2:

Swift code:

============================
let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
let sortedCities = sort(cities) { $0 < $1 }
if let indexOfLondon = find(sortedCities, "London") {
println("London is city number \(indexOfLondon + 1) in the list")
}
==============================

JS code ( http://jsfiddle.net/pYWb9/1/ ) :

============================
var cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
var sortedCities = cities.sort()

indexOfLondon = sortedCities.indexOf("London")+1
alert("London is city number " + indexOfLondon + " in the list")
==============================

Example 3:

Swift code:

============================
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"

var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
==============================

JS code ( http://jsfiddle.net/p3FM6/1/ ):

============================
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var occupations= {Malcolm: 'Captain',Kaylee:'Mechanic'}
occupations["Jayne"] = "Public Relations"
==============================

Example 4:

Swift code:

============================
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
teamScore
==============================

JS code ( http://jsfiddle.net/s9DF5/1/ ):

============================
var individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for (score in individualScores) {
if (score > 50) {
teamScore += 3
} else {
teamScore += 1
}
==============================