Skip to content

Commit

Permalink
update to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tiger Chow committed Jan 17, 2017
1 parent f9f281f commit 45eb081
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
17 changes: 14 additions & 3 deletions Rational.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Buttons and Lights LLC";
TargetAttributes = {
32A7BB541B6F78BB00B699B5 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0820;
};
32A7BB5D1B6F78BB00B699B5 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0820;
};
32CD448F1B63F95F0042E80A = {
CreatedOnToolsVersion = 7.0;
Expand Down Expand Up @@ -445,7 +447,7 @@
32A7BB671B6F78BB00B699B5 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -458,14 +460,15 @@
PRODUCT_NAME = $PROJECT_NAME;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
32A7BB681B6F78BB00B699B5 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -478,6 +481,7 @@
PRODUCT_NAME = $PROJECT_NAME;
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -497,6 +501,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.buttonsAndLights.Rational-iOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -514,6 +519,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.buttonsAndLights.Rational-iOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand All @@ -531,8 +537,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
Expand Down Expand Up @@ -578,8 +586,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
Expand All @@ -599,6 +609,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 2 additions & 2 deletions Rational/Source/Int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Darwin

extension Int {
internal func divMod(other:Int) -> (quotient:Int, modulus:Int) {
internal func divMod(_ other:Int) -> (quotient:Int, modulus:Int) {
let quotient = self / other
let remainder = self % other

Expand All @@ -29,7 +29,7 @@ extension Int {
}
}

internal func quotRem(other:Int) -> (quotient:Int, remainder:Int) {
internal func quotRem(_ other:Int) -> (quotient:Int, remainder:Int) {
return (self / other, self % other)
}
}
36 changes: 18 additions & 18 deletions Rational/Source/Rational.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import Darwin

infix operator { }
infix operator

internal func <T, U, V>(f: U -> V, g: T -> U) -> T -> V {
internal func <T, U, V>(f: @escaping (U) -> V, g: @escaping (T) -> U) -> (T) -> V {
return { f(g($0)) }
}

Expand All @@ -27,7 +27,7 @@ public struct Rational {
self.init(verifiedNumerator: tuple.numerator, verifiedDenominator: tuple.denominator)
}

private init(verifiedNumerator numerator: Int, verifiedDenominator denominator:Int) {
fileprivate init(verifiedNumerator numerator: Int, verifiedDenominator denominator:Int) {
precondition(denominator != 0)

self.numerator = numerator
Expand All @@ -50,7 +50,7 @@ public struct Rational {
}
}

extension Rational : IntegerLiteralConvertible, FloatLiteralConvertible {
extension Rational : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral {
// Initialize a Rational from an integer or an integer literal.
public init(integerLiteral value: Int) {
self.init(verifiedNumerator: value, verifiedDenominator: 1)
Expand All @@ -66,23 +66,23 @@ extension Rational : IntegerLiteralConvertible, FloatLiteralConvertible {

let thePower:Int = 10 ** Rational.findExponentForNumber(value)

let isNegative = value.isSignMinus
let isNegative = value.sign == .minus
let theTuple:(n:Int, d:Int) = Rational.normalizeSign(numerator: (isNegative ? -1 : 1) * lround(value * Double(thePower)), denominator: thePower)
let theSimpleTuple:(Int, Int) = Rational.lowestTerms(numerator: theTuple.n, denominator: theTuple.d)
self.init(verified:(theSimpleTuple.0 * (isNegative ? -1 : 1), theSimpleTuple.1))
}
}

extension Rational {
internal static func normalizeSign(numerator numerator: Int, denominator:Int) -> (Int, Int) {
internal static func normalizeSign(numerator: Int, denominator:Int) -> (Int, Int) {
if denominator < 0 {
return (-numerator, -denominator)
} else {
return (numerator, denominator)
}
}

internal static func lowestTerms(numerator numerator: Int, denominator:Int) -> (Int, Int) {
internal static func lowestTerms(numerator: Int, denominator:Int) -> (Int, Int) {
let common = greatestCommonDivisor(abs(numerator), abs(denominator))
return (sNumerator: numerator/common, sDenominator: denominator/common)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ extension Rational {

/// Return the greatest common factor for the two integer arguments. This function uses the iterative Euclidean
/// algorithm for calculating GCF.
public static func greatestCommonDivisor(lhs: Int, _ rhs: Int) -> Int {
public static func greatestCommonDivisor(_ lhs: Int, _ rhs: Int) -> Int {
var a = lhs
var b = rhs
while b != 0 {
Expand All @@ -164,41 +164,41 @@ extension Rational {
return a
}

public static func leastCommonMultiple(lhs: Int, _ rhs: Int) -> Int {
public static func leastCommonMultiple(_ lhs: Int, _ rhs: Int) -> Int {
return (abs(lhs) / greatestCommonDivisor(lhs, rhs)) * abs(rhs)
}

public static func findLeastCommonDenominatorAndApply<T>(lhs: Rational, rhs: Rational, transform:(lhsNumerator: Int, rhsNumerator:Int, denominator: Int) -> T) -> T {
public static func findLeastCommonDenominatorAndApply<T>(_ lhs: Rational, rhs: Rational, transform:(_ lhsNumerator: Int, _ rhsNumerator:Int, _ denominator: Int) -> T) -> T {
let newDenominator = Rational.leastCommonDenominator(lhs, rhs)

let leftNumerator = lhs.numerator * (newDenominator / lhs.denominator)
let rightNumerator = rhs.numerator * (newDenominator / rhs.denominator)

return transform(lhsNumerator: leftNumerator, rhsNumerator: rightNumerator, denominator: newDenominator)
return transform(leftNumerator, rightNumerator, newDenominator)
}

/* @todo figure out a good name 2015-07-28 */
public static func findLeastCommonDenominatorAndOperateOnNumerators(lhs: Rational, rhs: Rational, computeNumerator:(newLeftNumerator: Int, newRightNumerator:Int) -> Int) -> Rational {
public static func findLeastCommonDenominatorAndOperateOnNumerators(_ lhs: Rational, rhs: Rational, computeNumerator:(_ newLeftNumerator: Int, _ newRightNumerator:Int) -> Int) -> Rational {

return findLeastCommonDenominatorAndApply(lhs, rhs: rhs) { (newLeftNumerator, newRightNumerator, denominator) -> Rational in
let theTuple = Rational.normalizeSign(numerator: computeNumerator(newLeftNumerator: newLeftNumerator, newRightNumerator: newRightNumerator), denominator: denominator)
let theTuple = Rational.normalizeSign(numerator: computeNumerator(newLeftNumerator, newRightNumerator), denominator: denominator)
return Rational(verified: theTuple)
}

}

static func greatestCommonDenominator(lhs: Rational, _ rhs: Rational) -> Int {
static func greatestCommonDenominator(_ lhs: Rational, _ rhs: Rational) -> Int {
return greatestCommonDivisor(lhs.denominator, rhs.denominator)
}

static func leastCommonDenominator(lhs: Rational, _ rhs: Rational) -> Int {
static func leastCommonDenominator(_ lhs: Rational, _ rhs: Rational) -> Int {
return leastCommonMultiple(lhs.denominator, rhs.denominator)
}

private static func findExponentForNumber(input:Double) -> Int {
fileprivate static func findExponentForNumber(_ input:Double) -> Int {

func _findExp(accum:Int, _ remainder:Double) -> (accum:Int, remainder:Double) {
if remainder % 1 == 0 {
func _findExp(_ accum:Int, _ remainder:Double) -> (accum:Int, remainder:Double) {
if remainder.truncatingRemainder(dividingBy: 1) == 0 {
return (accum, remainder)
} else {
return _findExp(accum + 1, remainder * 10)
Expand Down
4 changes: 2 additions & 2 deletions RationalTests/IntTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import XCTest
import SwiftCheck
@testable import Rational

@warn_unused_result public func ==(lhs:(quotient: Int, modulus: Int), rhs:(quotient: Int, modulus: Int)) -> Bool {
public func ==(lhs:(quotient: Int, modulus: Int), rhs:(quotient: Int, modulus: Int)) -> Bool {
return (lhs.quotient == rhs.quotient) && (lhs.modulus == rhs.modulus)
}

@warn_unused_result public func ==(lhs:(quotient: Int, remainder: Int), rhs:(quotient: Int, remainder: Int)) -> Bool {
public func ==(lhs:(quotient: Int, remainder: Int), rhs:(quotient: Int, remainder: Int)) -> Bool {
return (lhs.quotient == rhs.quotient) && (lhs.remainder == rhs.remainder)
}

Expand Down
11 changes: 7 additions & 4 deletions RationalTests/RationalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import SwiftCheck
@testable import Rational

extension Rational : Arbitrary {
internal static func create(numerator n: Int) -> Int -> Rational {
internal static func create(numerator n: Int) -> (Int) -> Rational {
return { d in Rational(n, d)! }
}

public static var arbitrary: SwiftCheck.Gen<Rational> {
return Rational.create <^> Int.arbitrary <*> Int.arbitrary.suchThat { $0 != 0 }
return Gen<Rational>.compose { c in
let d = c.generate(using: Int.arbitrary.suchThat { $0 != 0 })
return Rational(c.generate(), d)!
}
}

public static func shrink(value: Rational) -> [Rational] {
public static func shrink(_ value: Rational) -> [Rational] {
let numerators = value.numerator...0
let denominators = value.denominator..<0

Expand Down Expand Up @@ -106,7 +109,7 @@ class RationalTests: XCTestCase {
property("Multiplication's effect pivots on multiplicative identity") <- forAll { (input:(Int, Rational)) in
let (i, r) = input
let absI = abs(i)
return (absI != 0) && (r.numerator != 0) ==> {
return ((absI != 0) && (r.numerator != 0)) ==> {
if r.numerator > r.denominator {
return Rational(absI) * r > Rational(absI )
} else if r.numerator < r.denominator {
Expand Down

0 comments on commit 45eb081

Please sign in to comment.