Created
November 3, 2011 09:39
-
-
Save shishkin/1336143 to your computer and use it in GitHub Desktop.
a-plus-abs-b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Author
shishkin
commented
Feb 6, 2012
via email
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 < ***@***.*** > wrote:
You need to delay the condition to make it work like a regular 'if'. E.g.
use a Func<bool> parameter instead of bool
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1336143
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment