在循环内我添加了一个新元素到列表中,我需要在下一次迭代中使用可变的.Net List<T>
。F#:是否可以使用可变.Net列表添加元素到同一个列表
F#
鼓励在一般情况下使用不可变集合,并且似乎我无法实现我想要的使用immutable list
或seq
。
继续使用可变的.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')
“可接受”是非常主观的。话虽如此:可以接受吗?最有可能的。必要?很可能不会。更快使用可变吗?不清楚。如果你给更多的上下文,你会得到更好的答案。你如何建立清单?你怎么使用它? –
F#具有可变列表类型的特殊类型缩写,因此可以使用它。 –
几乎可以肯定地有一种不变的选择,如果你给我们提供了一些背景知识,我们可以解释一下。 – TheInnerLight