这个答案只涵盖了为什么<|
使用而不是|>
.
这里有 5 个示例,它们致力于使用<|
大型函数。第 6 个示例是显示使用|>
而不是<|
. 关键是使用第 6 个示例,|>
您必须查看代码以找到主要功能funThatNeedsListAndFunc
,但<|
主要功能funThatNeedsListAndFunc
是显而易见的。因此,您通常会看到<|
最后一个参数是一个函数,并且您希望在主函数之后传入该函数以便于理解。就这样; 不要阅读更多内容。
在阅读了 Mark 的博客后,我还了解到删除around<|
很有用。使用示例如示例 7 所示。( )
fun
( )
let funThatNeedsListAndFunc list func =
func list
let func = List.sum
let list = Seq.toList { 0 .. 5}
let result1 = funThatNeedsListAndFunc list func
printfn "result1: %A" result1
let result2 = funThatNeedsListAndFunc list <| func
printfn "result2: %A" result2
let result3 = funThatNeedsListAndFunc list <| List.sum
printfn "result3: %A" result3
let result4 = funThatNeedsListAndFunc list <|
fun (list : 'a list) -> List.sum list
printfn "result4: %A" result4
let result5 = funThatNeedsListAndFunc list <|
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
printfn "result5: %A" result5
.
let result6 =
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
|> funThatNeedsListAndFunc list
printfn "result6: %A" result6
.
let result7 =
funThatNeedsListAndFunc list (fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result)
printfn "result7: %A" result7