Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you work with IList<> in F#?

Tags:

sum

f#

ilist

I have a list of type IList<Effort>. The model Effort contains a float called Amount. I would like to return the sum of Amount for the whole list, in F#. How would this be achieved?

like image 762
kim3er Avatar asked Apr 03 '10 15:04

kim3er


2 Answers

efforts |> Seq.sumBy (fun e -> e.Amount)
like image 54
Mauricio Scheffer Avatar answered Oct 06 '22 02:10

Mauricio Scheffer


Upvoted the answers of Seq.fold, pipelined Seq.fold, and pipelined Seq.sumBy (I like the third one best).

That said, no one has mentioned that seq<'T> is F#'s name for IEnumerable<T>, and so the Seq module functions work on any IEnumerable, including ILists.

like image 43
Brian Avatar answered Oct 06 '22 01:10

Brian