Take the following example,
I have a class
public class SomeItem
{
public string Name;
public DateTime Published;
public uint16 Size;
}
I have a List<SomeItem> and I want to calculate the total size of all the items.
In C# I’d simply write
var totalSize = items.Sum((i) => i.Size);
I have taken a look at the List functions in F# but they always complain about the types.
How would you write this in F#?
(I have tried the search engines, but search engine support for F# is terrible)
Assuming you have a value of type
IEnumerable<Item>, you can usesum_byfrom theSeqmodule:Note that F#’s
Microsoft.FSharp.Collections.List<T>type is not the same class as the one you might be used to fromSystem.Collections.Generic.List<T>, and they’re not interchanegable. However, the methods in theSeqmodule work on anyIEnumerable<T>, andIEnumerable<T>is the same between F# and C#.