where - Indexed

Category:
LINQ
Description: This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.
Code:
let LINQ02() =
let digits = [ "zero"; "one"; "two"; "three"; "four"; "five"; "six"; "seven"; "eight"; "nine" ]
query {
for d in digits do
select d
}
|> Seq.mapi (fun i d -> if d.Length < i then Some(d) else None)
|> Seq.choose id
|> Seq.iter(fun n -> printfn "The word %s is shorter than its value." n)

//sample output
// The word five is shorter than its value.
// The word six is shorter than its value.
// The word seven is shorter than its value.
// The word eight is shorter than its value.
// The word nine is shorter than its value.


Execution Result:
The word five is shorter than its value.
The word six is shorter than its value.
The word seven is shorter than its value.
The word eight is shorter than its value.
The word nine is shorter than its value.

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

Comments

Vxsery Oct 18, 2011 at 2:18 AM 
I don't see how this is any different from just skipping the query statement all together and just passing digits to the rest of the pipe...