-
Notifications
You must be signed in to change notification settings - Fork 18
/
TurtleApiHelpers.fsx
56 lines (43 loc) · 1.38 KB
/
TurtleApiHelpers.fsx
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
44
45
46
47
48
49
50
51
52
53
54
55
56
(* ======================================
TurtleApiHelpers.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Helper functions for Turtle Api Layer
====================================== *)
// requires Common.fsx to be loaded by parent file
// Uncomment to use this file standalone
//#load "Common.fsx"
open System
open Common
// ======================================
// Helper functions for Turtle Api Layer
// ======================================
open Result
type ErrorMessage =
| InvalidDistance of string
| InvalidAngle of string
| InvalidColor of string
| InvalidCommand of string
// convert the distance parameter to a float, or throw an exception
let validateDistance distanceStr =
try
Ok (float distanceStr)
with
| ex ->
Error (InvalidDistance distanceStr)
// convert the angle parameter to a float, or throw an exception
let validateAngle angleStr =
try
Ok ((float angleStr) * 1.0<Degrees>)
with
| ex ->
Error (InvalidAngle angleStr)
// convert the color parameter to a PenColor, or throw an exception
let validateColor colorStr =
match colorStr with
| "Black" -> Ok Black
| "Blue" -> Ok Blue
| "Red" -> Ok Red
| _ ->
Error (InvalidColor colorStr)