Skip to content

Commit

Permalink
Swift3branch (#160)
Browse files Browse the repository at this point in the history
* [wip] remove swift 3 directive

* [wip] mechanical transition with code migrator

* Swift 3 preview 1 changes

* travis xcode8 update

* travis update OS X 10.12

* updated documentation to swift 3 and Xcode 8

* Removed "Foundation not needed" comment

Unlike Objc, swift doesn’t need foundation for most simple operations.
I was planning to remove the foundation requirement but now swift 3.0
will provide a Swift version of foundation.
https://github.com/apple/swift-corelibs-foundation

* ran swiftLink locally with autocorrect

- This next step is to turn this on on Travis CI to warn on commit

* travis update with swiftlint

* update travis

* added swiftlint rules yml

* update read me with contribution guides

closes #66

* update swiftLint rules

* force swift3 for XCTest classes by using compiler directive.

By wrapping the import to only import when swift 3 is present I am
hoping to impress to users that swift 3 is required. Plus this would
make it easy to go back and keep requiring a certain a version  in the
future.

* BinarySearchTree using indirect enums

closes #152

reference:
https://airspeedvelocity.net/2015/07/22/a-persistent-tree-using-indirect
-enums-in-swift/

* update swiftlint format requirement

update swift lint doc and script in Xcode project
`swiftlint autocorrect --format`

* operator_whitespace removal

* removed leading white space

* mid triple new lines clean up

* closes #10
  • Loading branch information
masters3d authored Jul 19, 2016
1 parent b1e76a2 commit 0b8be6a
Show file tree
Hide file tree
Showing 138 changed files with 3,335 additions and 4,028 deletions.
10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable_name:
min_length:
warning: 1
error: 0

force_try: warning

disabled_rules: # rule identifiers to exclude from running
- function_body_length
- line_length
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
language: objective-c
osx_image: xcode7.3
osx_image: xcode8
xcode_project: 'xcodeProject/xSwift.xcodeproj'
xcode_scheme: 'xSwiftTests'

install:
- brew update && brew install swiftlint

script:
- xcodebuild test -project 'xcodeProject/xSwift.xcodeproj' -scheme 'xSwiftTests' -sdk macosx10.11
- swiftlint
- xcodebuild test -project 'xcodeProject/xSwift.xcodeproj' -scheme 'xSwiftTests' -sdk macosx10.12
- bin/fetch-configlet
- bin/configlet .
sudo: false
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Exercism exercises in Swift

Please see the [contributing guide](https://github.com/exercism/x-api/blob/master/CONTRIBUTING.md#the-exercise-data)

## Swift Track

We use Travis CI to automatically run all the unit tests on the code that is going to be checked in. In addition we also have [SwiftLint](https://github.com/realm/SwiftLint) set up to make sure that the code is consistent across all exercises. Please run `swiftlint autocorrect --format` on your code before submitting a PR. Also in order for the tests to run they need to be added to the `xcodeProject` project. Make sure all the tests pass locally and that you are using the latest Xcode version otherwise the Travis checks may fail.

## License

The MIT License (MIT)
Expand Down
4 changes: 2 additions & 2 deletions docs/INSTALLATION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Installing Swift

In order to use Swift, you must be running Xcode version 7.3 or greater which is available at [Apple's developer center](https://developer.apple.com/xcode/downloads/).
In order to use Swift, you must be running Xcode version 8.0 or greater which is available at [Apple's developer center](https://developer.apple.com/xcode/downloads/).


All exercises tested with Xcode 7.3 using Swift 2.2
All exercises tested with Xcode 8.0 using Swift 3
4 changes: 2 additions & 2 deletions docs/LEARNING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Exercism provides exercises and feedback, but it can be difficult to jump
into learning Swift for the first time. These
resources can help you get started:

* [Swifty App](https://itunes.apple.com/us/app/swifty-learn-how-to-code-in/id886315617?mt=8), by Johannes Berger
* [The Swift Programming Language](https://itunes.apple.com/us/book/the-swift-programming-language/id881256329?mt=11), by Apple
* [Apple Swift Website](http://www.apple.com/swift/)
* [The Swift Programming Language Website](https://swift.org/documentation/)
6 changes: 1 addition & 5 deletions exercises/accumulate/AccumulateExample.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

// Foundation not needed


extension Array {
func accumulate<S>(yield: (Element) -> S) -> [S] {
func accumulate<S>(_ yield: (Element) -> S) -> [S] {
var result: [S] = [S]()
for item in self {
result.append(yield(item))
Expand Down
95 changes: 44 additions & 51 deletions exercises/accumulate/AccumulateTest.swift
Original file line number Diff line number Diff line change
@@ -1,98 +1,91 @@

import XCTest


#if swift(>=3.0)
import XCTest
#endif

private extension String {

var length: Int {return self.characters.count}

func reverse() -> String {
var result:String = ""
var result: String = ""
for char in self.characters {
result = "\(char)\(result)"
}
return result
}

}

class AccumulateTest: XCTestCase {

func testEmptyAccumulation() {
func testEmptyAccumulation() {

let input = [Int]()
func square(input:Int) -> Int {
let input = [Int]()
func square(_ input: Int) -> Int {
return input * input
}
let result = input.accumulate(square)
}
let result = input.accumulate(square)

XCTAssertTrue(result.isEmpty)
}

func testAccumulateSquares() {
let input = [1,2,3,4]
let expected = [1,4,9,16]
func square(input:Int) -> Int {

let input = [1, 2, 3, 4]
let expected = [1, 4, 9, 16]
func square(_ input: Int) -> Int {
return input * input
}

let result = input.accumulate(square)

XCTAssertEqual(expected, result)
}


func testAccumulateUpcases() {
let input = ["hello","world"]
let expected = ["HELLO","WORLD"]
func toUpper(input:String) -> String {
return input.uppercaseString

let input = ["hello", "world"]
let expected = ["HELLO", "WORLD"]
func toUpper(_ input: String) -> String {
return input.uppercased()
}

let result = input.accumulate(toUpper)

XCTAssertEqual(expected, result)

}


}
func testAccumulateReversedStrings() {
let input = ["the","quick","brown","fox","etc"]
let expected = ["eht","kciuq","nworb","xof","cte"]
func reverse(input:String) -> String {

let input = ["the", "quick", "brown", "fox", "etc"]
let expected = ["eht", "kciuq", "nworb", "xof", "cte"]
func reverse(_ input: String) -> String {
return input.reverse()
}

let result = input.accumulate(reverse)

XCTAssertEqual(expected, result)
}

func testAccumulateRecursively() {
let input = ["a","b","c"]

let input = ["a", "b", "c"]

let expected = [
["a1","a2","a3"],
["b1","b2","b3"],
["c1","c2","c3"]
["a1", "a2", "a3"],
["b1", "b2", "b3"],
["c1", "c2", "c3"]
]
func recurse(input:String) -> [String] {
func appendTo(innerInput:String) -> String {

func recurse(_ input: String) -> [String] {
func appendTo(_ innerInput: String) -> String {
return input+innerInput
}
let result = ["1","2","3"].accumulate(appendTo)
let result = ["1", "2", "3"].accumulate(appendTo)
return result
}


let result = input.accumulate(recurse)

XCTAssertEqual(expected, result)
}
}
84 changes: 39 additions & 45 deletions exercises/acronym/AcronymExample.swift
Original file line number Diff line number Diff line change
@@ -1,80 +1,74 @@
// Foundation not needed
private extension String {



private extension String{

func substringWithRangeInt(intRange:Range<Int>)->String{
let start = self.startIndex.advancedBy(intRange.startIndex)
let end = self.startIndex.advancedBy(intRange.endIndex)
return self.substringWithRange(start..<end)
func substringWithRangeInt(_ intRange: CountableRange<Int>) -> String {
let start = self.characters.index(self.startIndex, offsetBy: intRange.lowerBound)
let end = self.characters.index(self.startIndex, offsetBy: intRange.upperBound)
return self.substring(with: start..<end)
}
func substringWithRangeInt(start start:Int, end:Int) -> String{

func substringWithRangeInt(start: Int, end: Int) -> String {
let range = start..<end
return self.substringWithRangeInt(range)
}
var isUppercase:Bool {
return self == self.uppercaseString

var isUppercase: Bool {
return self == self.uppercased()
}
var isLowercase:Bool {
return self == self.lowercaseString

var isLowercase: Bool {
return self == self.lowercased()
}
}

struct Acronym{
static func abbreviate(inString:String) -> String {
var previousLetter:String = ""
func splitCamelcaseAt(currentLetter: String, inout withString previousLetter: String ) -> Bool {
struct Acronym {

static func abbreviate(_ inString: String) -> String {

var previousLetter: String = ""

func splitCamelcaseAt(_ currentLetter: String, withString previousLetter: inout String ) -> Bool {

defer { previousLetter = currentLetter }

if currentLetter == " " { return false
} else if currentLetter.isEmpty { return false
} else if (previousLetter.isLowercase && currentLetter.isUppercase){
} else if (previousLetter.isLowercase && currentLetter.isUppercase) {
//previousLetter = currentLetter // see defer block
return true
}
//previousLetter = currentLetter // see defer block
return false
}
func insertSpaceAtCamelcase(inString:String)->String{

func insertSpaceAtCamelcase(_ inString: String) -> String {
var accumulate = ""
var lastIndexAdded = 0
for (index , each) in inString.characters.map({String($0)}).enumerate() {
if splitCamelcaseAt(each, withString: &previousLetter){

for (index, each) in inString.characters.map({String($0)}).enumerated() {
if splitCamelcaseAt(each, withString: &previousLetter) {
accumulate += inString.substringWithRangeInt(start: lastIndexAdded, end: index)+" " // inserts a space
lastIndexAdded = index
}
}
let lastStringSection = inString.substringWithRangeInt(start: lastIndexAdded, end: inString.characters.count)
return accumulate + lastStringSection
}
func splitAt(characterToCompare:Character, charToSplitAt:String = " ,-:")-> Bool{
for each in charToSplitAt.characters{
if each == characterToCompare{

func splitAt(_ characterToCompare: Character, charToSplitAt: String = " ,-:") -> Bool {
for each in charToSplitAt.characters {
if each == characterToCompare {
return true
}
}
return false
}
func splitStringToArray(inString:String) -> [String]{
return inString.characters.split(isSeparator: { splitAt($0) }).map{String($0)}

func splitStringToArray(_ inString: String) -> [String] {

return inString.characters.split(isSeparator: { splitAt($0) }).map {String($0)}
}

return splitStringToArray(insertSpaceAtCamelcase(inString)).map({$0.uppercaseString.substringWithRangeInt(start: 0, end: 1)}).joinWithSeparator("")
}

}

return splitStringToArray(insertSpaceAtCamelcase(inString)).map({$0.uppercased().substringWithRangeInt(start: 0, end: 1)}).joined(separator: "")
}

}
35 changes: 17 additions & 18 deletions exercises/acronym/AcronymTest.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@

import XCTest


#if swift(>=3.0)
import XCTest
#endif

class AcronymTest: XCTestCase {
func testAcronymAbbreviateTest1(){

func testAcronymAbbreviateTest1() {
XCTAssertEqual("PNG", Acronym.abbreviate("Portable Network Graphics"))
}
func testAcronymAbbreviateTest2(){

func testAcronymAbbreviateTest2() {
XCTAssertEqual("ROR", Acronym.abbreviate("Ruby on Rails"))
}
func testAcronymAbbreviateTest3(){

func testAcronymAbbreviateTest3() {
XCTAssertEqual("HTML", Acronym.abbreviate("HyperText Markup Language"))
}
func testAcronymAbbreviateTest4(){

func testAcronymAbbreviateTest4() {
XCTAssertEqual("FIFO", Acronym.abbreviate("First In, First Out"))
}
func testAcronymAbbreviateTest5(){

func testAcronymAbbreviateTest5() {
XCTAssertEqual("PHP", Acronym.abbreviate("PHP: Hypertext Preprocessor"))
}
func testAcronymAbbreviateTest6(){

func testAcronymAbbreviateTest6() {
XCTAssertEqual("CMOS", Acronym.abbreviate("Complementary metal-oxide semiconductor"))
}
}

}
Loading

0 comments on commit 0b8be6a

Please sign in to comment.