Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Created May 9, 2018 14:50
Show Gist options
  • Save arialdomartini/6d7ad5869b23376cef5c9b78f729657b to your computer and use it in GitHub Desktop.
Save arialdomartini/6d7ad5869b23376cef5c9b78f729657b to your computer and use it in GitHub Desktop.
Tuples without fields nor variables
using System;
using FluentAssertions;
using Xunit;
namespace SamuraiTDD.WithSum
{
public class MyTupleIntTest
{
[Fact]
public void should_retain_values()
{
var tuple = MyTuple.Build(44, 100);
tuple.First().Should().Be(44);
tuple.Second().Should().Be(100);
}
[Fact]
public void should_add_tuples()
{
var tuple1 = MyTuple.Build(2, 5);
var tuple2 = MyTuple.Build(100, 50);
var result = tuple1.Add(tuple2);
result.First().Should().Be(102);
result.Second().Should().Be(55);
}
}
public static class MyTuple
{
public static Func<Func<int, int, int>, int> Build(int a, int b) => f => f(a, b);
public static int First(this Func<Func<int, int, int>, int> tuple) => tuple((a, _) => a);
public static int Second(this Func<Func<int, int, int>, int> tuple) => tuple((_, b) => b);
public static Func<Func<int, int, int>, int> Add(
this Func<Func<int, int, int>, int> a,
Func<Func<int, int, int>, int> b) => Build(a.First() + b.First(), a.Second() + b.Second());
}
}
using System;
using FluentAssertions;
using Xunit;
namespace SamuraiTDD.WithFunc
{
public class MyTupleTest
{
[Fact]
public void should_retain_values()
{
var tuple = MyTuple.Build(44, 100);
tuple.First().Should().Be(44);
tuple.Second().Should().Be(100);
}
}
public static class MyTuple
{
public static Func<Func<T, T, T>, T> Build<T>(T a, T b) => f => f(a, b);
public static T First<T>(this Func<Func<T, T, T>, T> tuple) => tuple((a, b) => a);
public static T Second<T>(this Func<Func<T, T, T>, T> tuple) => tuple((a, b) => b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment