Question

I was trying to use Seq.first today, and the compiler says it has been deprecated in favor of Seq.tryPick. It says 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<int>) =
   x.filter(fun x -> x>0)
   |> Seq.tryPick (??)
Was it helpful?

Solution

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.head

let r3 = a |> Seq.head 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top