From 8f251e6756ead4c1087057a4db3ec1fe8450b6e4 Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Mon, 2 Jun 2014 15:51:44 -0400 Subject: [PATCH] Adds basic support for the Swift programming language Text only lexer for now until Pygments catches up --- lib/linguist/languages.yml | 6 + lib/linguist/samples.json | 265 ++++++++++++++++++++++++++++++++- samples/Swift/section-11.swift | 4 + samples/Swift/section-13.swift | 8 + samples/Swift/section-15.swift | 2 + samples/Swift/section-17.swift | 1 + samples/Swift/section-19.swift | 10 ++ samples/Swift/section-21.swift | 8 + samples/Swift/section-23.swift | 11 ++ samples/Swift/section-25.swift | 14 ++ samples/Swift/section-27.swift | 11 ++ samples/Swift/section-29.swift | 11 ++ samples/Swift/section-3.swift | 1 + samples/Swift/section-31.swift | 4 + samples/Swift/section-33.swift | 4 + samples/Swift/section-35.swift | 9 ++ samples/Swift/section-37.swift | 9 ++ samples/Swift/section-39.swift | 8 + samples/Swift/section-41.swift | 13 ++ samples/Swift/section-43.swift | 5 + samples/Swift/section-45.swift | 1 + samples/Swift/section-47.swift | 1 + samples/Swift/section-49.swift | 6 + samples/Swift/section-5.swift | 3 + samples/Swift/section-51.swift | 3 + samples/Swift/section-53.swift | 12 ++ samples/Swift/section-55.swift | 20 +++ samples/Swift/section-57.swift | 26 ++++ samples/Swift/section-59.swift | 21 +++ samples/Swift/section-61.swift | 8 + samples/Swift/section-63.swift | 2 + samples/Swift/section-65.swift | 21 +++ samples/Swift/section-67.swift | 3 + samples/Swift/section-69.swift | 17 +++ samples/Swift/section-7.swift | 3 + samples/Swift/section-71.swift | 9 ++ samples/Swift/section-73.swift | 14 ++ samples/Swift/section-75.swift | 4 + samples/Swift/section-77.swift | 20 +++ samples/Swift/section-79.swift | 9 ++ samples/Swift/section-81.swift | 3 + samples/Swift/section-83.swift | 8 + samples/Swift/section-85.swift | 7 + samples/Swift/section-87.swift | 11 ++ samples/Swift/section-9.swift | 3 + 45 files changed, 636 insertions(+), 3 deletions(-) create mode 100644 samples/Swift/section-11.swift create mode 100644 samples/Swift/section-13.swift create mode 100644 samples/Swift/section-15.swift create mode 100644 samples/Swift/section-17.swift create mode 100644 samples/Swift/section-19.swift create mode 100644 samples/Swift/section-21.swift create mode 100644 samples/Swift/section-23.swift create mode 100644 samples/Swift/section-25.swift create mode 100644 samples/Swift/section-27.swift create mode 100644 samples/Swift/section-29.swift create mode 100644 samples/Swift/section-3.swift create mode 100644 samples/Swift/section-31.swift create mode 100644 samples/Swift/section-33.swift create mode 100644 samples/Swift/section-35.swift create mode 100644 samples/Swift/section-37.swift create mode 100644 samples/Swift/section-39.swift create mode 100644 samples/Swift/section-41.swift create mode 100644 samples/Swift/section-43.swift create mode 100644 samples/Swift/section-45.swift create mode 100644 samples/Swift/section-47.swift create mode 100644 samples/Swift/section-49.swift create mode 100644 samples/Swift/section-5.swift create mode 100644 samples/Swift/section-51.swift create mode 100644 samples/Swift/section-53.swift create mode 100644 samples/Swift/section-55.swift create mode 100644 samples/Swift/section-57.swift create mode 100644 samples/Swift/section-59.swift create mode 100644 samples/Swift/section-61.swift create mode 100644 samples/Swift/section-63.swift create mode 100644 samples/Swift/section-65.swift create mode 100644 samples/Swift/section-67.swift create mode 100644 samples/Swift/section-69.swift create mode 100644 samples/Swift/section-7.swift create mode 100644 samples/Swift/section-71.swift create mode 100644 samples/Swift/section-73.swift create mode 100644 samples/Swift/section-75.swift create mode 100644 samples/Swift/section-77.swift create mode 100644 samples/Swift/section-79.swift create mode 100644 samples/Swift/section-81.swift create mode 100644 samples/Swift/section-83.swift create mode 100644 samples/Swift/section-85.swift create mode 100644 samples/Swift/section-87.swift create mode 100644 samples/Swift/section-9.swift diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index 24e27408..974bde31 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -2046,6 +2046,12 @@ SuperCollider: extensions: - .scd +Swift: + type: programming + lexer: Text only + extensions: + - .swift + SystemVerilog: type: programming color: "#343761" diff --git a/lib/linguist/samples.json b/lib/linguist/samples.json index e7206004..ac1a92ea 100644 --- a/lib/linguist/samples.json +++ b/lib/linguist/samples.json @@ -581,6 +581,9 @@ "SuperCollider": [ ".scd" ], + "Swift": [ + ".swift" + ], "SystemVerilog": [ ".sv", ".svh", @@ -723,8 +726,8 @@ ".gemrc" ] }, - "tokens_total": 611474, - "languages_total": 755, + "tokens_total": 612602, + "languages_total": 798, "tokens": { "ABAP": { "*/**": 1, @@ -61881,6 +61884,260 @@ "wait": 1, ".fork": 1 }, + "Swift": { + "let": 43, + "apples": 1, + "oranges": 1, + "appleSummary": 1, + "fruitSummary": 1, + "var": 42, + "shoppingList": 3, + "[": 18, + "]": 18, + "occupations": 2, + "emptyArray": 1, + "String": 27, + "(": 89, + ")": 89, + "emptyDictionary": 1, + "Dictionary": 1, + "": 1, + "Float": 1, + "//": 1, + "Went": 1, + "shopping": 1, + "and": 1, + "bought": 1, + "everything.": 1, + "individualScores": 2, + "teamScore": 4, + "for": 10, + "score": 2, + "in": 11, + "{": 77, + "if": 6, + "+": 15, + "}": 77, + "else": 1, + "optionalString": 2, + "nil": 1, + "optionalName": 2, + "greeting": 2, + "name": 21, + "vegetable": 2, + "switch": 4, + "case": 21, + "vegetableComment": 4, + "x": 1, + "where": 2, + "x.hasSuffix": 1, + "default": 2, + "interestingNumbers": 2, + "largest": 4, + "kind": 1, + "numbers": 6, + "number": 13, + "n": 5, + "while": 2, + "<": 4, + "*": 7, + "m": 5, + "do": 1, + "firstForLoop": 3, + "i": 6, + "secondForLoop": 3, + ";": 2, + "println": 1, + "func": 24, + "greet": 2, + "day": 1, + "-": 21, + "return": 30, + "getGasPrices": 2, + "Double": 11, + "sumOf": 3, + "Int...": 1, + "Int": 19, + "sum": 3, + "returnFifteen": 2, + "y": 3, + "add": 2, + "makeIncrementer": 2, + "addOne": 2, + "increment": 2, + "hasAnyMatches": 2, + "list": 2, + "condition": 2, + "Bool": 4, + "item": 4, + "true": 2, + "false": 2, + "lessThanTen": 2, + "numbers.map": 2, + "result": 5, + "sort": 1, + "class": 7, + "Shape": 2, + "numberOfSides": 4, + "simpleDescription": 14, + "myVariable": 2, + "myConstant": 1, + "shape": 1, + "shape.numberOfSides": 1, + "shapeDescription": 1, + "shape.simpleDescription": 1, + "NamedShape": 3, + "init": 4, + "self.name": 1, + "Square": 7, + "sideLength": 17, + "self.sideLength": 2, + "super.init": 2, + "area": 1, + "override": 2, + "test": 1, + "test.area": 1, + "test.simpleDescription": 1, + "EquilateralTriangle": 4, + "perimeter": 1, + "get": 2, + "set": 1, + "newValue": 1, + "/": 1, + "triangle": 3, + "triangle.perimeter": 2, + "triangle.sideLength": 2, + "TriangleAndSquare": 2, + "willSet": 2, + "square.sideLength": 1, + "newValue.sideLength": 2, + "square": 2, + "size": 4, + "triangleAndSquare": 1, + "triangleAndSquare.square.sideLength": 1, + "triangleAndSquare.triangle.sideLength": 2, + "triangleAndSquare.square": 1, + "Counter": 2, + "count": 2, + "incrementBy": 1, + "amount": 2, + "numberOfTimes": 2, + "times": 4, + "counter": 1, + "counter.incrementBy": 1, + "optionalSquare": 2, + ".sideLength": 1, + "enum": 4, + "Rank": 2, + "Ace": 1, + "Two": 1, + "Three": 1, + "Four": 1, + "Five": 1, + "Six": 1, + "Seven": 1, + "Eight": 1, + "Nine": 1, + "Ten": 1, + "Jack": 1, + "Queen": 1, + "King": 1, + "self": 3, + ".Ace": 1, + ".Jack": 1, + ".Queen": 1, + ".King": 1, + "self.toRaw": 1, + "ace": 1, + "Rank.Ace": 1, + "aceRawValue": 1, + "ace.toRaw": 1, + "convertedRank": 1, + "Rank.fromRaw": 1, + "threeDescription": 1, + "convertedRank.simpleDescription": 1, + "Suit": 2, + "Spades": 1, + "Hearts": 1, + "Diamonds": 1, + "Clubs": 1, + ".Spades": 2, + ".Hearts": 1, + ".Diamonds": 1, + ".Clubs": 1, + "hearts": 1, + "Suit.Hearts": 1, + "heartsDescription": 1, + "hearts.simpleDescription": 1, + "implicitInteger": 1, + "implicitDouble": 1, + "explicitDouble": 1, + "struct": 2, + "Card": 2, + "rank": 2, + "suit": 2, + "threeOfSpades": 1, + ".Three": 1, + "threeOfSpadesDescription": 1, + "threeOfSpades.simpleDescription": 1, + "ServerResponse": 1, + "Result": 1, + "Error": 1, + "success": 2, + "ServerResponse.Result": 1, + "failure": 1, + "ServerResponse.Error": 1, + ".Result": 1, + "sunrise": 1, + "sunset": 1, + "serverResponse": 2, + ".Error": 1, + "error": 1, + "protocol": 1, + "ExampleProtocol": 5, + "mutating": 3, + "adjust": 4, + "SimpleClass": 2, + "anotherProperty": 1, + "a": 2, + "a.adjust": 1, + "aDescription": 1, + "a.simpleDescription": 1, + "SimpleStructure": 2, + "b": 1, + "b.adjust": 1, + "bDescription": 1, + "b.simpleDescription": 1, + "extension": 1, + "protocolValue": 1, + "protocolValue.simpleDescription": 1, + "repeat": 2, + "": 1, + "ItemType": 3, + "OptionalValue": 2, + "": 1, + "None": 1, + "Some": 1, + "T": 5, + "possibleInteger": 2, + "": 1, + ".None": 1, + ".Some": 1, + "anyCommonElements": 2, + "": 1, + "U": 4, + "Sequence": 2, + "GeneratorType": 3, + "Element": 3, + "Equatable": 1, + "lhs": 2, + "rhs": 2, + "lhsItem": 2, + "rhsItem": 2, + "label": 2, + "width": 2, + "widthLabel": 1 + }, "SystemVerilog": { "module": 3, "endpoint_phy_wrapper": 2, @@ -66478,6 +66735,7 @@ "Stata": 3133, "Stylus": 76, "SuperCollider": 133, + "Swift": 1128, "SystemVerilog": 541, "Tcl": 1133, "Tea": 3, @@ -66663,6 +66921,7 @@ "Stata": 7, "Stylus": 1, "SuperCollider": 1, + "Swift": 43, "SystemVerilog": 4, "Tcl": 2, "Tea": 1, @@ -66686,5 +66945,5 @@ "YAML": 2, "Zephir": 2 }, - "md5": "a7d557f9fecf06444c92c22e6f32daa7" + "md5": "e41e5530c6efe39b710ace7e1d5d318c" } \ No newline at end of file diff --git a/samples/Swift/section-11.swift b/samples/Swift/section-11.swift new file mode 100644 index 00000000..3be056ab --- /dev/null +++ b/samples/Swift/section-11.swift @@ -0,0 +1,4 @@ +let apples = 3 +let oranges = 5 +let appleSummary = "I have \(apples) apples." +let fruitSummary = "I have \(apples + oranges) pieces of fruit." diff --git a/samples/Swift/section-13.swift b/samples/Swift/section-13.swift new file mode 100644 index 00000000..06589fd7 --- /dev/null +++ b/samples/Swift/section-13.swift @@ -0,0 +1,8 @@ +var shoppingList = ["catfish", "water", "tulips", "blue paint"] +shoppingList[1] = "bottle of water" + +var occupations = [ + "Malcolm": "Captain", + "Kaylee": "Mechanic", + ] +occupations["Jayne"] = "Public Relations" diff --git a/samples/Swift/section-15.swift b/samples/Swift/section-15.swift new file mode 100644 index 00000000..3641de25 --- /dev/null +++ b/samples/Swift/section-15.swift @@ -0,0 +1,2 @@ +let emptyArray = String[]() +let emptyDictionary = Dictionary() diff --git a/samples/Swift/section-17.swift b/samples/Swift/section-17.swift new file mode 100644 index 00000000..e079c3b2 --- /dev/null +++ b/samples/Swift/section-17.swift @@ -0,0 +1 @@ +shoppingList = [] // Went shopping and bought everything. diff --git a/samples/Swift/section-19.swift b/samples/Swift/section-19.swift new file mode 100644 index 00000000..262512fc --- /dev/null +++ b/samples/Swift/section-19.swift @@ -0,0 +1,10 @@ +let individualScores = [75, 43, 103, 87, 12] +var teamScore = 0 +for score in individualScores { + if score > 50 { + teamScore += 3 + } else { + teamScore += 1 + } +} +teamScore diff --git a/samples/Swift/section-21.swift b/samples/Swift/section-21.swift new file mode 100644 index 00000000..fb06bd77 --- /dev/null +++ b/samples/Swift/section-21.swift @@ -0,0 +1,8 @@ +var optionalString: String? = "Hello" +optionalString == nil + +var optionalName: String? = "John Appleseed" +var greeting = "Hello!" +if let name = optionalName { + greeting = "Hello, \(name)" +} diff --git a/samples/Swift/section-23.swift b/samples/Swift/section-23.swift new file mode 100644 index 00000000..09f4747c --- /dev/null +++ b/samples/Swift/section-23.swift @@ -0,0 +1,11 @@ +let vegetable = "red pepper" +switch vegetable { + case "celery": + let vegetableComment = "Add some raisins and make ants on a log." + case "cucumber", "watercress": + let vegetableComment = "That would make a good tea sandwich." + case let x where x.hasSuffix("pepper"): + let vegetableComment = "Is it a spicy \(x)?" + default: + let vegetableComment = "Everything tastes good in soup." +} diff --git a/samples/Swift/section-25.swift b/samples/Swift/section-25.swift new file mode 100644 index 00000000..80e19f9c --- /dev/null +++ b/samples/Swift/section-25.swift @@ -0,0 +1,14 @@ +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 + } + } +} +largest diff --git a/samples/Swift/section-27.swift b/samples/Swift/section-27.swift new file mode 100644 index 00000000..e2feda12 --- /dev/null +++ b/samples/Swift/section-27.swift @@ -0,0 +1,11 @@ +var n = 2 +while n < 100 { + n = n * 2 +} +n + +var m = 2 +do { + m = m * 2 +} while m < 100 +m diff --git a/samples/Swift/section-29.swift b/samples/Swift/section-29.swift new file mode 100644 index 00000000..b269cf97 --- /dev/null +++ b/samples/Swift/section-29.swift @@ -0,0 +1,11 @@ +var firstForLoop = 0 +for i in 0..3 { + firstForLoop += i +} +firstForLoop + +var secondForLoop = 0 +for var i = 0; i < 3; ++i { + secondForLoop += 1 +} +secondForLoop diff --git a/samples/Swift/section-3.swift b/samples/Swift/section-3.swift new file mode 100644 index 00000000..47ea7d53 --- /dev/null +++ b/samples/Swift/section-3.swift @@ -0,0 +1 @@ +println("Hello, world") diff --git a/samples/Swift/section-31.swift b/samples/Swift/section-31.swift new file mode 100644 index 00000000..9b77ea62 --- /dev/null +++ b/samples/Swift/section-31.swift @@ -0,0 +1,4 @@ +func greet(name: String, day: String) -> String { + return "Hello \(name), today is \(day)." +} +greet("Bob", "Tuesday") diff --git a/samples/Swift/section-33.swift b/samples/Swift/section-33.swift new file mode 100644 index 00000000..89361a75 --- /dev/null +++ b/samples/Swift/section-33.swift @@ -0,0 +1,4 @@ +func getGasPrices() -> (Double, Double, Double) { + return (3.59, 3.69, 3.79) +} +getGasPrices() diff --git a/samples/Swift/section-35.swift b/samples/Swift/section-35.swift new file mode 100644 index 00000000..c724746f --- /dev/null +++ b/samples/Swift/section-35.swift @@ -0,0 +1,9 @@ +func sumOf(numbers: Int...) -> Int { + var sum = 0 + for number in numbers { + sum += number + } + return sum +} +sumOf() +sumOf(42, 597, 12) diff --git a/samples/Swift/section-37.swift b/samples/Swift/section-37.swift new file mode 100644 index 00000000..f78fdbe9 --- /dev/null +++ b/samples/Swift/section-37.swift @@ -0,0 +1,9 @@ +func returnFifteen() -> Int { + var y = 10 + func add() { + y += 5 + } + add() + return y +} +returnFifteen() diff --git a/samples/Swift/section-39.swift b/samples/Swift/section-39.swift new file mode 100644 index 00000000..d2fa5fe3 --- /dev/null +++ b/samples/Swift/section-39.swift @@ -0,0 +1,8 @@ +func makeIncrementer() -> (Int -> Int) { + func addOne(number: Int) -> Int { + return 1 + number + } + return addOne +} +var increment = makeIncrementer() +increment(7) diff --git a/samples/Swift/section-41.swift b/samples/Swift/section-41.swift new file mode 100644 index 00000000..e72d410a --- /dev/null +++ b/samples/Swift/section-41.swift @@ -0,0 +1,13 @@ +func hasAnyMatches(list: Int[], condition: Int -> Bool) -> Bool { + for item in list { + if condition(item) { + return true + } + } + return false +} +func lessThanTen(number: Int) -> Bool { + return number < 10 +} +var numbers = [20, 19, 7, 12] +hasAnyMatches(numbers, lessThanTen) diff --git a/samples/Swift/section-43.swift b/samples/Swift/section-43.swift new file mode 100644 index 00000000..5dee10f7 --- /dev/null +++ b/samples/Swift/section-43.swift @@ -0,0 +1,5 @@ +numbers.map({ + (number: Int) -> Int in + let result = 3 * number + return result +}) diff --git a/samples/Swift/section-45.swift b/samples/Swift/section-45.swift new file mode 100644 index 00000000..dbf29eed --- /dev/null +++ b/samples/Swift/section-45.swift @@ -0,0 +1 @@ +numbers.map({ number in 3 * number }) diff --git a/samples/Swift/section-47.swift b/samples/Swift/section-47.swift new file mode 100644 index 00000000..73defb77 --- /dev/null +++ b/samples/Swift/section-47.swift @@ -0,0 +1 @@ +sort([1, 5, 3, 12, 2]) { $0 > $1 } diff --git a/samples/Swift/section-49.swift b/samples/Swift/section-49.swift new file mode 100644 index 00000000..69739b8d --- /dev/null +++ b/samples/Swift/section-49.swift @@ -0,0 +1,6 @@ +class Shape { + var numberOfSides = 0 + func simpleDescription() -> String { + return "A shape with \(numberOfSides) sides." + } +} diff --git a/samples/Swift/section-5.swift b/samples/Swift/section-5.swift new file mode 100644 index 00000000..885d5812 --- /dev/null +++ b/samples/Swift/section-5.swift @@ -0,0 +1,3 @@ +var myVariable = 42 +myVariable = 50 +let myConstant = 42 diff --git a/samples/Swift/section-51.swift b/samples/Swift/section-51.swift new file mode 100644 index 00000000..a84750c8 --- /dev/null +++ b/samples/Swift/section-51.swift @@ -0,0 +1,3 @@ +var shape = Shape() +shape.numberOfSides = 7 +var shapeDescription = shape.simpleDescription() diff --git a/samples/Swift/section-53.swift b/samples/Swift/section-53.swift new file mode 100644 index 00000000..74aac461 --- /dev/null +++ b/samples/Swift/section-53.swift @@ -0,0 +1,12 @@ +class NamedShape { + var numberOfSides: Int = 0 + var name: String + + init(name: String) { + self.name = name + } + + func simpleDescription() -> String { + return "A shape with \(numberOfSides) sides." + } +} diff --git a/samples/Swift/section-55.swift b/samples/Swift/section-55.swift new file mode 100644 index 00000000..6e489078 --- /dev/null +++ b/samples/Swift/section-55.swift @@ -0,0 +1,20 @@ +class Square: NamedShape { + var sideLength: Double + + init(sideLength: Double, name: String) { + self.sideLength = sideLength + super.init(name: name) + numberOfSides = 4 + } + + func area() -> Double { + return sideLength * sideLength + } + + override func simpleDescription() -> String { + return "A square with sides of length \(sideLength)." + } +} +let test = Square(sideLength: 5.2, name: "my test square") +test.area() +test.simpleDescription() diff --git a/samples/Swift/section-57.swift b/samples/Swift/section-57.swift new file mode 100644 index 00000000..90d2e03c --- /dev/null +++ b/samples/Swift/section-57.swift @@ -0,0 +1,26 @@ +class EquilateralTriangle: NamedShape { + var sideLength: Double = 0.0 + + init(sideLength: Double, name: String) { + self.sideLength = sideLength + super.init(name: name) + numberOfSides = 3 + } + + var perimeter: Double { + get { + return 3.0 * sideLength + } + set { + sideLength = newValue / 3.0 + } + } + + override func simpleDescription() -> String { + return "An equilateral triagle with sides of length \(sideLength)." + } +} +var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle") +triangle.perimeter +triangle.perimeter = 9.9 +triangle.sideLength diff --git a/samples/Swift/section-59.swift b/samples/Swift/section-59.swift new file mode 100644 index 00000000..21e3b09d --- /dev/null +++ b/samples/Swift/section-59.swift @@ -0,0 +1,21 @@ +class TriangleAndSquare { + var triangle: EquilateralTriangle { + willSet { + square.sideLength = newValue.sideLength + } + } + var square: Square { + willSet { + triangle.sideLength = newValue.sideLength + } + } + init(size: Double, name: String) { + square = Square(sideLength: size, name: name) + triangle = EquilateralTriangle(sideLength: size, name: name) + } +} +var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape") +triangleAndSquare.square.sideLength +triangleAndSquare.triangle.sideLength +triangleAndSquare.square = Square(sideLength: 50, name: "larger square") +triangleAndSquare.triangle.sideLength diff --git a/samples/Swift/section-61.swift b/samples/Swift/section-61.swift new file mode 100644 index 00000000..013ded74 --- /dev/null +++ b/samples/Swift/section-61.swift @@ -0,0 +1,8 @@ +class Counter { + var count: Int = 0 + func incrementBy(amount: Int, numberOfTimes times: Int) { + count += amount * times + } +} +var counter = Counter() +counter.incrementBy(2, numberOfTimes: 7) diff --git a/samples/Swift/section-63.swift b/samples/Swift/section-63.swift new file mode 100644 index 00000000..9305cfc7 --- /dev/null +++ b/samples/Swift/section-63.swift @@ -0,0 +1,2 @@ +let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square") +let sideLength = optionalSquare?.sideLength diff --git a/samples/Swift/section-65.swift b/samples/Swift/section-65.swift new file mode 100644 index 00000000..809c71ce --- /dev/null +++ b/samples/Swift/section-65.swift @@ -0,0 +1,21 @@ +enum Rank: Int { + case Ace = 1 + case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten + case Jack, Queen, King + func simpleDescription() -> String { + switch self { + case .Ace: + return "ace" + case .Jack: + return "jack" + case .Queen: + return "queen" + case .King: + return "king" + default: + return String(self.toRaw()) + } + } +} +let ace = Rank.Ace +let aceRawValue = ace.toRaw() diff --git a/samples/Swift/section-67.swift b/samples/Swift/section-67.swift new file mode 100644 index 00000000..d5be66c2 --- /dev/null +++ b/samples/Swift/section-67.swift @@ -0,0 +1,3 @@ +if let convertedRank = Rank.fromRaw(3) { + let threeDescription = convertedRank.simpleDescription() +} diff --git a/samples/Swift/section-69.swift b/samples/Swift/section-69.swift new file mode 100644 index 00000000..fc6d7a4c --- /dev/null +++ b/samples/Swift/section-69.swift @@ -0,0 +1,17 @@ +enum Suit { + case Spades, Hearts, Diamonds, Clubs + func simpleDescription() -> String { + switch self { + case .Spades: + return "spades" + case .Hearts: + return "hearts" + case .Diamonds: + return "diamonds" + case .Clubs: + return "clubs" + } + } +} +let hearts = Suit.Hearts +let heartsDescription = hearts.simpleDescription() diff --git a/samples/Swift/section-7.swift b/samples/Swift/section-7.swift new file mode 100644 index 00000000..7d55c034 --- /dev/null +++ b/samples/Swift/section-7.swift @@ -0,0 +1,3 @@ +let implicitInteger = 70 +let implicitDouble = 70.0 +let explicitDouble: Double = 70 diff --git a/samples/Swift/section-71.swift b/samples/Swift/section-71.swift new file mode 100644 index 00000000..f006d0f9 --- /dev/null +++ b/samples/Swift/section-71.swift @@ -0,0 +1,9 @@ +struct Card { + var rank: Rank + var suit: Suit + func simpleDescription() -> String { + return "The \(rank.simpleDescription()) of \(suit.simpleDescription())" + } +} +let threeOfSpades = Card(rank: .Three, suit: .Spades) +let threeOfSpadesDescription = threeOfSpades.simpleDescription() diff --git a/samples/Swift/section-73.swift b/samples/Swift/section-73.swift new file mode 100644 index 00000000..be0cf35f --- /dev/null +++ b/samples/Swift/section-73.swift @@ -0,0 +1,14 @@ +enum ServerResponse { + case Result(String, String) + case Error(String) +} + +let success = ServerResponse.Result("6:00 am", "8:09 pm") +let failure = ServerResponse.Error("Out of cheese.") + +switch success { + case let .Result(sunrise, sunset): + let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)." + case let .Error(error): + let serverResponse = "Failure... \(error)" +} diff --git a/samples/Swift/section-75.swift b/samples/Swift/section-75.swift new file mode 100644 index 00000000..46dd36da --- /dev/null +++ b/samples/Swift/section-75.swift @@ -0,0 +1,4 @@ +protocol ExampleProtocol { + var simpleDescription: String { get } + mutating func adjust() +} diff --git a/samples/Swift/section-77.swift b/samples/Swift/section-77.swift new file mode 100644 index 00000000..bc052d20 --- /dev/null +++ b/samples/Swift/section-77.swift @@ -0,0 +1,20 @@ +class SimpleClass: ExampleProtocol { + var simpleDescription: String = "A very simple class." + var anotherProperty: Int = 69105 + func adjust() { + simpleDescription += " Now 100% adjusted." + } +} +var a = SimpleClass() +a.adjust() +let aDescription = a.simpleDescription + +struct SimpleStructure: ExampleProtocol { + var simpleDescription: String = "A simple structure" + mutating func adjust() { + simpleDescription += " (adjusted)" + } +} +var b = SimpleStructure() +b.adjust() +let bDescription = b.simpleDescription diff --git a/samples/Swift/section-79.swift b/samples/Swift/section-79.swift new file mode 100644 index 00000000..0e7b831b --- /dev/null +++ b/samples/Swift/section-79.swift @@ -0,0 +1,9 @@ +extension Int: ExampleProtocol { + var simpleDescription: String { + return "The number \(self)" + } + mutating func adjust() { + self += 42 + } + } +7.simpleDescription diff --git a/samples/Swift/section-81.swift b/samples/Swift/section-81.swift new file mode 100644 index 00000000..74e0a000 --- /dev/null +++ b/samples/Swift/section-81.swift @@ -0,0 +1,3 @@ +let protocolValue: ExampleProtocol = a +protocolValue.simpleDescription +// protocolValue.anotherProperty // Uncomment to see the error diff --git a/samples/Swift/section-83.swift b/samples/Swift/section-83.swift new file mode 100644 index 00000000..aa2e6d75 --- /dev/null +++ b/samples/Swift/section-83.swift @@ -0,0 +1,8 @@ +func repeat(item: ItemType, times: Int) -> ItemType[] { + var result = ItemType[]() + for i in 0..times { + result += item + } + return result +} +repeat("knock", 4) diff --git a/samples/Swift/section-85.swift b/samples/Swift/section-85.swift new file mode 100644 index 00000000..b2f52840 --- /dev/null +++ b/samples/Swift/section-85.swift @@ -0,0 +1,7 @@ +// Reimplement the Swift standard library's optional type +enum OptionalValue { + case None + case Some(T) +} +var possibleInteger: OptionalValue = .None +possibleInteger = .Some(100) diff --git a/samples/Swift/section-87.swift b/samples/Swift/section-87.swift new file mode 100644 index 00000000..f9dc7f33 --- /dev/null +++ b/samples/Swift/section-87.swift @@ -0,0 +1,11 @@ +func anyCommonElements (lhs: T, rhs: U) -> Bool { + for lhsItem in lhs { + for rhsItem in rhs { + if lhsItem == rhsItem { + return true + } + } + } + return false +} +anyCommonElements([1, 2, 3], [3]) diff --git a/samples/Swift/section-9.swift b/samples/Swift/section-9.swift new file mode 100644 index 00000000..2e83efa1 --- /dev/null +++ b/samples/Swift/section-9.swift @@ -0,0 +1,3 @@ +let label = "The width is " +let width = 94 +let widthLabel = label + String(width)