-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
46 lines (42 loc) · 1.62 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
namespace Awaitables.Example
{
internal class Program
{
private static void Main(string[] args)
{
var program = GetSelectedProgram();
if (program.HasValue)
{
program.Value();
}
else
{
Console.WriteLine("Invalid argument");
}
async Option<Action> GetSelectedProgram()
{
var arg = await args.ElementAt(0);
var asInt = await arg.TryParseInt();
return await _programs.TryGetValue(asInt);
}
}
public static Dictionary<int, Action> _programs = GeneratePrograms();
private static Dictionary<int, Action> GeneratePrograms() => new Dictionary<int, Action>
{
{ 1, () => Console.WriteLine("Play chess") },
{ 2, () => Console.WriteLine("Solve World Hunger") },
{ 3, () => Console.WriteLine("Win a noble prize") },
};
}
public static class Extensions
{
public static Option<T> ElementAt<T>(this IReadOnlyList<T> list, int index)
=> list.Count > index ? Option.Some(list[index]) : Option.None<T>();
public static Option<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
=> dictionary.TryGetValue(key, out var value) ? Option.Some(value) : Option.None<TValue>();
public static Option<int> TryParseInt(this string str)
=> int.TryParse(str, out var value) ? Option.Some(value) : Option.None<int>();
}
}