Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - Is there a singly-linked list in System.Collections.Immutable somewhere?

In functional programming, singly linked lists are extremely popular, because it is easy to reuse sub-lists without allocating any memory or copying values. This means you can add or remove items from one end without any allocation. The F# list is like this.

From what I've read, it sounds like System.Collections.Immutable.ImmutableList<T> is like an immutable version of System.Collections.Generic.List<T>, which is an abstraction over arrays. This is more optimized for random access than a linked list, but requires copying the entire list when adding or removing items.

System.Collections.Generic.LinkedList<T> is a mutable doubly-linked list, which means that either mutation and/or copying is required to add or remove items.

There is no System.Collections.Immutable.ImmutableLinkedList<T> that I've been able to find.

Is there really no immutable singly-linked list in the System.Collections.Immutable package? Is using Microsoft.FSharp.Core.List<T> the best option here?

like image 414
JamesFaix Avatar asked Nov 01 '17 23:11

JamesFaix


People also ask

Are linked lists immutable?

So, we know linked lists are recursive in nature. Combined with the immutable nature of the language, we know that the data can never change. This is interesting, because now we can confidently say that A -> B -> C -> D is a different l ist from B -> C -> D (even though one recursively contains the other one).

Are lists immutable in C#?

The C# List Is Mutable. The C# list is mutable. This means you can easily add or remove elements when you need to.

Which operation is not possible in singly linked list?

We can easily traverse to its previous node in a circular linked list, which is not possible in a singly linked list.

Why linked list is rarely used?

A drawback of linked lists is that access time is linear (and difficult to pipeline). Faster access, such as random access, is not feasible. Arrays have better cache locality compared to linked lists.


1 Answers

There is (as of this writing), no great answer to this in the .NET base class library.

The closest thing in System.Collections.Immutable is ImmutableStack<T>, which is implemented as a singly-linked list under the hood. However, the exposed functions are very limited so unless you actually want a stack this is probably a poor choice. You are correct that ImmutableList<T> is a different datastructure with different operations and performance characteristics.

There is FSharpList<T> in the F# standard library, but due to being an F# primitive this is awkward to use from C# (it's possible, it just won't result in clean, idiomatic code). Also requires taking the full F# standard lib as a dependency.

Having faced the same problem in one of my projects, I wrote a NuGet package "ImmutableLinkedList" (github here) for this, so that could be an option as well.

like image 192
ChaseMedallion Avatar answered Oct 06 '22 00:10

ChaseMedallion