gpt4 book ai didi

arrays - 在 bool[,] 中获取 true 的计数 F#

转载 作者:行者123 更新时间:2023-12-01 07:26:14 26 4
gpt4 key购买 nike

我在 F# 中有一个 bool[,] 并且我想获得 true 的存在计数。在不陷入命令式编程的情况下,我该如何做到这一点?

这是我当前的解决方案,它实际上只是用 f# 编写的 c#。

let mutable x = 0
for cell in cells do
if cell = true then x <- x + 1
x

最佳答案

这是一种方法:

let x = cells |> Seq.cast |> Seq.filter id |> Seq.length

它的作用是过滤掉 false值(见 Seq.filter ),然后只计算剩下的。

关于arrays - 在 bool[,] 中获取 true 的计数 F#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com