Skip to content

Instantly share code, notes, and snippets.

@jovaneyck
Last active July 3, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovaneyck/33faf4459490ff013762e5997633eece to your computer and use it in GitHub Desktop.
Save jovaneyck/33faf4459490ff013762e5997633eece to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Xunit;
using LanguageExt;
using static LanguageExt.Prelude;
namespace LanguageExtDemo
{
public class Either
{
[Fact]
public void mapping_a_list_of_eithers_to_an_either_of_lists()
{
Assert.Equal(
Right(List(1,2,3)),
List((Either<string,int>)Right(1), Right(2), Right(3)).Sequence());
Assert.Equal(
Left("error"),
List((Either<string,int>)Right(1), Right(2), Left("error"), Right(3)).Sequence());
}
[Fact]
public void mapping_an_enumerable_of_eithers_to_an_either_of_enumerable()
{
Either<string,List<int>> result = new List<Either<string, int>> {Right(1), Right(2), Right(3)}
.Aggregate(
(Either<string, List<int>>) Right(new List<int>()),
(ma, mb) =>
from a in ma
from b in mb
select a.Concat(new []{b}).ToList());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment