2022-05-14 17:25:18 +08:00
|
|
|
|
using Sanchime.Functional.Products;
|
|
|
|
|
using Sanchime.Functional.Extensions;
|
2022-05-08 14:57:11 +08:00
|
|
|
|
using Sanchime.Toolkits;
|
|
|
|
|
|
|
|
|
|
// Test<int> test = new Test<int>();
|
|
|
|
|
|
|
|
|
|
// Console.WriteLine(test.Map((Test<int> x) => {x.Value = 2; return x;}).Value);
|
|
|
|
|
|
|
|
|
|
// Console.WriteLine(test.Value);
|
|
|
|
|
|
2022-05-08 17:06:05 +08:00
|
|
|
|
// foo(10);
|
|
|
|
|
// foo(Option.None);
|
2022-05-08 14:57:11 +08:00
|
|
|
|
|
2022-05-08 17:06:05 +08:00
|
|
|
|
// void foo(Option<int> option)
|
|
|
|
|
// {
|
|
|
|
|
// var res = option.Map(x => x + 2);
|
|
|
|
|
// res.WriteLine();
|
|
|
|
|
// }
|
|
|
|
|
|
2022-05-08 21:09:18 +08:00
|
|
|
|
// Option.Some(1).Map(x => (float)x + 1.2).Map(x => x.ToString()).Map(x => x.GetType().Name).WriteLine();
|
|
|
|
|
|
2022-05-14 17:25:18 +08:00
|
|
|
|
// 测试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);
|
2022-05-08 21:09:18 +08:00
|
|
|
|
|
2022-05-14 17:25:18 +08:00
|
|
|
|
foo("111").WriteLine();
|
|
|
|
|
foo("aaa").WriteLine();
|
|
|
|
|
foo("123").WriteLine();
|
|
|
|
|
// 管道
|
|
|
|
|
foo("1ab").Pipe(x => Console.WriteLine(x));
|
2022-05-08 17:06:05 +08:00
|
|
|
|
public struct Age
|
2022-05-08 14:57:11 +08:00
|
|
|
|
{
|
2022-05-08 17:06:05 +08:00
|
|
|
|
private int _value;
|
|
|
|
|
|
|
|
|
|
public static Option<Age> Of(int age)
|
|
|
|
|
=> IsValid(age) ? Option.Some(new Age(age)) : Option.None;
|
2022-05-08 14:57:11 +08:00
|
|
|
|
|
2022-05-08 17:06:05 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|