Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2014 06:52
Show Gist options
  • Save anonymous/1d63873ff53aebb0e6a3 to your computer and use it in GitHub Desktop.
Save anonymous/1d63873ff53aebb0e6a3 to your computer and use it in GitHub Desktop.
import Foundation
struct Dice : SequenceType {
typealias Generator = GeneratorOf<Int>
let dice:[Int]
init (rolls:Int, sides:Int) {
dice = Array(count:rolls, repeatedValue:sides)
}
func roll() -> Int {
return dice.map { Int(arc4random_uniform(UInt32($0 - 1)) + 1) }
.reduce(0, +)
}
func generate() -> Generator {
return GeneratorOf { self.roll() }
}
}
for roll in Dice(rolls: 3, sides: 6) {
println(roll)
}
//Everything after this point is (even more) silly! Just doing it for fun :)
infix operator ⫐ {
associativity none
precedence 140
}
func ⫐ (lhs:Int, rhs:Int) -> Dice {
return Dice(rolls: lhs, sides: rhs)
}
for roll in 3⫐6 {
println(roll)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment