Created
September 24, 2014 06:52
-
-
Save anonymous/1d63873ff53aebb0e6a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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