0

我有类型的 Tuple 的 seq,(x: int, y:bool)我想找到if为 truex的项目y=isMin 的最小值。valListisMin

let valList = seq{ for i =0 to 8 do yield (GetVal (i,not isMin),not isMin) } 
let onlyMinType (x: int, y:bool) = if y==isMin then x
let maxVal = valList |> Seq.collect(onlyMinType) |> if isMin then Seq.min else Seq.max
maxVal

我不知道在这里做什么 Seq.collect(onlyMinType)和功能onlyMinType

4

2 回答 2

1

I have seq of Tuples of type (x: int, y:bool) and I want to find the min x of the items who has y=isMin in valList ifisMinistrue`.

The idea is to use Seq.filter snd to filter values from seq which second value is true. Then use Seq.sortBy fst to sort seq basing on the first value of each tuple.

> (Seq.sortBy fst << Seq.filter snd) [(1, false); (2, true); (0, true)];;
val it : seq<int * bool> = seq [(0, true); (2, true)]
于 2013-10-04T09:57:07.933 回答
1

您可以通过Seq.minBy几种不同的方式使用:

[(1, false); (2, true); (0, true); (-1, false)]
|> Seq.filter snd
|> Seq.minBy fst
|> fst

或者

[(1, false); (2, true); (0, true); (-1, false)]
|> Seq.minBy (fun (n, b) -> if b then n else Int32.MaxValue)
|> fst

会很好地工作。

于 2013-10-04T21:30:01.087 回答