take

Implementing take using foldr

戏子无情 提交于 2019-11-27 07:55:15
问题 This is my take version using foldr : myTake n list = foldr step [] list where step x y | (length y) < n = x : y | otherwise = y main = do print $ myTake 2 [1,2,3,4] The output is not what I expect: [3,4] I then tried to debug by inserting the length of y into itself and the result was: [3,2,1,0] I don't understand why the lengths are inserted in decreasing order. Perhaps something obvious I missed? 回答1: If you want to implement take using foldr you need to simulate traversing the list from

LINQ with Skip and Take

大城市里の小女人 提交于 2019-11-27 03:51:14
问题 I used the below code to take some items from IEnumerable , but it is always returning the source as null and count as 0 and actually there are items exists in IEnumerable private void GetItemsPrice(IEnumerable<Item> items, int customerNumber) { var a = items.Skip(2).Take(5); } When i try to access a it has count 0 . Anything goes wrong here? 回答1: Remember, that variable a in your code is a query itself . It is not result of query execution . When you are using Immediate Window to watch query

LINQ Partition List into Lists of 8 members [duplicate]

醉酒当歌 提交于 2019-11-26 13:29:44
This question already has an answer here: Split List into Sublists with LINQ 27 answers How would one take a List (using LINQ) and break it into a List of Lists partitioning the original list on every 8th entry? I imagine something like this would involve Skip and/or Take, but I'm still pretty new to LINQ. Edit: Using C# / .Net 3.5 Edit2: This question is phrased differently than the other "duplicate" question. Although the problems are similar, the answers in this question are superior: Both the "accepted" answer is very solid (with the yield statement) as well as Jon Skeet's suggestion to