-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.fsx
More file actions
95 lines (65 loc) · 2.24 KB
/
Copy pathbits.fsx
File metadata and controls
95 lines (65 loc) · 2.24 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Wrapping and unwrapping the single-case union
type Quantity = Quantity of float
type Price = Price of float
type Discount = Discount of float
let totalPrice (Quantity q) (Price p) (Discount d) = q * p * (1. - d)
// The same using object style
type Quantity' =
| Quantity' of float
member this.Value =
let (Quantity' value) = this
value
type Price' =
| Price' of float
member this.Value =
let (Price' value) = this
value
type Discount' =
| Discount' of float
member this.Value = let (Discount' value) = this in value
let totalPrice' (quantity: Quantity') (price: Price') (discount: Discount') =
quantity.Value
* price.Value
* (1. - discount.Value)
// Named tuples and their deconstruction
type Point3D = Point3D of x: int * y: int * z: int
Point3D(3, 4, 5)
let getZ (Point3D (z = zValue)) = zValue
let getXZ (Point3D (x = xValue; z = zValue)) = xValue, zValue
let getZ' (Point3D (_, _, z)) = z // Deconstructing the classic way (alternative)
// Deconstructing named tuples in objects
type Color = string
type Paint =
| Paint' of volume: float * pigment: Color
member me.Volume =
let (Paint' (volume = value)) = me
value
let paint = Paint'(2.5, "red")
paint.Volume
// Active patterns, oh boy!
let (|Even|Odd|) input = if input % 2 = 0 then Even else Odd
let testNumber num =
match num with
| Even -> printfn $"{num} is even."
| Odd -> printfn $"{num} is odd."
[ 1 .. 10 ] |> List.iter testNumber
open System.Drawing
let (|RGB|) (color: Color) = (color.R, color.G, color.B)
let (|RGBA|) (color: Color) = (color.R, color.G, color.B, color.A)
let (|HSB|) (color: Color) =
(color.GetHue(), color.GetSaturation(), color.GetBrightness())
let printRGB (color: Color) =
match color with
| RGB (r, g, b) -> printfn $"Red: {r}, Green: {g}, Blue: {b}"
let printRGBA (color: Color) =
match color with
| RGBA (r, g, b, a) -> printfn $"Red: {r}, Green: {g}, Blue: {b}, Alpha: {a}"
let printHSB (color: Color) =
match color with
| HSB (h, s, b) -> printfn $"Hue: {h}, Saturation: {s}, Brightness: {b}"
let printAll (color: Color) =
printfn $"Color: {color}:"
color |> printRGB
color |> printRGBA
color |> printHSB
printAll Color.Red