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

Make it easier to iterate through DiceKit types #94

Open
Samasaur1 opened this issue Dec 26, 2020 · 1 comment
Open

Make it easier to iterate through DiceKit types #94

Samasaur1 opened this issue Dec 26, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@Samasaur1
Copy link
Owner

One example of this problem is here. Take a look at the doc comment for the chances property of a Chances object.

/// The rolls and the chances of them occurring.
///
/// This is the property one should use in order to iterate through the possibilities, like so:
///
/// ```
/// let chances: Chances = getChancesFromSomewhereElse()
/// let arr = chances.chances.sorted(by: { first, second in
/// first.key < second.key
/// })
/// for (roll, chance) in arr {
/// print("The chance of rolling a \(roll) is \(chance.n) out of \(chance.d)")
/// }
/// ```
///
/// - Since: 0.21.0
public private(set) var chances: [Roll: Chance]

It wants you to use chances.chances, and then possibly sorted(by:) on top of that, if you want to iterate through it. However, Swift has support for adding iteration to custom types, using IteratorProtocol. While I'm not sure how far we should take this idea (mapping? conformance to Collection? other DiceKit type?), these two ideas seems like a good start:

import DiceKit

let dice: Dice = getDiceStruct()
for die in dice {
    print(die)
}
for (die, count) in dice {
    print("Die \(die) is repeated \(count) time(s)")
}

let chances: Chances = getChances()
for (roll, chance) in chances {
    print("The probability of rolling a \(roll) is \(chance)")
}

I'm not as sure about iterating through Dice, in part because I do see two ways to do that, as I listed above. Given 2d6 + d4, the first one would loop three times (d6, d6, d4), and the second would loop twice ((d6, 2), (d4, 1)). More consideration is required on that front. However, the idea overall seems sound.

@Samasaur1 Samasaur1 added the enhancement New feature or request label Dec 26, 2020
Samasaur1 added a commit that referenced this issue Oct 2, 2021
This begins to address #94, although it is not a complete solution
Samasaur1 added a commit that referenced this issue Jun 26, 2022
This begins to address #94, although it is not a complete solution
@Samasaur1
Copy link
Owner Author

Well actually, the user might want to iterate through Chances in order of likelihood. The implementation of IteratorProtocol in #96 is not the only option.

What might be a step in the right direction would be to allow calling sorted(by:) directly on the Chances object, rather than on Chances.chances (the repetition is confusing). This would turn this:

let chances = getChances()
for (roll, chance) in chances.chances.sorted(by: { $0.key < $1.key }) {
    print("The chance of rolling a \(roll) is \(chance.n) out of \(chance.d)")
}

(which is already condensed) into this:

let chances = getChances()
for (roll, chance) in chances.sorted(by: { $0.key < $1.key }) {
    print("The chance of rolling a \(roll) is \(chance.n) out of \(chance.d)")
}

It's not perfect, but it's definitely an improvement, and feels more similar to Dictionary

Samasaur1 added a commit that referenced this issue Jul 27, 2022
This begins to address #94, although it is not a complete solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant