Questions tagged [accumulate]

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

When we are interested only in the final result, it is equivalent to left folding; and if we keep the whole sequence of results progressively calculated, it is known as left scan.

319 questions
8
votes
2 answers

Java8 stream.reduce() with 3 parameters - getting transparency

I wrote this code to reduce a list of words to a long count of how many words start with an 'A'. I'm just writing it to learn Java 8, so I'd like to understand it a little better [Disclaimer: I realize this is probably not the best way to write…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
8
votes
4 answers

Efficient accumulate

Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){…
tach
  • 663
  • 12
  • 19
7
votes
2 answers

Why can't I use istream_view and std::accumulate to sum up my input?

This program: #include #include #include int main() { auto rng = std::ranges::istream_view(std::cin); std::cout << std::accumulate(std::ranges::begin(rng), std::ranges::end(rng), 0); } is supposed to sum…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
4 answers

Folding in Haskell, using more than one function

I have a list of integers based on letters. For example: let charlist = map (ord) "ABCDEF" charlist would then look like the following: [65,66,67,68,69,70] I also have a list of three functions: (+), (-) and (*). The list in this example looks…
Saser
  • 137
  • 5
6
votes
1 answer

How to vectorize a cumulative operation in Pandas

Based on the answer to How to vectorize an operation that uses previous values?, I am not able to answer the following question I have: Is there a way to vectorize the Value End Of Period (VEoP) column? import pandas as pd terms =…
6
votes
2 answers

Using accumulate function with second to last value as .init argument

I have recently come across an interesting question of calculating a vector values using its penultimate value as .init argument plus an additional vector's current value. Here is the sample data set: set.seed(13) dt <- data.frame(id =…
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
6
votes
1 answer

How does this implementation of `init` work?

I'm learning Haskell and I found an interesting implementation of init using foldr. However, I have difficulty understanding how it works. init' xs = foldr f (const []) xs id where f x g h = h $ g (x:) Consider I have an input of [1,2,3], then…
6
votes
1 answer

std::accumulate C++20 version

I'm trying to understand this code but I can't figure out why this version for (; first != last; ++first) init = std::move(init) + *first; is faster than this for (; first != last; ++first) init += *first; I did take them from…
Sam
  • 263
  • 2
  • 12
6
votes
1 answer

How to fold/accumulate a numpy matrix product (dot)?

With using python library numpy, it is possible to use the function cumprod to evaluate cumulative products, e.g. a = np.array([1,2,3,4,2]) np.cumprod(a) gives array([ 1, 2, 6, 24, 48]) It is indeed possible to apply this function only along…
Georg Sievelson
  • 300
  • 3
  • 7
6
votes
3 answers

Sum of std::vector members C++
I have Example Class: class Example { private: int testValue1; int testValue2; int testValue3; public: Example(int pVal1, int pVal2, int pVal3); Example(const Example); const Example operator =(const Example); inline int…
kovko
  • 123
  • 1
  • 8
6
votes
2 answers

computing column sums of matrix vector > with iterators?

In a previous post column vector with row means -- with std::accumulate? I asked if it was possible, using STL functionality, to compute row means of a matrix vector< vector > data ( rows, vector ( columns ) ); The top answer by…
alle_meije
  • 2,424
  • 1
  • 19
  • 40
6
votes
4 answers

Compute the sum of part of the vector using std:: accumulate

Having this vector vector v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; how can I compute the sum of the first half of it (which is 15) using the accumulate function? Can I do this using the for loop with only the iterators (not numerical indexes)?
Slazer
  • 4,750
  • 7
  • 33
  • 60
5
votes
3 answers

using lag results within the same mutate function dplyr

I want to replicate the below formula R using dplyr + lag function. The code works till the 2nd row of each group and then onward gives me 0s forecast = lag(value,1)*(1-lag(Attrition)/52) Conditions: the first value for forecast should be empty as…
5
votes
3 answers

Grouping Ids based on at least one common values

I have a list whose elements are integers and I would like to accumulate these elements if only they share at least one value. With regard to those elements that don't share any values with the rest I would like them to stay as they are. Here is my…
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
5
votes
3 answers

R: pass multiple arguments to accumulate/reduce

This is related to R: use the newly generated data in the previous row I realized the actual problem I was faced with is a bit more complicated than the example I gave in the thread above - it seems I have to pass 3 arguments to the recursive…
dianaiii
  • 89
  • 4
1
2
3
21 22