这似乎有效,但看起来很笨重。有没有更好的方法来编码?
// Hunting for the best index to use for a data compare
let getIndex connDB strTable =
match getIndexByPrimaryKey connDB strTable with
| Some(name) -> Some(name)
| None ->
match getIndexByCluster connDB strTable with
| Some(name) -> Some(name)
| None ->
match getIndexByFirstColumns connDB strTable with
| Some(name) -> Some(name)
| None ->
match getIndexByOnlyUnique connDB strTable with
| Some(name) -> Some(name)
| None ->
match getAnyUniqueIndex connDB strTable with
| Some(name) -> Some(name)
| None -> None
最佳答案
可能最简洁的版本是使用 List.tryPick :
let getIndex connDB strTable =
[ getIndexByPrimaryKey;
getIndexByCluster;
getIndexByFirstColumns;
getIndexByOnlyUnique;
getAnyUniqueIndex; ]
|> List.tryPick (fun fn -> fn connDB strTable)
更新:
该技术可以轻松扩展为使用闭包,因此它适用于具有不同参数的函数(还有很多
fun
:-)):let getIndex connDB strTable =
[ fun () -> getIndexByPrimaryKey connDB strTable;
fun () -> getIndexByCluster connDB strTable;
fun () -> getIndexByFirstColumns connDB strTable;
fun () -> getIndexByOnlyUnique connDB strTable;
fun () -> getAnyUniqueIndex connDB strTable; ]
|> List.tryPick (fun fn -> fn())
关于f# - 我的匹配看起来很笨重,有没有更好的方法来编码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12610745/