tags:

views:

80

answers:

1

I was trying to use Seq.first today, and the compiler says it has been deprecated in favor of Seq.tryPick. It says that that it applies a function and returns the first result that returns Some. I guess I can just say fun x -> x!=0 since I know the first one will return Some in my case, but what is the proper constraint to put here? What is the correct syntax?

To clarify, I want to use it in the format:

let foo(x:seq) = x.filter(fun x -> x>0) |> Seq.tryPick (??)

+6  A: 

The key is that 'Seq.first' did not return the first element, rather it returned the first element that matched some 'choose' predicate:

let a = [1;2;3]
// two ways to select the first even number (old name, new name)
let r1 = a |> Seq.first (fun x -> if x%2=0 then Some(x) else None) 
let r2 = a |> Seq.tryPick (fun x -> if x%2=0 then Some(x) else None)

If you just want the first element, use Seq.hd

let r3 = a |> Seq.hd
Brian
Thanks, I just tried 'seq.first' and got the deprecated message, but have never used it before, so I didn't know the exact syntax. Seq.hd works perfect.
esac