怎么采取不超过常数?(How to take no more than a constant?)

我想学习F#。

我想不再使用Seq(或数组)中的元素而不是常量。

我用这个代码: [ "11"; "12"; "13" ] |> Seq.take 2 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.take 2 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.take 2 |> Seq.toList |> Seq.iter (printf "%A ")我得到"11" "12"

如果我试试[ "11"; "12"; "13" ] |> Seq.take 4 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.take 4 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.take 4 |> Seq.toList |> Seq.iter (printf "%A ")我得到一个类似于System.InvalidOperationException: The input sequence has an insufficient number of elements.的异常System.InvalidOperationException: The input sequence has an insufficient number of elements.

我可以想起像[ "11"; "12"; "13" ] |> Seq.takeWhile (fun elem -> true) |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.takeWhile (fun elem -> true) |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.takeWhile (fun elem -> true) |> Seq.toList |> Seq.iter (printf "%A ")但我不知道如何停止达到一些常数限制。

所以我需要像[ "11"; "12"; "13" ] |> Seq.takeNoMoreThan 4 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.takeNoMoreThan 4 |> Seq.toList |> Seq.iter (printf "%A ") [ "11"; "12"; "13" ] |> Seq.takeNoMoreThan 4 |> Seq.toList |> Seq.iter (printf "%A ") 。

我不知道如何实现我的目标。

Im trying to learn F#.

I want to take no more elements from Seq (or array) than a constant.

I use this code: [ "11"; "12"; "13" ] |> Seq.take 2 |> Seq.toList |> Seq.iter (printf "%A ") I get "11" "12"

If I try [ "11"; "12"; "13" ] |> Seq.take 4 |> Seq.toList |> Seq.iter (printf "%A ") I get an exception like System.InvalidOperationException: The input sequence has an insufficient number of elements.

I can think of takeWhile as in [ "11"; "12"; "13" ] |> Seq.takeWhile (fun elem -> true) |> Seq.toList |> Seq.iter (printf "%A ") But I have no idea how to stop taking on reaching some constant limit.

So I need something like [ "11"; "12"; "13" ] |> Seq.takeNoMoreThan 4 |> Seq.toList |> Seq.iter (printf "%A ").

I have no idea how to achieve my goal.

最满意答案

您应该使用Seq.truncate 。

You should use Seq.truncate instead.

更多推荐