Created
September 30, 2018 15:05
-
-
Save aronson/34a2f3d1d9b21f077cf3fe701176e763 to your computer and use it in GitHub Desktop.
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 | |
// Attempts to read a string and parse it into an integer | |
// If the string was not an integer, return "None", otherwise return "Some (integer)" | |
// I absolutely stole this code from the internet. Don't worry about it too much, it's just a shortcut. | |
// I don't really get the syntax either, but shit works | |
let (|Int|_|) str = | |
match System.Int32.TryParse(str) with | |
| (true,int) -> Some(int) | |
| _ -> None | |
// Given a random number generator and a number, roll a number between 1 and the number | |
let getRandomRoll (randomGenerator:Random) maxNumber = | |
randomGenerator.Next(1, maxNumber) | |
// Use this program like "roller.exe 20 10 4 5" to roll dice with each number as the max | |
// This code runs when the program actually starts. We call the other two functions above in it | |
[<EntryPoint>] | |
let main argv = | |
let randomGenerator = new Random() | |
// "argv" is a list of arguments when you run the program, so we work with one at a time | |
argv |> Seq.iter(fun inputString -> | |
// This match statement will run different code if it is or isn't a number | |
match inputString with | |
// This code only runs if it's actually a number | |
| Int number -> | |
// Ok, it was a number so first do a random roll from 1 to number | |
let diceRoll = getRandomRoll randomGenerator number | |
// Now print out the result of the roll | |
printfn "Rolled a %i" diceRoll | |
// This code runs if it wasn't a number | |
| _ -> | |
// Politely inform the user that they are a dumbass | |
printfn "%s is not a number!" inputString) | |
0 // return an integer exit code (this is for Windows, doesn't matter for our code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment