管道中的 printfn
所以我有一个函数 SolveEquasion 返回一对 float*float[]。打印数字和数组并继续使用数组的最佳方法是什么?我编写了以下代码,但似乎有更好的方法
<前><代码>... |>求解方程 |> (fun (det, 解) -> printfn "行列式 = %f\n解 = %A" det (Array.toList 解), 解 ) |>斯德
So I have a function SolveEquasion that returns a pair float*float[]. What is the best way to print the number and the array and continue working with the array? I made the following code but it seems there is a better way
... |> SolveEquasion |> (fun (det, solution) -> printfn "Determinant = %f\nSolution = %A" det (Array.toList solution), solution ) |> snd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想在管道中执行此操作,我认为您的解决方案无法改进。另一种方法是使用
let
绑定,同时拆分管道操作,以避免出现类似于map
和iter< 的爱子的函数。 /代码>。
I don't think your solution can improved if you want to do this in a pipeline. Another approach is to use a
let
binding, along with splitting up the pipelined operations, to avoid having a function that acts like the love child ofmap
anditer
.我认为最初的解决方案很好,我们可以通过为您的匿名函数提供我在其他一些基于管道化高阶函数的库中看到的名称来提高其清晰度:tap。
I think the original solution is fine, and we can improve its clarity by giving your anonymous function the name I've seen it given in some other libraries based around pipelining higher-order functions: tap.
好吧,一方面,您可以通过返回单个值而不是前一个函数的元组来跳过 snd 的使用:
Well, for one thing you can skip the use of
snd
by returning a single value rather than a tuple from the previous function:我可能会使用 Daniel 的方法,并使用
let
将要打印的值分配给符号。或者,您可以定义printf
的变体,它接受一些参数并返回其中一个。我不确定是否有一个通用的方案应该如何完成 - 对于您的示例,它将需要一个二元素元组:然后您可以编写:
I'd probably use Daniel's approach and just assign the value you want to print to a symbol using
let
. Alternatively, you could define a variant ofprintf
that takes some arguments and returns one of them. I'm not sure if there is a general scheme how this should be done - for your example it would take a two-element tuple:Then you can write: