.net - F# : Is it okay to use mutable .Net List 将元素添加到同一列表

标签 .net f# immutability mutable

在循环内,我将一个新元素添加到下一次迭代中所需的列表中,为此我使用可变的 .Net List<T> .

F#一般来说,鼓励使用不可变集合,而且似乎我无法使用不可变来实现我想要的listseq .

继续使用可变的.Net是否可以接受List<T> ,或者鼓励只使用不可变的?如果是这样我怎样才能实现这一目标?

我的代码有点长且复杂,所以让我们考虑这个伪 F# 代码:

let children = new List<node>()
let bestBranchEntropies = entropiesSuchAs data.Rows parents
//Finding the best children for the best parent
bestBranchEntropies |> Seq.iter (fun bestBranch -> let attribut = IGetTheAttributByMaximumGainHere
                                                   //Creating one of the children in every iteration
                                                   let node = {
                                                                 content = attribut;
                                                                 branch = Some(fst bestBranch);
                                                                 children = None;
                                                                 isLeaf = false;
                                                              }
                                                   //Considering it a child
                                                   children.Add node

                                )
                       //After having all the children
                       let children' = children |> Seq.map (fun child ->  {
                                                                            content = child.content; 
                                                                            branch = child.branch; 
                                                                            children = //recursive call here to do the same work above (getting children again, yeah it's a tree)
                                                                            isLeaf = child.isLeaf;
                                                                          })

                       Some(children')

最佳答案

据我所知,当然不需要可变列表(如果您的伪代码完全反射(reflect)了问题)。考虑以下因素:

let children = 
    bestBranchEntropies 
    |> Seq.map (fun bestBranch -> 
        let attribut = IGetTheAttributByMaximumGainHere
        //Creating one of the children in every iteration
        {
            content = attribut;
            branch = Some(fst bestBranch);
            children = None;
            isLeaf = false;
        }
    |> Seq.toList
children 
|> Seq.map (fun child ->  
    {
        content = child.content
        branch = child.branch
        children = //recursive call here to do the same work above (getting children again, yeah it's a tree)
        isLeaf = child.isLeaf
    }
)
|> Some

可以跳过第一个 Seq.toList,并且您可以一直使用管道。从你的伪代码来看,整个第二个循环实际上可以安全地与第一个循环合并?

关于.net - F# : Is it okay to use mutable .Net List 将元素添加到同一列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403185/

相关文章:

.net - 如何对配置进行单元测试

c# - 您如何将此查询转换为方法语法?

c# - 使用 FSharp.Data 将 Web 应用程序部署到 Azure 网站

java - 不可变类的例子

c# - 如何轻松测试 C# 应用程序找不到外部程序集的情况?

asp.net - 来自 Visual Studio 调试的 localhost 的 ERR_SSL_PROTOCOL_ERROR

.net - `type private MyRecord = {...}` 和 `type MyRecord = private {...}` 有什么区别

visual-studio - 在 F# 中调用另一个 dll 的类型提供程序

Python改变函数中的变量和数组?

java - 如何在不显式使用构造函数的情况下生成不可变的 DTO?