将另一个参数通过管道传递到 F# 中的行中

发布于 2024-08-22 10:49:59 字数 956 浏览 8 评论 0原文

管道参数传入行是否仅适用于接受一个参数的函数? 如果我们看一下 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 define
let 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铜锣湾横着走 2024-08-29 10:49:59

混合管道运算符和柯里化参数时,请注意传递参数的顺序。

let size = 4
let photosInMB_pipeforward =
    size, @"C:\Users\chrsmith\Pictures\"
    ||> filesUnderFolder
    |> Seq.map fileInfo
    |> Seq.map fileSize
    |> Seq.fold (+) 0L 
    |> bytesToMB

想象一下,编译器就像这样在函数及其参数两边加上括号。

@"C:\Users\chrsmith\Pictures\" |>文件夹下的文件大小
变成
@"C:\Users\chrsmith\Pictures\" |> (文件夹下的文件大小)

(filesUnderFolder size) @"C:\Users\chrsmith\Pictures\"

乱序示例

let print2 x y = printfn "%A - %A" x y;;

(1, 2) ||> print2;;
1 - 2

1 |> print2 2;;
2 - 1

具有三个参数的

let print3 x y z = printfn "%A - %A - %A" x y z;;

(1, 2, 3) |||> print3;;
1 - 2 - 3

(2, 3) ||> print3 1;;
1 - 2 - 3

3 |> print3 1 2;;
1 - 2 - 3

定义

let inline (|>) x f = f x

let inline (||>) (x1,x2) f = f x1 x2

let inline (|||>) (x1,x2,x3) f = f x1 x2 x3

When mixing pipeline operators and curried arguments be aware of the order you pass arguments with.

let size = 4
let photosInMB_pipeforward =
    size, @"C:\Users\chrsmith\Pictures\"
    ||> filesUnderFolder
    |> Seq.map fileInfo
    |> Seq.map fileSize
    |> Seq.fold (+) 0L 
    |> bytesToMB

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

let print2 x y = printfn "%A - %A" x y;;

(1, 2) ||> print2;;
1 - 2

1 |> print2 2;;
2 - 1

With three arguments

let print3 x y z = printfn "%A - %A - %A" x y z;;

(1, 2, 3) |||> print3;;
1 - 2 - 3

(2, 3) ||> print3 1;;
1 - 2 - 3

3 |> print3 1 2;;
1 - 2 - 3

Definitions

let inline (|>) x f = f x

let inline (||>) (x1,x2) f = f x1 x2

let inline (|||>) (x1,x2,x3) f = f x1 x2 x3
撩心不撩汉 2024-08-29 10:49:59

您建议的示例应该可以正常工作,

let add x y = x + y

41
|> add 1
|> printfn "%d"

如果 filesUnderFolder 接受两个柯里化参数,并且您将其部分应用于一个参数,则可以在另一个参数的管道中使用它。

(另请注意鲜为人知的管道运算符 ||>

(41,1)
||> add
|> printfn "%d"

它采用 2 元组并将它们按顺序输入到后面的内容中。)

The example you suggested should work fine, a la

let add x y = x + y

41
|> add 1
|> printfn "%d"

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 ||>

(41,1)
||> add
|> printfn "%d"

which takes a 2-tuple and feed them sequentially into what follows.)

傲性难收 2024-08-29 10:49:59

这可能是不好的风格(?),但您可以“从右侧”向管道添加其他参数

let h x y z = x + y - z

let sub x y = x - y

let sqr x = x * x

3 |> h <| 2 <| 7
  |> sub <| 23
  |> sqr

// is the same as
sqr (sub (h 3 2 7) 23)

It may be bad style (?), but you can add additional parameters to the pipeline 'from the right side'

let h x y z = x + y - z

let sub x y = x - y

let sqr x = x * x

3 |> h <| 2 <| 7
  |> sub <| 23
  |> sqr

// is the same as
sqr (sub (h 3 2 7) 23)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文