Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deal with IEnumerable in F#?

Tags:

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#?

like image 555
Trident D'Gao Avatar asked Sep 28 '13 17:09

Trident D'Gao


1 Answers

Use Seq.cast<T>:

let al = new System.Collections.ArrayList()
al.Add(1) |> ignore
al.Add(2) |> ignore
al |> Seq.cast<int> |> Seq.iter(printf "%i")
like image 81
Lee Avatar answered Oct 04 '22 21:10

Lee