1 people like it.

Yet another Tennis Kata

It's a joke one liner implementation of Tennis Kata.Please see this code not seriously.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
/// one liner tennis kata
let play x y = Seq.map([y,x,"A ";x,y,"B "].get_Item>>fun(w,l,p)->p+match !w with|3->(if !l=3 then l:=2;"advantage"else"win!")|2 when !l=3->w:=3;"deuce"|p->incr w;["15";"30";"40"].[p])


/// A wins a point
let POINT_A = 0
/// B wins a point
let POINT_B = 1
/// helper function to make game points
let cycle points = seq {while true do yield! points}


// A wins every point
let game1 = cycle [POINT_A]
// A and B swap points identically
let game2 = cycle [POINT_A; POINT_B]
// A and B trade points but A wins more points than B
let game3 = cycle [POINT_A; POINT_B; POINT_A]

/// print out the game result
let print = String.concat"!, ">>printfn"%s"

game1 |> play (ref 0) (ref 0) |> Seq.take 10 |> print
// result: A 15!, A 30!, A 40!, A win!
game2 |> play (ref 0) (ref 0) |> Seq.take 10 |> print
// result: A 15!, B 15!, A 30!, B 30!, A 40!, B deuce!, A advantage!, B deuce!, A advantage!, B deuce!
game3 |> play (ref 0) (ref 0) |> Seq.take 10 |> print
// result: A 15!, B 15!, A 30!, A 40!, B 30!, A win!


// simulate 1000 games and show how many times player A and player B wins.
let randomGame = 
    let rnd = new System.Random() 
    seq { while true do yield if rnd.NextDouble() < 0.5 then POINT_A else POINT_B }
seq {
for i in 1 .. 1000 do 
  yield randomGame |> play(ref 0)(ref 0) |> Seq.find(Seq.forall((<>)'!')>>not)
}
|> Seq.groupBy(Seq.forall((<>)'A'))
|> Seq.iter(fun (k,v) -> printfn "%s wins %d times." (if k then "A" else "B") (Seq.length v))
// result:
// A wins 498 times.
// B wins 502 times.
val play : x:int ref -> y:int ref -> (seq<int> -> seq<string>)

Full name: Script.play


 one liner tennis kata
val x : int ref
val y : int ref
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val w : int ref
val l : int ref
val p : string
val p : int
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val POINT_A : int

Full name: Script.POINT_A


 A wins a point
val POINT_B : int

Full name: Script.POINT_B


 B wins a point
val cycle : points:seq<'a> -> seq<'a>

Full name: Script.cycle


 helper function to make game points
val points : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val game1 : seq<int>

Full name: Script.game1
val game2 : seq<int>

Full name: Script.game2
val game3 : seq<int>

Full name: Script.game3
val print : (seq<string> -> unit)

Full name: Script.print


 print out the game result
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val randomGame : seq<int>

Full name: Script.randomGame
val rnd : System.Random
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
System.Random.NextDouble() : float
val i : int
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.find
val forall : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.forall
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val groupBy : projection:('T -> 'Key) -> source:seq<'T> -> seq<'Key * seq<'T>> (requires equality)

Full name: Microsoft.FSharp.Collections.Seq.groupBy
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val k : bool
val v : seq<string>
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length

More information

Link:http://fssnip.net/8U
Posted:12 years ago
Author:nagat01
Tags: tennis , kata , golf