将另一个参数通过管道传递到 F# 中的行中
管道参数传入行是否仅适用于接受一个参数的函数? 如果我们看一下 Chris Smiths 页面,
// Using the Pipe-Forward operator (|>)
let photosInMB_pipeforward =
@"C:\Users\chrsmith\Pictures\"
|> filesUnderFolder
|> Seq.map fileInfo
|> Seq.map fileSize
|> Seq.fold (+) 0L
|> bytesToMB
他的 filesUnderFolder 函数只需要 rootFolder 参数, 如果函数需要两个参数怎么办,即let filesUnderFolder size rootFolder
那么这不起作用:
// Using the Pipe-Forward operator (|>)
let size= 4
let photosInMB_pipeforward =
@"C:\Users\chrsmith\Pictures\"
|> filesUnderFolder size
|> Seq.map fileInfo
|> Seq.map fileSize
|> Seq.fold (+) 0L
|> bytesToMB
因为我可以定义让内联 (>>) fgxy = g(fxy)
我想我应该能够将管道运算符与具有多个输入参数的函数一起使用,对吧?我缺少什么?
Is piping parameter into line is working only for functions that accept one parameter?
If we look at the example at Chris Smiths' page,
// Using the Pipe-Forward operator (|>)
let photosInMB_pipeforward =
@"C:\Users\chrsmith\Pictures\"
|> filesUnderFolder
|> Seq.map fileInfo
|> Seq.map fileSize
|> Seq.fold (+) 0L
|> bytesToMB
where his filesUnderFolder function was expecting only rootFolder parameter,
what if the function was expecting two parameters, i.e.let filesUnderFolder size rootFolder
Then this does not work:
// Using the Pipe-Forward operator (|>)
let size= 4
let photosInMB_pipeforward =
@"C:\Users\chrsmith\Pictures\"
|> filesUnderFolder size
|> Seq.map fileInfo
|> Seq.map fileSize
|> Seq.fold (+) 0L
|> bytesToMB
Since I can definelet inline (>>) f g x y = g(f x y)
I think I should be able to use pipeline operator with functions having multiple input parameters, right? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
混合管道运算符和柯里化参数时,请注意传递参数的顺序。
想象一下,编译器就像这样在函数及其参数两边加上括号。
@"C:\Users\chrsmith\Pictures\" |>文件夹下的文件大小
变成
@"C:\Users\chrsmith\Pictures\" |> (文件夹下的文件大小)
或
(filesUnderFolder size) @"C:\Users\chrsmith\Pictures\"
乱序示例
具有三个参数的
定义
When mixing pipeline operators and curried arguments be aware of the order you pass arguments with.
Think about it as if the compiler is putting parentheses around the function and its parameters like this.
@"C:\Users\chrsmith\Pictures\" |> filesUnderFolder size
becomes
@"C:\Users\chrsmith\Pictures\" |> (filesUnderFolder size)
or
(filesUnderFolder size) @"C:\Users\chrsmith\Pictures\"
Out of order example
With three arguments
Definitions
您建议的示例应该可以正常工作,
如果
filesUnderFolder
接受两个柯里化参数,并且您将其部分应用于一个参数,则可以在另一个参数的管道中使用它。(另请注意鲜为人知的管道运算符
||>
,它采用 2 元组并将它们按顺序输入到后面的内容中。)
The example you suggested should work fine, a la
If
filesUnderFolder
takes two curried args, and you partially apply it to one arg, you can use it in the pipeline for the other.(Note also the lesser known pipeline operator
||>
which takes a 2-tuple and feed them sequentially into what follows.)
这可能是不好的风格(?),但您可以“从右侧”向管道添加其他参数
It may be bad style (?), but you can add additional parameters to the pipeline 'from the right side'