Active Pattern Sample

Rating: No reviews yet
Downloads: 31
Change Set: ffa732c6110c
Released: Oct 15, 2010
Updated: Oct 15, 2010 by ptrelford
Dev status: Alpha Help Icon

Recommended Download

Application TickSpec Library
application, 109K, uploaded Oct 15, 2010 - 31 downloads

Release Notes

Parser string parameters using F#'s Active Patterns:
let (|Col|) = function
    | "left" -> 0 | "middle" -> 1 | "right" -> 2
    | _ -> raise (new System.InvalidCastException())

let (|Row|) = function
    | "top" -> 0 | "middle" -> 1 | "bottom" -> 2
    | _ -> raise (new System.InvalidCastException())   

let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)`` 
   (mark:string,Row row,Col col) =
    Assert.IsTrue(System.String.IsNullOrEmpty(layout.[row].[col]))
    layout.[row].[col] <- mark    

Or use new Parser Attribute from C# and F#:
type Row = Top = 0 | Middle = 1 | Bottom = 2
type Col = Left = 0 | Middle = 1 | Right = 2

let [<Parser>] toCol = function
    | "left" -> Col.Left | "middle" -> Col.Middle | "right" -> Col.Right
    | _ -> raise (new System.InvalidCastException())

let [<Parser>] toRow = function
    | "top" -> Row.Top | "middle" -> Row.Middle | "bottom" -> Row.Bottom
    | _ -> raise (new System.InvalidCastException())
   
let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)`` 
        (mark:string,row:Row,col:Col) =       
    let y = int row             
    let x = int col        
    Debug.Assert(System.String.IsNullOrEmpty(layout.[y].[x]))
    layout.[y].[x] <- mark

Reviews for this release

No reviews yet for this release.