Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

a-plus-abs-b

View a-plus-abs-b.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
namespace FunctionalCSharp
{
using System;
 
using Shouldly;
 
using Xunit;
 
public class Tests
{
[Fact]
public void Elementary_blocks()
{
Func<int, int, bool> gt = (a, b) => a > b;
Func<int, int, int> add = (a, b) => a + b;
Func<int, int, int> sub = (a, b) => a - b;
Func<bool, Func<int, int, int>, Func<int, int, int>, Func<int, int, int>> @if =
(condition, @then, @else) => condition ? @then : @else;
Func<int, int, int> a_plus_abs_b = (a, b) => @if(gt(b, 0), add, sub)(a, b);
 
a_plus_abs_b(5, -4).ShouldBe(9);
}
 
[Fact]
public void Non_functional_way()
{
Func<int, int, int> a_plus_abs_b = (a, b) => a + Math.Abs(b);
 
a_plus_abs_b(5, -4).ShouldBe(9);
}
}
}
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.