Skip to content

Commit 8067d3b

Browse files
author
Hooman Mehr
committed
Cleanup
1 parent 66a8287 commit 8067d3b

File tree

3 files changed

+65
-43
lines changed

3 files changed

+65
-43
lines changed

Sources/RationalNumber.swift

+1-43
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,6 @@ extension Rational {
316316
}
317317

318318

319-
320-
/// Convert from a Double.
321-
///
322-
323-
public init(_ x: Double, tolerance: Percentage = 0.01%, preferredDenominators: [Int] = [] ) {
324-
325-
self.init(x, tolerance: x*tolerance, preferredDenominators: preferredDenominators)
326-
}
327-
328-
329319
public static func +(lhs: Rational, rhs: Rational) -> Rational {
330320

331321
let (lnum, rnum, denom) = numsWithCommonDenom(lhs, rhs)
@@ -396,39 +386,6 @@ internal func numsWithCommonDenom(_ left: Rational, _ right: Rational) -> (lnum:
396386
}
397387

398388

399-
extension String {
400-
401-
func mapped(with lookupTable: [Character:Character]) -> String {
402-
403-
return String(self.characters.map{ lookupTable[$0] ?? $0 })
404-
}
405-
}
406-
407-
408-
409-
/// Utility protocol to help write more readable code.
410-
///
411-
/// One alternative is overloading multiplication / division operators, but it would
412-
/// pollute operator space and lead to ambiguity.
413-
414-
internal protocol Scalable {
415-
416-
associatedtype ScaleFactor
417-
418-
/// Returns a new item by scaling the current item.
419-
420-
func scaled(by factor: ScaleFactor) -> Self
421-
}
422-
423-
extension Int: Scalable {
424-
425-
typealias ScaleFactor = Double
426-
427-
func scaled(by factor: Double) -> Int {
428-
429-
return Int((Double(self) * factor).rounded())
430-
}
431-
}
432389

433390
extension Rational: Scalable {
434391

@@ -439,3 +396,4 @@ extension Rational: Scalable {
439396
return Rational(numerator.scaled(by: factor), over: denominator)
440397
}
441398
}
399+

Sources/Utilities.swift

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Utilities.swift
3+
// SwiftMath
4+
//
5+
//
6+
// Global functions (public in internal) defined by SwiftMath module.
7+
// (c) 2016 Hooman Mehr. Licensed under Apache License v2.0 with Runtime Library Exception
8+
//
9+
10+
11+
12+
/// Utility protocol to help write more readable code.
13+
///
14+
/// One alternative is overloading multiplication / division operators, but it would
15+
/// pollute operator space and lead to ambiguity.
16+
17+
internal protocol Scalable {
18+
19+
20+
/// The type of scale factor supporting by this scalable type.
21+
22+
associatedtype ScaleFactor
23+
24+
25+
/// Returns a new item by scaling the current item to the given factor.
26+
///
27+
/// - Parameter factor: The given scaling factor.
28+
29+
func scaled(by factor: ScaleFactor) -> Self
30+
}
31+
32+
33+
34+
35+
extension Int: Scalable {
36+
37+
typealias ScaleFactor = Double
38+
39+
func scaled(by factor: Double) -> Int {
40+
41+
return Int((Double(self) * factor).rounded())
42+
}
43+
}
44+
45+
46+
47+
48+
extension String {
49+
50+
/// Returns a string with the given key characters replaced with corresponding value characters.
51+
///
52+
/// - Parameter lookupTable: A dictionary that mapps character keys to character values. Each
53+
/// character key in the string is replaced with the corresponding character value from this
54+
/// dictionary.
55+
56+
func mapped(with lookupTable: [Character:Character]) -> String {
57+
58+
return String(self.characters.map{ lookupTable[$0] ?? $0 })
59+
}
60+
}

SwiftMath.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
861B50B41DA97DF100C6F0B1 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 861B50B31DA97DF100C6F0B1 /* Utilities.swift */; };
1011
OBJ_25 /* Algorithms.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* Algorithms.swift */; };
1112
OBJ_26 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* Operators.swift */; };
1213
OBJ_27 /* Percentage.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* Percentage.swift */; };
@@ -26,6 +27,7 @@
2627
/* End PBXContainerItemProxy section */
2728

2829
/* Begin PBXFileReference section */
30+
861B50B31DA97DF100C6F0B1 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
2931
OBJ_10 /* Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
3032
OBJ_11 /* Percentage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Percentage.swift; sourceTree = "<group>"; };
3133
OBJ_12 /* RationalNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RationalNumber.swift; sourceTree = "<group>"; };
@@ -106,6 +108,7 @@
106108
OBJ_10 /* Operators.swift */,
107109
OBJ_11 /* Percentage.swift */,
108110
OBJ_12 /* RationalNumber.swift */,
111+
861B50B31DA97DF100C6F0B1 /* Utilities.swift */,
109112
);
110113
name = SwiftMath;
111114
path = Sources;
@@ -181,6 +184,7 @@
181184
OBJ_25 /* Algorithms.swift in Sources */,
182185
OBJ_26 /* Operators.swift in Sources */,
183186
OBJ_27 /* Percentage.swift in Sources */,
187+
861B50B41DA97DF100C6F0B1 /* Utilities.swift in Sources */,
184188
OBJ_28 /* RationalNumber.swift in Sources */,
185189
);
186190
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)