Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Successfulness types #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions DiceKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
531084C31B5B0761008DD696 /* Die.Roll.swift in Sources */ = {isa = PBXBuildFile; fileRef = 531084C11B5B0102008DD696 /* Die.Roll.swift */; };
531E4F5F1B73F5E20073F01A /* EquatableTestUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 531E4F5E1B73F5E20073F01A /* EquatableTestUtilities.swift */; };
531E4F611B7400270073F01A /* Arbitrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 531E4F601B7400270073F01A /* Arbitrary.swift */; };
533B55F41BDDC0D100C08EDB /* OutcomeWithSuccessfulness_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533B55F31BDDC0D100C08EDB /* OutcomeWithSuccessfulness_Tests.swift */; };
533D0C771B6419F8003A7D32 /* FrequencyDistribution_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533D0C761B6419F8003A7D32 /* FrequencyDistribution_Tests.swift */; };
533D0C7B1B6423FA003A7D32 /* ProbabilityMass_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533D0C7A1B6423FA003A7D32 /* ProbabilityMass_Tests.swift */; };
533D0C7D1B643F6C003A7D32 /* Constant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533D0C7C1B643F6C003A7D32 /* Constant.swift */; };
533EB1EE1B5B293000B3F8A1 /* MultiplicationExpressionResult_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533EB1ED1B5B293000B3F8A1 /* MultiplicationExpressionResult_Tests.swift */; };
533F88091B52B988003838C8 /* DiceKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 533F87FE1B52B988003838C8 /* DiceKit.framework */; };
533F880E1B52B988003838C8 /* Die_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533F880D1B52B988003838C8 /* Die_Tests.swift */; };
534900341B78172900FA2804 /* ArithmeticType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534900331B78172900FA2804 /* ArithmeticType.swift */; };
537675F81BDDB81A00F6CE9C /* OutcomeWithSuccessfulness.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537675F71BDDB81A00F6CE9C /* OutcomeWithSuccessfulness.swift */; };
537675FA1BDDB88700F6CE9C /* Successfulness.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537675F91BDDB88700F6CE9C /* Successfulness.swift */; };
537675FC1BDDBAD400F6CE9C /* Successfulness_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537675FB1BDDBAD400F6CE9C /* Successfulness_Tests.swift */; };
5379780B1B531B21005818EC /* Die.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5379780A1B531B21005818EC /* Die.swift */; };
538743871B5316BA00EB15C8 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 530FEDD11B530CAB00960BFD /* Nimble.framework */; };
538AD9651B75A66F001B5CB5 /* MockExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 538AD9641B75A66F001B5CB5 /* MockExpression.swift */; };
Expand Down Expand Up @@ -94,6 +98,7 @@
531084C11B5B0102008DD696 /* Die.Roll.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Die.Roll.swift; sourceTree = "<group>"; };
531E4F5E1B73F5E20073F01A /* EquatableTestUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EquatableTestUtilities.swift; sourceTree = "<group>"; };
531E4F601B7400270073F01A /* Arbitrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Arbitrary.swift; sourceTree = "<group>"; };
533B55F31BDDC0D100C08EDB /* OutcomeWithSuccessfulness_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OutcomeWithSuccessfulness_Tests.swift; sourceTree = "<group>"; };
533D0C761B6419F8003A7D32 /* FrequencyDistribution_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FrequencyDistribution_Tests.swift; sourceTree = "<group>"; };
533D0C7A1B6423FA003A7D32 /* ProbabilityMass_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProbabilityMass_Tests.swift; sourceTree = "<group>"; };
533D0C7C1B643F6C003A7D32 /* Constant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constant.swift; sourceTree = "<group>"; };
Expand All @@ -104,6 +109,9 @@
533F880D1B52B988003838C8 /* Die_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Die_Tests.swift; sourceTree = "<group>"; };
533F880F1B52B988003838C8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
534900331B78172900FA2804 /* ArithmeticType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArithmeticType.swift; sourceTree = "<group>"; };
537675F71BDDB81A00F6CE9C /* OutcomeWithSuccessfulness.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OutcomeWithSuccessfulness.swift; sourceTree = "<group>"; };
537675F91BDDB88700F6CE9C /* Successfulness.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Successfulness.swift; sourceTree = "<group>"; };
537675FB1BDDBAD400F6CE9C /* Successfulness_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Successfulness_Tests.swift; sourceTree = "<group>"; };
5379780A1B531B21005818EC /* Die.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Die.swift; sourceTree = "<group>"; };
538AD9641B75A66F001B5CB5 /* MockExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockExpression.swift; sourceTree = "<group>"; };
538AD9681B7678B7001B5CB5 /* Die.Roll_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Die.Roll_Tests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -232,10 +240,12 @@
531084BF1B5AF993008DD696 /* MultiplicationExpressionResult.swift */,
53ECD0E11B5B498A002D859F /* NegationExpression.swift */,
53ECD0E31B5B5185002D859F /* NegationExpressionResult.swift */,
537675F71BDDB81A00F6CE9C /* OutcomeWithSuccessfulness.swift */,
53FA0EAB1B6579F300C8D3B5 /* ProbabilisticExpressionType.swift */,
539D568D1B5C7BDA00AC6A37 /* ProbabilityMass.swift */,
8B4C14011B7F8B47008AC355 /* SubtractionExpression.swift */,
8B4C14031B7FA8A2008AC355 /* SubtractionExpressionResult.swift */,
537675F91BDDB88700F6CE9C /* Successfulness.swift */,
);
path = DiceKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -264,10 +274,12 @@
533EB1ED1B5B293000B3F8A1 /* MultiplicationExpressionResult_Tests.swift */,
539D56831B5B6D1A00AC6A37 /* NegationExpression_Tests.swift */,
539D56851B5B6D2B00AC6A37 /* NegationExpressionResult_Tests.swift */,
533B55F31BDDC0D100C08EDB /* OutcomeWithSuccessfulness_Tests.swift */,
53FA0EAD1B657A3600C8D3B5 /* ProbabilisticExpressionType_Tests.swift */,
533D0C7A1B6423FA003A7D32 /* ProbabilityMass_Tests.swift */,
8BFAA9F81B7ED8E800EF65AE /* SubtractionExpression_Tests.swift */,
8BFAA9FA1B7F708000EF65AE /* SubtractionExpressionResult_Tests.swift */,
537675FB1BDDBAD400F6CE9C /* Successfulness_Tests.swift */,
);
path = DiceKitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -430,6 +442,7 @@
53D1EC7C1B6312E100433D2C /* FrequencyDistribution.swift in Sources */,
53FA0EAC1B6579F300C8D3B5 /* ProbabilisticExpressionType.swift in Sources */,
531084C01B5AF993008DD696 /* MultiplicationExpressionResult.swift in Sources */,
537675F81BDDB81A00F6CE9C /* OutcomeWithSuccessfulness.swift in Sources */,
539D568C1B5C7B7700AC6A37 /* Dictionary+Functions.swift in Sources */,
6E9F18981B7FDF0D00AB8893 /* MinimizationExpressionResult.swift in Sources */,
53D1EC7E1B63133E00433D2C /* ApproximatelyEquatable.swift in Sources */,
Expand All @@ -448,6 +461,7 @@
53CFC30F1B5AD674009C6C8F /* MultiplicationExpression.swift in Sources */,
53ECD0E41B5B5185002D859F /* NegationExpressionResult.swift in Sources */,
533D0C7D1B643F6C003A7D32 /* Constant.swift in Sources */,
537675FA1BDDB88700F6CE9C /* Successfulness.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -464,7 +478,9 @@
53D05EEC1B546C73007CE7FC /* Int+Random_Tests.swift in Sources */,
6E9F18941B7FD16100AB8893 /* MinimizationExpression_Tests.swift in Sources */,
539D568A1B5B6D6A00AC6A37 /* AdditionExpressionResult_Tests.swift in Sources */,
533B55F41BDDC0D100C08EDB /* OutcomeWithSuccessfulness_Tests.swift in Sources */,
533D0C771B6419F8003A7D32 /* FrequencyDistribution_Tests.swift in Sources */,
537675FC1BDDBAD400F6CE9C /* Successfulness_Tests.swift in Sources */,
6E0F58C91B8D63690095087F /* MinimizationExpressionResult_Tests.swift in Sources */,
538AD96B1B768931001B5CB5 /* Constant_Tests.swift in Sources */,
533F880E1B52B988003838C8 /* Die_Tests.swift in Sources */,
Expand Down
5 changes: 4 additions & 1 deletion DiceKit/FrequencyDistribution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public protocol FrequencyDistributionOutcomeType: InvertibleMultiplicativeType,
/// The number that will be used when determining how many times to perform another
/// expression when multiplied by `self`.
var multiplierEquivalent: Int { get }

// Force Int for IntegerLiteralType
init(integerLiteral: Int)

}

Expand Down Expand Up @@ -49,7 +52,7 @@ public struct FrequencyDistribution<OutcomeType: FrequencyDistributionOutcomeTyp
return FrequencyDistribution(FrequenciesPerOutcome())
}
public static var multiplicativeIdentity: FrequencyDistribution {
return FrequencyDistribution([Outcome.additiveIdentity: Frequency.multiplicativeIdentity])
return FrequencyDistribution([.additiveIdentity: .multiplicativeIdentity])
}

public let frequenciesPerOutcome: FrequenciesPerOutcome
Expand Down
125 changes: 125 additions & 0 deletions DiceKit/OutcomeWithSuccessfulness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// OutcomeWithSuccessfulness.swift
// DiceKit
//
// Created by Brentley Jones on 10/25/15.
// Copyright © 2015 Brentley Jones. All rights reserved.
//

/// A paired `FrequencyDistributionOutcomeType` and `Successfulness`.
public struct OutcomeWithSuccessfulness<Outcome: FrequencyDistributionOutcomeType>: Equatable {

public let outcome: Outcome
public let successfulness: Successfulness

public init(_ outcome: Outcome, successfulness: Successfulness) {
self.outcome = outcome
self.successfulness = successfulness
}
}

// MARK: - CustomStringConvertible

extension OutcomeWithSuccessfulness: CustomStringConvertible {
public var description: String {
if successfulness == .additiveIdentity {
return "\(outcome)"
} else {
return "(\(outcome) with \(successfulness.rawDescription(surroundWithParentheses: false)))"
}
}
}

// MARK: - CustomDebugStringConvertible

extension OutcomeWithSuccessfulness: CustomDebugStringConvertible {
public var debugDescription: String {
return "OutcomeWithSuccessfulness(\(String(reflecting: outcome)), successfulness: \(String(reflecting: successfulness)))"
}
}

// MARK: - Equatable

public func == <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> Bool {
return lhs.outcome == rhs.outcome && lhs.successfulness == rhs.successfulness
}

// MARK: - Comparable

extension OutcomeWithSuccessfulness: Comparable {
}

public func < <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> Bool {
if lhs.outcome == rhs.outcome {
return lhs.successfulness < rhs.successfulness
} else {
return lhs.outcome < rhs.outcome
}
}

// MARK: - Hashable

extension OutcomeWithSuccessfulness: Hashable {

public var hashValue: Int {
return outcome.hashValue ^ successfulness.hashValue
}
}

// MARK: - ArithmeticType

extension OutcomeWithSuccessfulness: ArithmeticType {

// TODO: Change to stored properties when allowed by Swift
public static var additiveIdentity: OutcomeWithSuccessfulness {
return OutcomeWithSuccessfulness(.additiveIdentity, successfulness: .additiveIdentity)
}
public static var multiplicativeIdentity: OutcomeWithSuccessfulness {
return OutcomeWithSuccessfulness(.multiplicativeIdentity, successfulness: .multiplicativeIdentity)
}

// MARK: IntegerLiteralType

public init(integerLiteral value: Int) {
self.init(Outcome(integerLiteral: value), successfulness: 0)
}
}

public func + <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> OutcomeWithSuccessfulness<O> {
let outcome = lhs.outcome + rhs.outcome
let successfulness = lhs.successfulness + rhs.successfulness
return OutcomeWithSuccessfulness(outcome, successfulness: successfulness)
}

public func - <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> OutcomeWithSuccessfulness<O> {
let outcome = lhs.outcome - rhs.outcome
let successfulness = lhs.successfulness - rhs.successfulness
return OutcomeWithSuccessfulness(outcome, successfulness: successfulness)
}

public func * <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> OutcomeWithSuccessfulness<O> {
let outcome = lhs.outcome * rhs.outcome
let successfulness = lhs.successfulness * rhs.successfulness
return OutcomeWithSuccessfulness(outcome, successfulness: successfulness)
}

public func / <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> OutcomeWithSuccessfulness<O> {
let outcome = lhs.outcome / rhs.outcome
let successfulness = lhs.successfulness / rhs.successfulness
return OutcomeWithSuccessfulness(outcome, successfulness: successfulness)
}

public func % <O>(lhs: OutcomeWithSuccessfulness<O>, rhs: OutcomeWithSuccessfulness<O>) -> OutcomeWithSuccessfulness<O> {
let outcome = lhs.outcome % rhs.outcome
let successfulness = lhs.successfulness % rhs.successfulness
return OutcomeWithSuccessfulness(outcome, successfulness: successfulness)
}

// MARK: - FrequencyDistributionOutcomeType

extension OutcomeWithSuccessfulness: FrequencyDistributionOutcomeType {

public var multiplierEquivalent: Int {
return outcome.multiplierEquivalent
}
}
Loading