F# 需要重写代码以不需要可变变量
F# Need to rewrite code to not require a mutable variable
我已经完成了我一直在做的项目,但我想回去清理我的代码。在这个例子中,我使用了一个可变变量,但是我希望我的代码不包含任何可变变量。我如何将此代码部分重写为 return 布尔值但它不可变?
let mutable duplicates = false
for el in (combo|>Seq.head) do
let exists = Seq.exists (fun x -> x = el) (combo|>Seq.item 1)
duplicates <- exists
任何帮助将不胜感激,干杯!
let s1 = combo |> Seq.head
let s2 = combo |> Seq.item 1
let duplicates = System.Linq.Enumerable.Intersect(s1, s2) |> Seq.isEmpty |> not
let t = Seq.item 1 combo
let duplicates = Seq.head combo |> Seq.exists (fun el -> Seq.contains el t)
关于以这种方式处理 seq
的信息适用。
我已经完成了我一直在做的项目,但我想回去清理我的代码。在这个例子中,我使用了一个可变变量,但是我希望我的代码不包含任何可变变量。我如何将此代码部分重写为 return 布尔值但它不可变?
let mutable duplicates = false
for el in (combo|>Seq.head) do
let exists = Seq.exists (fun x -> x = el) (combo|>Seq.item 1)
duplicates <- exists
任何帮助将不胜感激,干杯!
let s1 = combo |> Seq.head
let s2 = combo |> Seq.item 1
let duplicates = System.Linq.Enumerable.Intersect(s1, s2) |> Seq.isEmpty |> not
let t = Seq.item 1 combo
let duplicates = Seq.head combo |> Seq.exists (fun el -> Seq.contains el t)
seq
的信息适用。