public
Created

a-plus-abs-b

  • Download Gist
a-plus-abs-b.cs
C#
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);
}
}
}

What do you mean? The execution is already delayed as @if is an expression.

On Mon, Feb 6, 2012 at 4:12 PM, Mauricio Scheffer <
reply@reply.github.com

wrote:

You need to delay the condition to make it work like a regular 'if'. E.g.
use a Func parameter instead of bool


Reply to this email directly or view it on GitHub:
https://gist.github.com/1336143

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.