Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# List.first deprecated, what is the new method

Tags:

f#

I am porting over some old F# code from CTP 1.9.6.8

The code uses List.first:

List.first (fun x -> if x.Date = d then Some(x) else None)

List.first has been deprecated. What is the current method used to achieve the same functionality.

I have reviewed the release notes and could not find any specific reference to the change.

Any help would be greatly appreciated.

like image 761
TonyAbell Avatar asked Dec 12 '09 00:12

TonyAbell


3 Answers

Try List.pick

List.pick (fun x -> if x.Date = d then Some(x) else None)
like image 77
JaredPar Avatar answered Oct 23 '22 06:10

JaredPar


@JaredPar is right.

Note that the F# library docs are here:

http://msdn.microsoft.com/en-us/library/ee353567(VS.100).aspx

and specifically the List module is here:

http://msdn.microsoft.com/en-us/library/ee353738(VS.100).aspx

and searching for 'first' on that page reveals the usual suspects.

like image 37
Brian Avatar answered Oct 23 '22 05:10

Brian


What @JaredPar suggested with List.pick is right, though it would raise a KeyNotFoundException if the element does not exist.

You can use List.tryPick if you want an option to be returned in both cases, found and not found.

It's use is the same:

List.tryPick (fun x -> if x.Date = d then Some(x) else None)

like image 41
raukodraug Avatar answered Oct 23 '22 07:10

raukodraug