Error? Seq.take 10 is working well, Seq.take 100 is not working

let a = [1;2;3;] for i in (a |> Seq.take 10) do Console.WriteLine(i) for i in (a |> Seq.take 100) do Console.WriteLine(i) 

The first line works well, but the second line gives an error: The input sequence has an insufficient number of elements.

Yes, there are no 100 elements, there are only 3 of them, but why does 10 work?

Online test

because it works in C #

 using System; using System.Linq; class P { static void Main() { var p = new[] {1,2,3,4}; foreach(var i in p.Take(10).ToArray()) Console.WriteLine(i); foreach(var i in p.Take(2).ToArray()) Console.WriteLine(i); foreach(var i in p.Take(100).ToArray()) Console.WriteLine(i); }} 

Online test

+4
source share
3 answers

It prints 3 items and then displays an error message.

+9
source

Other answers explained your mistake (and I recommend trying not to draw conclusions about compiler errors , you will probably be underestimated). In addition, you compare Seq.take with Enumerable.Take, but they do not have the same behavior . However, Seq.truncate has the same behavior as Enumerable.Take

+6
source

in your example, the second loop of the loop does not execute at all. the first throws 1 2 3 and then throws an exception

+5
source

Source: https://habr.com/ru/post/1346556/


All Articles