Skip to content

Instantly share code, notes, and snippets.

@RonJeffries
RonJeffries / payroll playground 1
Last active October 14, 2015 16:39
A Swift Playground for payroll. I have my reasons ...
// Swift playground experimenting with payroll.
// (I have my reasons.)
println("Hello")
import Foundation
// This stuff sets rounding to two places after decimal
var mode = NSRoundingMode.RoundPlain
// wanted a version where i could just use reduce but without building a Frame object ...
// expands rolls to include bonus values, e.g.
func testExpandRolls() {
let game = BowlingGame()
game.roll(5,4, 10, 5,5, 6,5)
var expanded = game.expandRolls()
XCTAssertEqualObjects(expanded, [5,4, 10,5,5, 5,5,6, 6,5], "expanded")
}
@RonJeffries
RonJeffries / score2
Created June 8, 2014 15:01
Swift Lang tuples another way
// might like this as better use of Tuple. Not sure. Return Tuple, use its named elements
// code is asking for a frame object, i think, but wanted to try Tuples
func frameScore(index:Int) -> (score: Int, step: Int) {
if isStrike(index) {
return (rolls[index] + twoRolls(index+1), 1)
} else if isSpare(index) {
return (twoRolls(index) + rolls[index+2], 2)
} else {
return (twoRolls(index), 2)
@RonJeffries
RonJeffries / score
Last active August 29, 2015 14:02
Swift language bowling with Tuples. Interesting ...
func score() -> Int {
var sum = 0
var index = 0
for frame in 1...10 {
var (score, step) = frameScore(index)
sum += score
index += step
}
return sum
}