我对F#非常新,在F#中遇到“收集已修改”问题。我知道这个问题在我们迭代集合的同时修改(添加/删除)它时很常见。并且在stackoverflow中的前面的线程也指向这一点。F#帮助:“收集已修改”
但在我而言,我的工作在2个不同的组: 我有2个类别:
- originalCollection原来的集合。其中我想删除的东西
- colToRemove集合包含我想要删除的物体
以下是代码:
Seq.iter (fun input -> ignore <| originalCollection.Remove(input)) colToRemove
而且我得到以下运行时错误: + $例外{System.InvalidOperationException:集合已修改;枚举操作可能不会执行。 在System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource资源) 在System.Collections.Generic.List 1.Enumerator.MoveNextRare() at System.Collections.Generic.List
1.Enumerator.MoveNext() 在[email protected] [T](FSharpFunc 2 f, IEnumerator
1e中,FSharpRef 1 started, Unit unitVar0) at [email protected]ctions-IEnumerator-MoveNext() at Microsoft.FSharp.Collections.SeqModule.Iterate[T](FSharpFunc
2动作,IEnumerable`1源)
这里是代码块:
match newCollection with
| Some(newCollection) ->
// compare newCollection to originalCollection.
// If there are things that exist in the originalCollection that are not in the newCollection, we want to remove them
let colToRemove = Seq.filter (fun input -> Seq.exists (fun i -> i.id = input.id) newCollection) originalCollection
Seq.iter (fun input -> ignore <| originalCollection.Remove(input)) colToRemove
| None ->()
谢谢!
注意:在此处使用单线程环境,因此不存在可能导致此异常的多线程问题。