Hello, I'd appreciate some help. I have a collection of items that have optional properties. What is the correct way to filter only those items that actually are in Some state? This is a test case I came up with:
using FluentAssertions;
using LanguageExt;
using System;
using System.Collections.Generic;
using Xunit;
using static LanguageExt.Prelude;
namespace OptionCollectionTest
{
public class UnitTest1
{
[Fact]
public void Test1()
{
var todos = new List<Todo>
{
new Todo { Id = 1, Description = None },
new Todo { Id = 2, Description = Some("This is a description of Todo(2)") },
new Todo { Id = 3, Description = Some("This is a description of Todo(3)") }
};
var expectedDescriptions = new List<string>
{
"This is a description of Todo(2)",
"This is a description of Todo(3)"
};
var actualDescriptions = GetDescriptions(todos);
actualDescriptions.Should().BeEquivalentTo(expectedDescriptions);
}
private IEnumerable<string> GetDescriptions(IEnumerable<Todo> todos)
{
throw new NotImplementedException();
}
private class Todo
{
public int Id { get; set; }
public Option<string> Description { get; set; }
}
}
}
I did some reading on how to filter out None (Nothing) values from lists in Elm/F# and some of them have indicated usage of identity function, but I was not able to figure out how to apply it to this library.
Try todos.Somes()
For the specific test case, todos.Map(t => t.Description).Somes()
I'm an idiot :)
Thank you both!
No need to be too hard on yourself. It is only obvious once you have seen it for the first time.
Most helpful comment
Try
todos.Somes()