Skip to content

Instantly share code, notes, and snippets.

@khellang
Created September 9, 2014 17:52
Show Gist options
  • Save khellang/7852ce8594d707da3cde to your computer and use it in GitHub Desktop.
Save khellang/7852ce8594d707da3cde to your computer and use it in GitHub Desktop.
Tennis Kata - Implemented in F#, tests are using FsUnit with Xunit
namespace Tennis
module Game =
type State = {
a: int;
b: int;
}
let callEqualScore score =
match score with
| 0 -> "love-love"
| 1 -> "15-all"
| 2 -> "30-all"
| _ -> "deuce"
let callFoo diff =
let player =
if diff > 0
then "player A"
else "player B"
let foo =
if abs diff >= 2
then "game"
else "advantage"
foo + " " + player
let getPoints score =
match score with
| 0 -> "love"
| 1 -> "15"
| 2 -> "30"
| 3 -> "40"
| _ -> failwith "Invalid score"
let callUnequalScore a b =
getPoints(a) + "-" + getPoints(b)
let callScore state =
match state with
| { a = x; b = y } when x = y -> callEqualScore x
| { a = x; b = y } when x < 4 && y < 4 -> callUnequalScore x y
| { a = x; b = y } -> callFoo(x - y)
| _ -> failwith "lol wat!?"
let getState state score =
match score with
| 'A' -> { state with a = state.a + 1 }
| 'B' -> { state with b = state.b + 1 }
| _ -> failwith "Invalid player"
let play sequence =
let state = { a = 0; b = 0 }
let state = Seq.fold(getState) state sequence
callScore state
namespace Tennis
open Xunit;
open FsUnit.Xunit;
module Tests =
[<Fact>]
let ``point to player A should equal '15-love'`` () =
Game.play "A" |> should equal "15-love"
[<Fact>]
let ``two points to player A should equal '30-love'`` () =
Game.play "AA" |> should equal "30-love"
[<Fact>]
let ``three points to player A should equal '40-love'`` () =
Game.play "AAA" |> should equal "40-love"
[<Fact>]
let ``four points to player A should equal 'game player A'`` () =
Game.play "AAAA" |> should equal "game player A"
[<Fact>]
let ``two points to player B should equal 'love-30'`` () =
Game.play "BB" |> should equal "love-30"
[<Fact>]
let ``two points to each player should equal '30-all'`` () =
Game.play "ABAB" |> should equal "30-all"
[<Fact>]
let ``three points to each player should equal 'deuce'`` () =
Game.play "AAABBB" |> should equal "deuce"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment