Snippets

Cameron Presley F# - User needs to make a selection based from a list

Updated by Cameron Presley

File ValidatingUserInputUsingAListOfOptions.fs Modified

  • Ignore whitespace
  • Hide word diff
             | Some x -> Some x
 
 // Defining the prompt
-let promptUserForHowManyPlayers numbers = numbers |> List.iter (fun x -> "%A" x)
+let promptUserForHowManyPlayers numbers = numbers |> List.iter (fun x -> printfn "%A" x)
 
 // Defining valid options
 let validNumPlayerChoices = [2; 3; 4; 5;]
Updated by Cameron Presley

File ValidatingUserInputUsingAListOfOptions.fs Modified

  • Ignore whitespace
  • Hide word diff
             | Some x -> Some x
 
 // Defining the prompt
-let prompt numbers = numbers |> List.iter (fun x -> "%A" x)
+let promptUserForHowManyPlayers numbers = numbers |> List.iter (fun x -> "%A" x)
 
 // Defining valid options
-let options = [2; 3; 4; 5;]
+let validNumPlayerChoices = [2; 3; 4; 5;]
 
 // Defning selection validator
-let selectionValidator selection options =
+let validateNumPlayers selection options =
     match selection |> Int32.TryParse with
     | (false,_) -> None
     | (true, value) -> options |> List.tryFind (fun x -> x = value)
     
 // Example usage
-userMakesAChoice prompt selectionValidator options
+userMakesAChoice promptUserForHowManyPlayers validateNumPlayers validNumPlayerChoices
Updated by Cameron Presley

File ValidatingUserInputUsingAListOfOptions.fs Modified

  • Ignore whitespace
  • Hide word diff
 open System
-let rec userMakesAChoice prompt selectionValidator validOptions =
-        match validTargets with
+let rec userMakesAChoice prompt selectionValidator options =
+        match options with
         | [] -> None
         | _ -> 
-            prompt validOptions
+            prompt options
             let result = Console.ReadLine()
-            match selectionValidator result validOptions with
-            | None -> userMakesAChoice prompt selectionValidator validOptions
+            match selectionValidator result options with
+            | None -> userMakesAChoice prompt selectionValidator options
             | Some x -> Some x
 
 // Defining the prompt
Updated by Cameron Presley

File ValidatingUserInputUsingAListOfOptions Deleted

  • Ignore whitespace
  • Hide word diff
-open System
-let rec userMakesAChoice prompt selectionValidator validOptions =
-        match validTargets with
-        | [] -> None
-        | _ -> 
-            prompt validOptions
-            let result = Console.ReadLine()
-            match selectionValidator result validOptions with
-            | None -> userMakesAChoice prompt selectionValidator validOptions
-            | Some x -> Some x
-
-// Defining the prompt
-let prompt numbers = numbers |> List.iter (fun x -> "%A" x)
-
-// Defining valid options
-let options = [2; 3; 4; 5;]
-
-// Defning selection validator
-let selectionValidator selection options =
-    match selection |> Int32.TryParse with
-    | (false,_) -> None
-    | (true, value) -> options |> List.tryFind (fun x -> x = value)
-    
-// Example usage
-userMakesAChoice prompt selectionValidator options

File ValidatingUserInputUsingAListOfOptions.fs Added

  • Ignore whitespace
  • Hide word diff
+open System
+let rec userMakesAChoice prompt selectionValidator validOptions =
+        match validTargets with
+        | [] -> None
+        | _ -> 
+            prompt validOptions
+            let result = Console.ReadLine()
+            match selectionValidator result validOptions with
+            | None -> userMakesAChoice prompt selectionValidator validOptions
+            | Some x -> Some x
+
+// Defining the prompt
+let prompt numbers = numbers |> List.iter (fun x -> "%A" x)
+
+// Defining valid options
+let options = [2; 3; 4; 5;]
+
+// Defning selection validator
+let selectionValidator selection options =
+    match selection |> Int32.TryParse with
+    | (false,_) -> None
+    | (true, value) -> options |> List.tryFind (fun x -> x = value)
+    
+// Example usage
+userMakesAChoice prompt selectionValidator options
Created by Cameron Presley

File ValidatingUserInputUsingAListOfOptions Added

  • Ignore whitespace
  • Hide word diff
+open System
+let rec userMakesAChoice prompt selectionValidator validOptions =
+        match validTargets with
+        | [] -> None
+        | _ -> 
+            prompt validOptions
+            let result = Console.ReadLine()
+            match selectionValidator result validOptions with
+            | None -> userMakesAChoice prompt selectionValidator validOptions
+            | Some x -> Some x
+
+// Defining the prompt
+let prompt numbers = numbers |> List.iter (fun x -> "%A" x)
+
+// Defining valid options
+let options = [2; 3; 4; 5;]
+
+// Defning selection validator
+let selectionValidator selection options =
+    match selection |> Int32.TryParse with
+    | (false,_) -> None
+    | (true, value) -> options |> List.tryFind (fun x -> x = value)
+    
+// Example usage
+userMakesAChoice prompt selectionValidator options
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.