Monadic list for a more functional approach to C# I am learning about monadic structures and have created a sort of monad list that implements the map and binding "operators" (or technically methods) in C#.

I understand that Linq already exists and would solve many of the problems that my solution solves (I am in fact already using linq to help in parts of my code) and that lists are technically already monads. However this is still a learning experience for me and so I want to make sure I have the right idea.

class ListMonad&lt;T&gt; : IEnumerable&lt;T&gt;
{
    private readonly List&lt;T&gt; items;
    private List&lt;T&gt; Items =&gt; items;

    public ListMonad(List&lt;T&gt; value)
    {
        this.items = new List&lt;T&gt;(value);
    }

    public ListMonad()
    {
        items = new List&lt;T&gt; { };
    }

    public ListMonad&lt;TO&gt; Bind&lt;TO&gt;(Func&lt;T, ListMonad&lt;TO&gt;&gt; func)
    {
        var lists = new List&lt;ListMonad&lt;TO&gt;&gt;();
        foreach(var el in Items)
            lists.Add(func(el));

        return new ListMonad&lt;TO&gt;(lists.SelectMany(x =&gt; x.Items).ToList());
    }

    public ListMonad&lt;TO&gt; Map&lt;TO&gt;(Func&lt;T, TO&gt; func)
    {
        var result = new ListMonad&lt;TO&gt;();
        foreach(var item in Items)
            result.Items.Add(func(item));

        return result;
    }

    IEnumerator&lt;T&gt; IEnumerable&lt;T&gt;.GetEnumerator()
    {
        foreach(var item in Items)
            yield return item;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable&lt;T&gt;)this).GetEnumerator();
    }

    public T this[int index] =&gt; Items[index];
}


Use:

class Program
{
    static void Main(string[] args)
    {
        var listMonad = new ListMonad&lt;int&gt;(new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7 });

        var doubledAndSquared = Compose((int x) =&gt; x * x, (int x) =&gt; x * 2);
        Func&lt;int, ListMonad&lt;int&gt;&gt; zeroSeparated = x =&gt; new ListMonad&lt;int&gt;(new List&lt;int&gt; { x, 0 });

        listMonad = listMonad
            .Bind(zeroSeparated)
            .Map(doubledAndSquared);

        foreach(var item in listMonad)
            Console.Write(item + ", ");
    }

    static Func&lt;V, U&gt; Compose&lt;T, U, V&gt;(Func&lt;T, U&gt; func1, Func&lt;V, T&gt; func2) =&gt;
        (V x) =&gt; func1(func2(x));
}
Just a few points that I've noticed:


Readonly property can be declared directly like this:

private List&lt;T&gt; Items { get; }

It is not clear if this class is intended to be immutable. The Items is not exposed directly, and its backing field is marked with readonly. However, the indexer is exposed. In case it is, you should use ReadOnlyCollection&lt;T&gt; as the container. If not, an T[] would allow you to mutate its element, but prevents you from adding/removing item.
ctor argument value should pluralized.
An overload of ctor that takes params T[] values would be a nice addition, enabling this type of code: new ListMonad&lt;int&gt;(1, 2, 3)
Another ctor overload that support IEnumerable&lt;T&gt; would be great too, which will allow you to write this:

// this likely defeats the purpose of this class?
public ListMonad&lt;TO&gt; Map&lt;TO&gt;(Func&lt;T, TO&gt; func) =&gt; new ListMonad&lt;TO&gt;(this.Select(func));
public ListMonad&lt;TO&gt; Bind&lt;TO&gt;(Func&lt;T, ListMonad&lt;TO&gt;&gt; func) =&gt; new ListMonad&lt;TO&gt;(this.SelectMany(func));

In the Bind method, you can use AddRange to avoid creating a nested list:

public ListMonad&lt;TO&gt; Bind&lt;TO&gt;(Func&lt;T, ListMonad&lt;TO&gt;&gt; func)
{
    var results = new List&lt;TO&gt;();
    foreach (var element in this)
        results.AddRange(func(element));

    return new ListMonad&lt;TO&gt;(results);
}

The argument order of Compose method seems weird, although it may just be my personal preferences. I would suggest to put the inner-most selector first, and moving toward outside, since this is how we normally solves equations:

public static Func&lt;TInput, TResult&gt; Compose&lt;TInput, T2, TResult&gt;(Func&lt;TInput, T2&gt; func1, Func&lt;T2, TResult&gt; func2)
{
    return x =&gt; func2(func1(x));
}

Compose can also be placed in a generic-ally typed static class to avoid having to explicitly mentioning the generic type multiple times:

public static class FuncHelper&lt;T&gt;
{
    // usage: FuncHelper&lt;int&gt;.Compose(x =&gt; x * 2, x =&gt; x * x)
    public static Func&lt;T, TResult&gt; Compose&lt;T2, TResult&gt;(Func&lt;T, T2&gt; func1, Func&lt;T2, TResult&gt; func2)
    {
        return x =&gt; func2(func1(x));
    }
    // and, you can chain even more function
    public static Func&lt;T, TResult&gt; Compose&lt;T2, T3, TResult&gt;(Func&lt;T, T2&gt; func1, Func&lt;T2, T3&gt; func2, Func&lt;T3, TResult&gt; func3)
    {
        return x =&gt; func3(func2(func1(x)));
    }
    // and, so on ...

    // or, you can chain as long as you want, if the input, intermediate and result types are all the same
    // usage: FuncHelper&lt;int&gt;.Compose(x =&gt; x + 1, x =&gt; x * 2, x =&gt; x + 3, x =&gt; x * 4, ...) 
    public static Func&lt;T, T&gt; Compose(params Func&lt;T, T&gt;[] funcs)
    {
        return x =&gt; funcs.Aggregate(x, (y, f) =&gt; f(y));
    }
}