我如何处理 F# 中的 IEnumerable? [英] How do I deal with IEnumerable in F#?
本文介绍了我如何处理 F# 中的 IEnumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有很多遗留接口以纯 IEnumerable
的形式获取实体集合.通常可以在 C# 中执行 foreach(CertainTypeWeSureItemIs item in items)
将对象转换为他们想要的任何类型.IEnumerable 不会直接转换为序列.将它包装在 seq { for x in xs ->x }
也没有多大帮助,因为它得到 seq{obj}
.那么我如何在 F# 中做到这一点?
There are quite a few legacy interfaces that get collections of entities in form of plain IEnumerable
. Commonly one would do foreach(CertainTypeWeSureItemIs item in items)
in C# casting objects to whatever type they want as they go. IEnumerable doesn't translate directly to a sequence. Wrapping it in seq { for x in xs -> x }
doesn't help much either because it gets seq{obj}
. So how do I do this in F#?
推荐答案
使用Seq.cast
:
let al = new System.Collections.ArrayList()
al.Add(1) |> ignore
al.Add(2) |> ignore
al |> Seq.cast<int> |> Seq.iter(printf "%i")
这篇关于我如何处理 F# 中的 IEnumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文