Skip to content

Commit 8ee804e

Browse files
committed
feature: adds DecimalNumber model
1 parent 1dfce28 commit 8ee804e

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public struct DecimalNumber: PositionalNumberSystemizable {
2+
public enum Digit: Int {
3+
case zero
4+
case one, two , three, four, five
5+
case six, seven, eight, nine, ten
6+
}
7+
}
8+
9+
extension DecimalNumber.Digit: PositionalDigitSystemizable {
10+
public static var placeholder: Self { .zero }
11+
12+
public var predecessor: Self {
13+
switch self {
14+
case .ten : return .nine
15+
case .nine : return .eight
16+
case .eight: return .seven
17+
case .seven: return .six
18+
case .six : return .five
19+
case .five : return .four
20+
case .four : return .three
21+
case .three: return .two
22+
case .two : return .one
23+
case .one : return .placeholder
24+
default : Self.fatalPredecessionError()
25+
}
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public protocol PositionalDigitSystemizable: CaseIterable, RawRepresentable<Int> {
2+
static var placeholder: Self { get }
3+
4+
var predecessor: Self { get }
5+
}
6+
7+
extension PositionalDigitSystemizable {
8+
public static func fatalPredecessionError() -> Never {
9+
fatalError("Digit has no defined precedent")
10+
}
11+
12+
public var quantity: Quantity {
13+
if self == Self.placeholder { return Quantity.none }
14+
15+
let precedingQuantity = self.predecessor.quantity
16+
return precedingQuantity.successor
17+
}
18+
}
19+
20+
extension PositionalDigitSystemizable {
21+
public var cardinality: Int {
22+
self.rawValue
23+
}
24+
25+
public var quantityMatchesCardinality: Bool {
26+
self.cardinality.represents(self.quantity)
27+
}
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
protocol PositionalNumberSystemizable {
2+
associatedtype Digit: PositionalDigitSystemizable
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Testing
2+
@testable import NumberSystems
3+
4+
@Test func quantitiesOfDecimalDigits() async throws {
5+
DecimalNumber.Digit.allCases.forEach {
6+
#expect($0.quantityMatchesCardinality)
7+
}
8+
}

0 commit comments

Comments
 (0)