You probably are looking for something like this:

1
2
3
4
5
6
7
8
9
10
11
12
 > let values = [ 1, 0.1; 1, 0.2; 3, 0.9; 1, 0.8 ];;

val values : (int * float) list = [(1, 0.1); (1, 0.2); (3, 0.9); (1, 0.8)]

> let res = values 
          |> Seq.groupBy fst
          |> Seq.map (fun (key, vals) -> key, Seq.sumBy snd vals);;

val res : seq<int * float>

> res;;
val it : seq<int * float> = seq [(1, 1.1); (3, 0.9)]

To start, we take the values and group them on the first element in the tuple. This gives us a seq<int * seq<int * float>>. Then we project that sequence, pattern matching out the key (int) and the vals (seq<int * float>). We return the key unaltered, and call sumBy on the values, summing only the second element in the tuple (the floats).

By on 9/3/2009 5:15 AM ()

Thank you both for the prompt replys

I got as far as the Seq.group_by

but couldn't work out the form of the summation step.

I think I will have the wind in my heels for the next few days until the next one

Carl

By on 9/3/2009 7:22 AM ()

Hi,

You should look at groupBy, map and sumBy functions.
Here's my try (I have no F# compiler here, not sure if it compiles):

1
2
3
4
my_table |> Seq.groupBy fst
        |> Seq.map (fun (k, li) -> k, li |> Seq.sumBy snd)

Or, using comprehensions and generating a list:

1
2
3
[ for k, li in Seq.groupBy fst my_table -> k, Seq.sumBy snd li ]

Laurent

By on 9/3/2009 5:06 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper