1
2
3
4
|
public Func< int , int , decimal > SquareForCivil() { ???? return (width,hight)=>width*hight; } |
1
2
3
4
|
public Func< int , int , decimal > SquareForBusiness() { ???? return (width, hight) => width * hight*1.2m; } |
1
2
3
4
|
public decimal PropertyFee( decimal price, int width, int hight, Func< int , int , decimal > square) { ???? return price*square(width, hight); } |
1
2
3
4
5
6
7
8
9
10
11
12
|
[Test] public void Should_calculate_propertyFee_for_two_area() { ???? //Arrange ???? var calculator = new PropertyFeeCalculator(); ???? //Act ???? var feeForBusiness= calculator.PropertyFee(2m,2, 2, calculator.SquareForBusiness()); ???? var feeForCivil = calculator.PropertyFee(1m, 2, 2, calculator.SquareForCivil()); ???? //Assert ???? feeForBusiness.Should().Be(9.6m); ???? feeForCivil.Should().Be(4m); } |
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
|
public double MemoryUtilization() ? { ????? //计算目前内存使用率 ????? var pcInfo = new ComputerInfo(); ????? var usedMem = pcInfo.TotalPhysicalMemory - pcInfo.AvailablePhysicalMemory; ????? return ( double )(usedMem / Convert.ToDecimal(pcInfo.TotalPhysicalMemory)); ? } ? ?? public int BigCalculatationForFirstStep() ? { ????? //第一步运算 ????? System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2)); ????? Console.WriteLine( "big calulation" ); ????? FirstStepExecuted = true ; ????? return 10; ? } ? ?? public void NextStep( double memoryUtilization, int firstStepDistance) ? { //下一步运算 ????? if (memoryUtilization<0.8&&firstStepDistance<100) ????? { ????????? Console.WriteLine( "Next step" ); ????? } ? } |
1
2
3
4
5
6
7
|
public void NextStepWithOrderFunction(Func< double > memoryUtilization,Func< int > firstStep) { ???? if (memoryUtilization() < 0.8 && firstStep() < 100) ???? { ???????? Console.WriteLine( "Next step" ); ???? } } |
1
2
3
4
|
public Func< int , int , int > AddTwoNumber() { ???? return (x, y) => x + y; } |
1
|
var result= _curringReasoning.AddTwoNumber()(1,2); |
1
2
3
4
5
|
public Func< int , Func< int , int >> AddTwoNumberCurrying() { ???? Func< int , Func< int , int >> addCurrying = x => y => x + y; ???? return addCurrying; } |
1
2
3
4
5
6
|
//Act var curringResult = curringReasoning.AddTwoNumberCurrying()(10); var result = curringResult(2); ? ?//Assert result.Should().Be(12); |
1
2
3
4
|
public Func< int , int , int , int > AddThreeNumber() { ???? return (x, y, z) => x + y + z; } |
1
2
3
4
5
|
public Func< int ,Func< int ,Func< int , int >>> AddThreeNumberCurrying() { ???? Func< int , Func< int , Func< int , int >>> addCurring = x => y => z => x + y + z; ???? return addCurring; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[Test] public void Three_number_add_test() { ???? //Arrange ???? var curringReasoning = new CurryingReasoning(); ? ????? //Act ???? var result1 = curringReasoning.AddThreeNumber()(1, 2, 3); ???? var curringResult = curringReasoning.AddThreeNumberCurrying()(1); ???? var curringResult2 = curringResult(2); ???? var result2 = curringResult2(3); ???? ????? //Assert ???? result1.Should().Be(6); ???? result2.Should().Be(6); } |
1
2
3
4
5
6
7
8
9
|
public static Func this Func { ???? return x => y => func(x, y); } ? ?public static Func this Func { ???? return x => y => z=>func(x, y,z); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Test] public void Should_auto_curry_two_number_add_function() { ???? //Arrange ???? var add = _curringReasoning.AddTwoNumber(); ???? var addCurrying = add.Curry(); ? ????? //Act ???? var result = addCurrying(1)(2); ? ????? //Assert ???? result.Should().Be(3); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private readonly List<> string , bool >> _conditions; ? ?public WordFinder { ???? Func< string , bool > searchCondition = word => expression(_model).ToString().Split( ' ' ).Contains(word); ???? _conditions.Add(searchCondition); ???? return this ; } ? ?public bool Execute( string wordList) { ???? return _conditions.Any(x=>x(wordList)); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[Test] public void Should_find_a_word() { ???? //Arrange ???? var article = new Article() ???? { ???????? Title = "this is a title" , ???????? Content = "this is content" , ???????? Comment = "this is comment" , ???????? Author = "this is author" ???? }; ? ????? //Act ???? var result = Finder.For(article) ???????? .Find(x => x.Title) ???????? .Find(x => x.Content) ???????? .Find(x => x.Comment) ???????? .Find(x => x.Author) ???????? .Execute( "content" ); ? ????? //Assert ???? result.Should().Be( true ); } |