是否存在具有持久不可变Vector类的.net库(如Clojure / Scala中所示)?

时间:2011-12-15 07:55:50

标签: c# .net collections clojure functional-programming

我花了一个小时的谷歌搜索,并可以找到各种.NET不可变列表,集和地图。我虽然无法找到持久的不可变Vector。

像Scala的不可变向量之类的东西就是我所追求的(我相信它在Clojure中类似)。它必须可以从C#库中调用。

Microsoft-land中是否存在这样的事情?

2 个答案:

答案 0 :(得分:2)

您可以查看ClojureCLR implementation。 它是在CLR / DLR执行引擎之上的Clojure语言的实现。 我想你不能简单地将代码复制并粘贴到你的项目中,但是只需要很少的工作就可以提取特定的持久数据结构。 查看Clojure \ Clojure \ Lib中的实现,例如PersistentVector.cs

伊多。

答案 1 :(得分:1)

callable包中有持久性向量的.NET实现(来自C#库的FSharpX.Collections),NuGet上也是available

以下是项目首页的使用示例:

// Create an empty PersistentVector and add some elements
PersistentVector<string> vector =
    PersistentVector<string>.Empty()
        .Conj("hello")
        .Conj("world")
        .Conj("!");

PersistentVector<string> vector2 = vector.Conj("!!!").Update(0,"hi");

Console.WriteLine(vector2[0]); // hi
Console.WriteLine(vector[0]);  // hello
Console.WriteLine(vector2[3]); // !!!

Console.WriteLine(vector.Length);  // 3
Console.WriteLine(vector2.Length); // 4

// remove the last element from a PersistentVector
PersistentVector<string> vector3 = vector2.Initial;

Console.WriteLine(vector3.Length); // 3