-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (108 loc) · 5.03 KB
/
Program.cs
File metadata and controls
145 lines (108 loc) · 5.03 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//==================================================================================================
// Demonstrates the usage of Fox.OptionKit library with practical examples.
// Shows Some, None, Map, Bind, Match, ValueOr, and ToOption operations.
//==================================================================================================
using Fox.OptionKit;
//======================================================================================================
// Basic usage: Some and None
//======================================================================================================
Console.WriteLine("=== Basic Usage ===");
var someValue = Option.Some(42);
Console.WriteLine($"someValue: {someValue}");
Console.WriteLine($"Has value: {someValue.HasValue}");
Console.WriteLine($"Value: {someValue.Value}");
var noneValue = Option.None<int>();
Console.WriteLine($"\nnoneValue: {noneValue}");
Console.WriteLine($"Is none: {noneValue.IsNone}");
//======================================================================================================
// ValueOr: Safe value access with default
//======================================================================================================
Console.WriteLine("\n=== ValueOr ===");
var value1 = someValue.ValueOr(0);
var value2 = noneValue.ValueOr(100);
Console.WriteLine($"someValue.ValueOr(0): {value1}");
Console.WriteLine($"noneValue.ValueOr(100): {value2}");
//======================================================================================================
// Map: Transform values
//======================================================================================================
Console.WriteLine("\n=== Map ===");
var mapped = Option.Some(10)
.Map(x => x * 2)
.Map(x => x + 5)
.Map(x => $"Result: {x}");
Console.WriteLine($"Mapped chain: {mapped}");
var mappedNone = Option.None<int>()
.Map(x => x * 2)
.Map(x => x + 5);
Console.WriteLine($"Mapped none: {mappedNone}");
//======================================================================================================
// Bind: Monadic composition
//======================================================================================================
Console.WriteLine("\n=== Bind ===");
Option<int> Divide(int numerator, int denominator)
{
return denominator == 0 ? Option.None<int>() : Option.Some(numerator / denominator);
}
var bindResult1 = Option.Some(100)
.Bind(x => Divide(x, 2))
.Bind(x => Divide(x, 5));
Console.WriteLine($"100 / 2 / 5: {bindResult1}");
var bindResult2 = Option.Some(100)
.Bind(x => Divide(x, 0))
.Bind(x => Divide(x, 5));
Console.WriteLine($"100 / 0 / 5: {bindResult2}");
//======================================================================================================
// Match: Pattern matching
//======================================================================================================
Console.WriteLine("\n=== Match ===");
var matchResult1 = Option.Some(42).Match(
some: value => $"Found: {value}",
none: () => "Not found");
Console.WriteLine($"Match Some: {matchResult1}");
var matchResult2 = Option.None<int>().Match(
some: value => $"Found: {value}",
none: () => "Not found");
Console.WriteLine($"Match None: {matchResult2}");
//======================================================================================================
// ToOption: Convert nullable to option
//======================================================================================================
Console.WriteLine("\n=== ToOption ===");
string? nullableString = "Hello";
var optionFromNullable = nullableString.ToOption();
Console.WriteLine($"From non-null string: {optionFromNullable}");
nullableString = null;
var optionFromNull = nullableString.ToOption();
Console.WriteLine($"From null string: {optionFromNull}");
int? nullableInt = 42;
var optionFromInt = nullableInt.ToOption();
Console.WriteLine($"From nullable int (42): {optionFromInt}");
nullableInt = null;
var optionFromNullInt = nullableInt.ToOption();
Console.WriteLine($"From nullable int (null): {optionFromNullInt}");
//======================================================================================================
// Real-world example: Safe dictionary lookup
//======================================================================================================
Console.WriteLine("\n=== Real-World Example ===");
var users = new Dictionary<int, string>
{
{ 1, "Alice" },
{ 2, "Bob" },
{ 3, "Charlie" }
};
Option<string> GetUser(int id)
{
return users.TryGetValue(id, out var name) ? Option.Some(name) : Option.None<string>();
}
var user1 = GetUser(1)
.Map(name => name.ToUpper())
.ValueOr("UNKNOWN");
Console.WriteLine($"User 1: {user1}");
var user99 = GetUser(99)
.Map(name => name.ToUpper())
.ValueOr("UNKNOWN");
Console.WriteLine($"User 99: {user99}");
var greeting = GetUser(2).Match(
some: name => $"Hello, {name}!",
none: () => "Hello, stranger!");
Console.WriteLine($"Greeting: {greeting}");
Console.WriteLine("\n=== Demo Complete ===");