timtowtdi

Summing the previous values in an IEnumerable

可紊 提交于 2019-12-30 05:46:07
问题 I have a sequence of numbers: var seq = new List<int> { 1, 3, 12, 19, 33 }; and I want to transform that into a new sequence where the number is added to the preceding numbers to create a new sequence: { 1, 3, 12, 19, 33 } --> {1, 4, 16, 35, 68 } I came up with the following, but I dislike the state variable 'count'. I also dislike the fact that I'm using the values Enumerable without acting on it. int count = 1; var summed = values.Select(_ => values.Take(count++).Sum()); How else could it

Why are there so many slightly different ways to do the same thing in Ruby?

浪尽此生 提交于 2019-12-06 19:19:33
问题 I am learning Ruby. My background is C++/Java/C#. Overall, I like the language, but I am a little confused about why there are so many different ways to accomplish the same thing, each with their own slightly different semantics. Take string creation, for example. I can use '', "", q%, Q%, or just % to create strings. Some forms support interpolation. Other forms allow me to specify the string delimiters. Why are there five ways to create string literals? Why would I ever use non-interpolated

What constitutes effective Perl training for non-Perl developers? [closed]

与世无争的帅哥 提交于 2019-12-03 04:17:12
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . I've been working with Perl long enough that many of its idiosyncracies have become second nature to me. When new programmers join our group, they frequently have little to no experience with Perl, and it's usually my task to train them (to the extent necessary). I'd like to

Summing the previous values in an IEnumerable

点点圈 提交于 2019-11-30 17:24:46
I have a sequence of numbers: var seq = new List<int> { 1, 3, 12, 19, 33 }; and I want to transform that into a new sequence where the number is added to the preceding numbers to create a new sequence: { 1, 3, 12, 19, 33 } --> {1, 4, 16, 35, 68 } I came up with the following, but I dislike the state variable 'count'. I also dislike the fact that I'm using the values Enumerable without acting on it. int count = 1; var summed = values.Select(_ => values.Take(count++).Sum()); How else could it be done? This is a common pattern in functional programming which in F# is called scan . It's like C#'s