Skip to content

Instantly share code, notes, and snippets.

@jmeline
Created July 10, 2019 06:06
Show Gist options
  • Save jmeline/7bb00941616ebb8e3deec2b092e7ea43 to your computer and use it in GitHub Desktop.
Save jmeline/7bb00941616ebb8e3deec2b092e7ea43 to your computer and use it in GitHub Desktop.
Compose in C#
using System;
namespace FunCSharp
{
public static class Extensions
{
// x => f(g(x))
public static Func<T1, T3> ComposeWith<T1, T2, T3>(this Func<T2, T3> f, Func<T1, T2> g)
{
return x => f(g(x));
}
public static T1 Identity<T1>(this T1 x) => x;
}
}
using System;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
namespace FunCSharp.Tests
{
public class UnitTest1
{
private readonly ITestOutputHelper _testOutputHelper;
public UnitTest1(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
private readonly Func<int, Func<int, int>> add = x => y => x + y;
private readonly Func<int, Func<int, int>> sub = x => y => x - y;
private readonly Func<int, Func<int, int>> mult = x => y => x * y;
// private readonly Func<int, int> tap = x =>
// {
// _testOutputHelper.WriteLine(x.ToString());
// return x;
// };
[Fact]
public void Test1()
{
var fn = sub(40)
.ComposeWith(add(10))
.ComposeWith(mult(10))
.ComposeWith(add(1));
fn(2).ShouldBe(0);
}
public class Options
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
[Fact]
public void Test2()
{
Func<Options, Options> applyDefaults = options =>
{
options.Id = 1337;
options.FirstName = "bob";
options.LastName = "dude";
options.Age = 10;
return options;
};
Func<Options, Options> setOptions = options =>
{
options.FirstName = "jacob";
return options;
};
var result = setOptions.ComposeWith(applyDefaults)(new Options());
result.ShouldSatisfyAllConditions(
() => result.Id.ShouldBe(1337),
() => result.FirstName.ShouldBe("jacob"),
() => result.LastName.ShouldBe("dude"),
() => result.Age.ShouldBe(10));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment