Created
November 11, 2014 15:12
-
-
Save cloudRoutine/e041b62fe120cf656a59 to your computer and use it in GitHub Desktop.
D6 Type
This file contains 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
open System | |
type D6 = | |
| One | Two | Three | Four | Five | Six | |
member self.Value = | |
match self with | |
| One -> 1 | Two -> 2 | Three -> 3 | |
| Four -> 4 | Five -> 5 | Six -> 6 | |
override self.ToString() = | |
match self with | |
| One -> "One" | Two -> "Two" | Three -> "Three" | |
| Four -> "Four" | Five -> "Five" | Six -> "Six" | |
static member Create (num:int) = | |
match num with | |
| 1 -> One | 2 -> Two | 3 -> Three | |
| 4 -> Four | 5 -> Five | 6 -> Six | |
| _ -> failwithf "Could not create D6, %d is not in range 1-6" num | |
static member inline Roll() = Random().Next(1,7) |> D6.Create | |
static member inline Add (a:D6) (b:D6) = a.Value + b.Value | |
static member inline (+) (a , b ) = D6.Add a b | |
static member inline Subtract (a:D6) (b:D6) = a.Value - b.Value | |
static member inline (-) (a , b ) = D6.Subtract a b | |
static member FromList (numls: int list ) = | |
numls |> List.map D6.Create | |
(* A random infinite sequence of ints 1-6 *) | |
let rollGen = | |
let rnd = Random() | |
let rec gen() = seq { yield rnd.Next(1,7) | |
yield! gen() } | |
gen() | |
(* a random infinite sequence of D6s *) | |
let d6Gen = rollGen |> Seq.map D6.Create | |
(* Slice a static list of ints from the random infinite sequence*) | |
let rollList num = rollGen |> Seq.take num |> List.ofSeq | |
(* Slice a static list of ints from the random infinite sequence*) | |
let d6List num = d6Gen |> Seq.take num |> List.ofSeq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment