为什么数据参数排在最后
Why data parameter comes last
为什么F#中的data参数排在最后,如下代码片段所示:
let startsWith lookFor (s:string) = s.StartsWith(lookFor)
let str1 =
"hello"
|> startsWith "h"
我认为你的部分答案在你的问题中。 |>
(正向管道)运算符允许您在调用函数之前指定函数的最后一个参数。如果参数的顺序相反,那将不起作用。最好的例子就是对列表进行操作的函数链。每个函数都将一个列表作为其最后一个参数,returns 一个可以传递给下一个函数的列表。
来自http://www.tryfsharp.org/Learn/getting-started#chaining-functions:
[0..100]
|> List.filter (fun x -> x % 2 = 0)
|> List.map (fun x -> x * 2)
|> List.sum
The |>
operator allows you to reorder your code by specifying the last
argument of a function before you call it. This example is
functionally equivalent to the previous code, but it reads much more
cleanly. First, it creates a list of numbers. Then, it pipes that list
of numbers to filter out the odds. Next, it pipes that result to
List.map
to double it. Finally, it pipes the doubled numbers to
List.sum
to add them together. The Forward Pipe Operator reorganizes
the function chain so that your code reads the way you think about the
problem instead of forcing you to think inside out.
正如评论中提到的,也有柯里化的概念,但我认为这不像链接函数那样容易掌握。
为什么F#中的data参数排在最后,如下代码片段所示:
let startsWith lookFor (s:string) = s.StartsWith(lookFor)
let str1 =
"hello"
|> startsWith "h"
我认为你的部分答案在你的问题中。 |>
(正向管道)运算符允许您在调用函数之前指定函数的最后一个参数。如果参数的顺序相反,那将不起作用。最好的例子就是对列表进行操作的函数链。每个函数都将一个列表作为其最后一个参数,returns 一个可以传递给下一个函数的列表。
来自http://www.tryfsharp.org/Learn/getting-started#chaining-functions:
[0..100] |> List.filter (fun x -> x % 2 = 0) |> List.map (fun x -> x * 2) |> List.sum
The
|>
operator allows you to reorder your code by specifying the last argument of a function before you call it. This example is functionally equivalent to the previous code, but it reads much more cleanly. First, it creates a list of numbers. Then, it pipes that list of numbers to filter out the odds. Next, it pipes that result toList.map
to double it. Finally, it pipes the doubled numbers toList.sum
to add them together. The Forward Pipe Operator reorganizes the function chain so that your code reads the way you think about the problem instead of forcing you to think inside out.
正如评论中提到的,也有柯里化的概念,但我认为这不像链接函数那样容易掌握。