管道中的 printfn

发布于 2024-10-19 21:10:05 字数 239 浏览 6 评论 0原文

所以我有一个函数 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 技术交流群。

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

发布评论

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

评论(4

愛上了 2024-10-26 21:10:05

如果您想在管道中执行此操作,我认为您的解决方案无法改进。另一种方法是使用 let 绑定,同时拆分管道操作,以避免出现类似于 mapiter< 的爱子的函数。 /代码>。

let (det, solution) = SolveEquasion
printfn "Determinant = %f\nSolution = %A" det (Array.toList solution)
//do something else with solution

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 of map and iter.

let (det, solution) = SolveEquasion
printfn "Determinant = %f\nSolution = %A" det (Array.toList solution)
//do something else with solution
眼趣 2024-10-26 21:10:05

我认为最初的解决方案很好,我们可以通过为您的匿名函数提供我在其他一些基于管道化高阶函数的库中看到的名称来提高其清晰度:tap

let tap f x =
    f x
    x

(1.0, [| 2.0; 3.0 |])
|> tap (fun (s, a) -> printfn "%A %A" s a)
|> snd

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.

let tap f x =
    f x
    x

(1.0, [| 2.0; 3.0 |])
|> tap (fun (s, a) -> printfn "%A %A" s a)
|> snd
素食主义者 2024-10-26 21:10:05

好吧,一方面,您可以通过返回单个值而不是前一个函数的元组来跳过 snd 的使用:

...
|> SolveEquasion
|> (fun (det, solution) -> 
        printfn "Determinant = %f\nSolution = %A" det (Array.toList solution) 
        solution )

Well, for one thing you can skip the use of snd by returning a single value rather than a tuple from the previous function:

...
|> SolveEquasion
|> (fun (det, solution) -> 
        printfn "Determinant = %f\nSolution = %A" det (Array.toList solution) 
        solution )
那些过往 2024-10-26 21:10:05

我可能会使用 Daniel 的方法,并使用 let 将要打印的值分配给符号。或者,您可以定义 printf 的变体,它接受一些参数并返回其中一个。我不确定是否有一个通用的方案应该如何完成 - 对于您的示例,它将需要一个二元素元组:

let mprintf fmt (a, b) = 
  Printf.kprintf (fun s -> printf "%s" s; (a, b)) fmt a b

然后您可以编写:

...  
|> SolveEquasion  
|> mprintfn "Determinant = %f\nSolution = %A"
|> snd |> // ... more stuff with solution

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 of printf 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:

let mprintf fmt (a, b) = 
  Printf.kprintf (fun s -> printf "%s" s; (a, b)) fmt a b

Then you can write:

...  
|> SolveEquasion  
|> mprintfn "Determinant = %f\nSolution = %A"
|> snd |> // ... more stuff with solution
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文