Skip to content

Instantly share code, notes, and snippets.

@shishkin
Created November 3, 2011 09:39
Show Gist options
  • Save shishkin/1336143 to your computer and use it in GitHub Desktop.
Save shishkin/1336143 to your computer and use it in GitHub Desktop.
a-plus-abs-b
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);
}
}
}
@shishkin
Copy link
Author

shishkin commented Feb 6, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment