This project is read-only.
groupValBy - Simple 1

Category:
LINQ
Description: This sample uses group by to partition a list of numbers by their remainder when divided by 5.
Code:
let LINQ23() =
let digits = [ "zero"; "one"; "two"; "three"; "four"; "five"; "six"; "seven"; "eight"; "nine" ]

let numbers = [ 5; 4; 1; 3; 9; 8; 6; 7; 2; 0 ]

query {
for n in numbers do
groupValBy n (n % 5) into g
select (g.Key, g.ToArray())
}
|> Seq.iter(fun (n,e) -> printfn "Numbers with a remainder of %A when divided by 5:" n; (e|>Seq.iter(fun x -> printfn"%d" x)))


//sample output
// Numbers with a remainder of 0 when divided by 5:
// 5
// 0
// Numbers with a remainder of 4 when divided by 5:
// 4
// 9
// Numbers with a remainder of 1 when divided by 5:
// 1
// 6
// Numbers with a remainder of 3 when divided by 5:
// 3
// 8
// Numbers with a remainder of 2 when divided by 5:
// 7
// 2


Execution Result:
Numbers with a remainder of 0 when divided by 5:
5
0
Numbers with a remainder of 4 when divided by 5:
4
9
Numbers with a remainder of 1 when divided by 5:
1
6
Numbers with a remainder of 3 when divided by 5:
3
8
Numbers with a remainder of 2 when divided by 5:
7
2

Last edited Sep 14, 2011 at 2:49 AM by ttliu2000, version 1

Comments

No comments yet.