测试了Option的一些操作

This commit is contained in:
Sanchime 2022-05-08 17:06:05 +08:00
parent 83cc3df7aa
commit d50e84cbbd
1 changed files with 37 additions and 6 deletions

View File

@ -8,12 +8,43 @@ using Sanchime.Toolkits;
// Console.WriteLine(test.Value);
foo(10);
foo(Option.None);
// foo(10);
// foo(Option.None);
void foo(Option<int> option)
// void foo(Option<int> option)
// {
// var res = option.Map(x => x + 2);
// res.WriteLine();
// }
// 测试Option的Bind
var parse = (string s) => Int32.TryParse(s, out int i) ? Option.Some(i) : Option.None;
var foo = (string s) => s.Pipe(parse).Bind(Age.Of);
foo("111").WriteLine();
foo("aaa").WriteLine();
foo("123").WriteLine();
// 管道
foo("1ab").Pipe(x => Console.WriteLine(x));
public struct Age
{
var res = option.Map(x => x + 2);
res.WriteLine();
}
private int _value;
public static Option<Age> Of(int age)
=> IsValid(age) ? Option.Some(new Age(age)) : Option.None;
private Age(int age)
{
if (!IsValid(age))
{
throw new ArgumentException("输入的年龄是无效的");
}
_value = age;
}
private static bool IsValid(int age)
=> age is (>= 0 and <= 150);
public override string ToString()
=> _value.ToString();
}