Skip to content

Commit

Permalink
Allow iterating through Chances
Browse files Browse the repository at this point in the history
This begins to address #94, although it is not a complete solution
  • Loading branch information
Samasaur1 committed Oct 2, 2021
1 parent d67f52f commit 231bde9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/DiceKit/Chances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ fileprivate extension Dictionary.Values where Dictionary.Value == Chance {
return total
}
}
extension Chances: Sequence {
public func makeIterator() -> IndexingIterator<[Dictionary<Roll, Chance>.Element]> {
return chances.sorted { $0.key < $1.key }.makeIterator()
}
}
7 changes: 7 additions & 0 deletions Tests/DiceKitTests/ChancesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ final class ChancesTests: XCTestCase {
let c = Chances(chances: [5: 0.234, 8: 0.432])
XCTAssertEqual(c.chances[5], c[of: 5])
}

func testSequence() {
let chances = Chances(chances: [5: 0.234, 8: 0.432])
for (r, c) in chances {
print("The chance of rolling \(r) is \(c)")
}
}
}

0 comments on commit 231bde9

Please sign in to comment.