You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// 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
publicprivate(set)varchances:[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
letdice:Dice=getDiceStruct()fordiein dice {print(die)}for(die, count)in dice {print("Die \(die) is repeated \(count) time(s)")}letchances: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.
The text was updated successfully, but these errors were encountered:
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:
letchances=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:
letchances=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
One example of this problem is here. Take a look at the doc comment for the
chances
property of aChances
object.DiceKit/Sources/DiceKit/Chances.swift
Lines 31 to 46 in d67f52f
It wants you to use
chances.chances
, and then possiblysorted(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 toCollection
? other DiceKit type?), these two ideas seems like a good start:I'm not as sure about iterating through
Dice
, in part because I do see two ways to do that, as I listed above. Given2d6 + 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.The text was updated successfully, but these errors were encountered: