Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:23:44+00:00 2026-05-11T15:23:44+00:00

Given a list of objects i need to return a list consisting of the

  • 0

Given a list of objects i need to return a list consisting of the objects and the sum of a property of the objects for all objects in the list seen so far.

More generally given

var input = new int[] {1,2,3} 

I would like to have the output of

// does not compile but did not want to include extra classes. var output = { (1,1), (2,3), (3,6) };  

What is the ‘right’ functional way to do this? I can do it in a standard iterative approach of course but I am looking for how this would be done in a functional, lazy way.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:23:44+00:00Added an answer on May 11, 2026 at 3:23 pm

    in functional terms this is a combination of :

    zip

    take two sequences and create a sequence of tuples of the elements

    and

    map

    Take a function f and a sequence and return a new sequence which is f(x) for each x in the original sequence

    The zip is trivial in c# 4.0 Taking the simplistic implementation from there we have

    static class Enumerable  {      public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(         this IEnumerable<TFirst> first,          IEnumerable<TSecond> second,          Func<TFirst, TSecond, TResult> func)      {          var ie1 = first.GetEnumerator();          var ie2 = second.GetEnumerator();          while (ie1.MoveNext() && ie2.MoveNext())              yield return func(ie1.Current, ie2.Current);      }  } 

    We then need the map. We already have it, it’s what we call Select in c#

    IEnumerable<int> input = { 1,2,3,4 }; int a = 0; var accumulate = input.Select(x =>      {          a += x;           return a;     }); 

    But it is safer to bake this into it’s own method (no currying in c#) and allow support for arbitrary types/accumulations.

    static class Enumerable  {      public static IEnumerable<T> SelectAccumulate<T>(         this IEnumerable<T> seq,         Func<T,T,T> accumulator)      {          var e = seq.GetEnumerator();          T t = default(T);                      while (e.MoveNext())          {             t = accumulator(t, e.Current);             yield return t;         }      }  } 

    Then we can put them together like so

    var input = new int[] {1,2,3}; var mapsum = input.Zip(     input.SelectAccumulate((x,y) => x+y),      (a,b) => new {a,b}); 

    This will iterate over the sequence twice, but is more general. You could choose to do the accumulator yourself within a standard select and a simple closure but it is no longer so useful as a ‘building block’ which is one of the driving forces behind functional programming.

    Tuple support is a pain except within a method as the anonymous types don’t traverse method boundaries without quite a bit of hassle. A few basic tuples should be included in c# 4.0. assuming a tuple class/struct called Pair<T,U> you could do:

    public static IEnumerable<Pair<T,T>> ZipMapAccumulate<T>(     this IEnumerable<T> input,     Func<T,T,T> accumulator) {     return input.Zip(         input.SelectAccumulate((x,y) => accumulator (x,y)),          (a,b) => new Pair<T,T>(a,b)); }  //get an int specific one public static Func<IEnumerable<int>, IEnumerable<Pair<int,int>>>      ZipMapSum() {     return input => Enumerable.ZipMapAccumulate(         input,          (i,j) => i + j); } 

    Where c# linq becomes much more cumbersome than languages like f# is the poor support for operators, currying and tuples unless you keep everything inside one function and ‘reconstruct it’ each and every time for each type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a Generic List of objects that contain a member variable that is a
Given the code below, how do I compare a List of objects's values with
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
I need to build a list of all roles on an ASP.NET web site,
Given the following view : def comments(request): comments_list = Thing.objects.filter(thing_type=2) #Thing model extends MPTTModel
I am writing common functions to serialize the given object and List<object> as follows
How do I list available methods on a given object or package in Perl?
I've got a System.Generic.Collections.List(Of MyCustomClass) type object. Given integer varaibles pagesize and pagenumber, how
Given an R list, I wish to find the index of a given list
Given a list of locations such as <td>El Cerrito, CA</td> <td>Corvallis, OR</td> <td>Morganton, NC</td>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.