Bug ? Seq.take 10 works well , Seq.take 100 doesn't work
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)
first line works well but second line gives error : The input sequence has an insufficient number of elements.
Yes , there is no 100 elements , they are just 3 but why 10 works then ?
Onlin开发者_如何学Pythone test
after all it works on 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
It's printing out 3 elements and then printing out the error message.
Other answers have explained your mistake (and I recommend trying not to jump to conclusions about compiler bugs, you'll probably be downvoted). Also, you're comparing Seq.take with Enumerable.Take, but they don't have the same behavior. However, Seq.truncate does have the same behavior as Enumerable.Take
in your sample second for loop is not executed at all. first one outputs 1 2 3 and then throws exception
精彩评论