-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivePatterns.fsx
More file actions
executable file
·81 lines (65 loc) · 2.14 KB
/
activePatterns.fsx
File metadata and controls
executable file
·81 lines (65 loc) · 2.14 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
#!/usr/bin/env -S dotnet fsi
#load "Lib/Extensions.fs"
open Extensions
type Person = {
Name: string
Age: int
Sex: Sex
}
and Sex =
| Male
| Female
type Freimaurer =
Freimaurer of level:Level * person:Person
and Level =
| Novice
| Advanced
| Master
type Error =
| TooYoung
| Women
let upgrade (Freimaurer (level,person)) =
match level with
| Novice -> Freimaurer (Advanced,person)
| Advanced -> Freimaurer (Master, person)
| Master -> Freimaurer (Master, person)
let (|Integer|_|) str = System.Int32.tryParse str
let (|Float|_|) str = System.Double.tryParse str
let (|Float32|_|) str = System.Single.tryParse str
let (|Child|Adult|) person =
if person.Age < 18
then Child
else Adult
let (|Sex|) person =
person.Sex
let getLevel (Freimaurer (level,_)) = level
let toFreimaurer person =
match person with
| Sex Female -> Error Women
| Child -> Error TooYoung
| p -> Ok (Freimaurer (Novice, p))
let persons = [
{ Name = "David"; Age = 400; Sex = Male }
{ Name = "Alice"; Age = 17; Sex = Female }
{ Name = "Vivi"; Age = 21; Sex = Female }
{ Name = "Markus"; Age = 38; Sex = Male }
{ Name = "Boris"; Age = 12; Sex = Male }
]
let frei = List.map (fun p -> toFreimaurer p, p) persons
let freimaurer = List.choose (Result.tryOk << fst) frei
frei |> List.iter (fun (frei,person) ->
match frei with
| Ok (Freimaurer (level,p)) -> printfn "Welcome %s to the Freimaurer as %A" p.Name level
| Error Women -> printfn "We don't like women like %s" person.Name
| Error TooYoung -> printfn "Soory, %s you are too young" person.Name
)
for frei,person in frei do
match frei with
| Ok (Freimaurer (level,p)) -> printfn "Welcome %s to the Freimaurer as %A" p.Name level
| Error Women -> printfn "We don't like women like %s" person.Name
| Error TooYoung -> printfn "Soory, %s you are too young" person.Name
freimaurer
|> List.map upgrade
|> List.iter (fun (Freimaurer (level, {Name = name})) ->
printfn "Welcome %s to the Freimaurer as %A" name level
)