Repository navigation
Expand file tree
/
Copy pathtypes-showcase.fss
More file actions
127 lines (96 loc) · 3.89 KB
/
Copy pathtypes-showcase.fss
File metadata and controls
127 lines (96 loc) · 3.89 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
type Address =
{ City: string
Zip: int }
type User =
{ Id: int
Name: string
Tags: string list
Address: Address option
Scores: int map
Pair: (int * string) }
type Subscription =
| Free
| Premium of int
type Priority =
| Low
| Medium
| High
type MapShowcase =
{ ById: string map[int]
ByFlag: string map[bool]
ByPriority: string map[Priority] }
let scores =
{ ["math"] = 20; ["science"] = 18 }
let statusByCode =
{ ["200"] = "ok"; ["404"] = "not-found" }
let mapShowcase =
{ ById = { [1] = "first"; [2] = "second" }
ByFlag = Map.ofList [(true, "enabled"); (false, "disabled")]
ByPriority = Map.empty |> Map.add Low "later" |> Map.add High "now" }
let lookup_priority values = values[High]
let lookup_id values = Map.tryGet 1 values
let lookup_flag values = values[true]
let topPriority = lookup_priority mapShowcase.ByPriority |> Option.defaultValue "unknown"
let firstId = lookup_id mapShowcase.ById |> Option.defaultValue "unknown"
let enabled = lookup_flag mapShowcase.ByFlag |> Option.defaultValue "unknown"
let user = { Id = 1; Name = "Ada"; Tags = ["engineer"; "writer"]; Address = Some { City = "London"; Zip = 12345 }; Scores = scores; Pair = (7, "seven") }
let subscription = Premium 12
// Helper typed with OfficeAddress.
let make_office_address (address: Address) = address
let officeAddress = make_office_address { City = "London"; Zip = 12345 }
// Same shape as Address, inferred structurally from literal.
let parisAddress = { City = "Paris"; Zip = 75000 }
let updated = { user with Tags = user.Tags @ ["mathematician"] }
// Inline structural annotation: any record with matching fields is accepted.
let format_address (address: { City: string; Zip: int }) = $"{address.City} ({address.Zip})"
// Name exposure for host capability maps.
let capabilities =
// Compact multiline map layout.
{ ["format_address"] = "address formatting"
["make_office_address"] = "office address constructor" }
let city =
match updated.Address with
| Some address -> address.City
| None -> "unknown"
let mathScore =
updated.Scores
|> Map.tryGet "math"
|> Option.defaultValue 0
let hasScience = updated.Scores |> Map.containsKey "science"
let formatAddressKey = "format_address"
let scienceKey = "science"
let okCode = "200"
let patchedScores = { ["science"] = 19; ..updated.Scores }
let mapPreview =
match updated.Scores with
| {} ->
"empty"
| { [subject] = score; ..remaining } ->
$"first={subject}:{score}, remaining={Map.count remaining}"
let monthlyPrice =
match subscription with
| Free -> 0
| Premium amount -> amount
let missing = "missing"
let unknown = "unknown"
Console.writeLine $"User: {updated.Name}"
Console.writeLine $"City: {city}"
Console.writeLine $"Math score: {mathScore}"
Console.writeLine $"Has 'science': {hasScience}"
Console.writeLine $"Patched science score: {patchedScores |> Map.tryGet scienceKey |> Option.defaultValue 0}"
Console.writeLine $"Map preview: {mapPreview}"
Console.writeLine $"Subscription price: {monthlyPrice}"
// Structural record equivalence showcase:
// - literal with matching fields
// - value created through another named record type (OfficeAddress)
Console.writeLine $"Structural address from literal: {format_address parisAddress}"
Console.writeLine $"Structural address from OfficeAddress: {format_address officeAddress}"
Console.writeLine $"Capability for format_address: {Map.tryGet formatAddressKey capabilities |> Option.defaultValue missing}"
Console.writeLine $"HTTP 200: {statusByCode[okCode] |> Option.defaultValue unknown}"
Console.writeLine $"Map keys: id={firstId}, bool={enabled}, union={topPriority}"
updated.Tags |> List.iter Console.writeLine
match updated with
| { Id = id; Pair = (left, right) } ->
Console.writeLine $"Record pattern: id={id}, pair=({left}, {right})"
| _ ->
Console.writeLine "Unexpected"