Questions tagged [accumulator]

An accumulator is a register in a computer processor in which intermediate arithmetic and logic results are stored.

253 questions
18
votes
2 answers

Spark losing println() on stdout

I have the following code: val blueCount = sc.accumulator[Long](0) val output = input.map { data => for (value <- data.getValues()) { if (record.getEnum() == DataEnum.BLUE) { blueCount += 1 println("Enum = BLUE : " +…
Edamame
  • 17,408
  • 44
  • 143
  • 254
17
votes
2 answers

Is the accumulator of reduce in Java 8 allowed to modify its arguments?

In Java 8, Stream has a method reduce: T reduce(T identity, BinaryOperator accumulator); Is the accumulator operator allowed to modify either of its arguments? I presume not since the JavaDoc says the accumulator should be NonInterfering,…
Graeme Moss
  • 6,955
  • 4
  • 25
  • 39
16
votes
6 answers

What is the 'accumulator' in HQ9+?

I was just reading a bit about the HQ9+ programming language: https://esolangs.org/wiki/HQ9+, https://en.wikipedia.org/wiki/HQ9+, and https://cliffle.com/esoterica/hq9plus, and it tells me something about a so-called “accumulator” which can be…
user142019
16
votes
7 answers

Is there a MATLAB accumarray equivalent in numpy?

I'm looking for a fast solution to MATLAB's accumarray in numpy. The accumarray accumulates the elements of an array which belong to the same index. An example: a = np.arange(1,11) # array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) accmap =…
petrichor
  • 6,249
  • 4
  • 33
  • 48
14
votes
5 answers

Should I avoid tail recursion in Prolog and in general?

I'm working through "Learn Prolog now" online book for fun. I'm trying to write a predicate that goes through each member of a list and adds one to it, using accumulators. I have already done it easily without tail…
14
votes
1 answer

Irrefutable pattern does not leak memory in recursion, but why?

The mapAndSum function in the code block all the way below combines map and sum (never mind that another sum is applied in the main function, it just serves to make the output compact). The map is computed lazily, while the sum is computed using an…
buechse
  • 163
  • 6
13
votes
3 answers

Is it possible to use boost accumulators with vectors?

I wanted to use boost accumulators to calculate statistics of a variable that is a vector. Is there a simple way to do this. I think it's not possible to use the dumbest thing: using namespace boost::accumulators; //stuff... …
Rafael S. Calsaverini
  • 12,352
  • 16
  • 69
  • 126
13
votes
5 answers

Accumulators, conj and recursion

I've solved 45 problems from 4clojure.com and I noticed a recurring problem in the way I try to solve some problems using recursion and accumulators. I'll try to explain the best I can what I'm doing to end up with fugly solutions hoping that some…
Cedric Martin
  • 5,798
  • 4
  • 26
  • 61
12
votes
3 answers

SQLite: accumulator (sum) column in a SELECT statement

I have a table like this one: SELECT value FROM table; value 1 3 13 1 5 I would like to add an accumulator column, so that I have this result: value accumulated 1 1 3 4 13 17 1 18 5 23 How can I do this? What's the real…
moala
  • 4,580
  • 6
  • 41
  • 64
12
votes
3 answers

Prolog Accumulators. Are they really a "different" concept?

I am learning Prolog under my Artificial Intelligence Lab, from the source Learn Prolog Now!. In the 5th Chapter we come to learn about Accumulators. And as an example, these two code snippets are given. To Find the Length of a List without…
tmj
  • 1,642
  • 2
  • 19
  • 34
11
votes
6 answers

In (reduce f val coll), is the val an accumulator?

When you call reduce and pass it a function and two arguments, can the first argument be considered to be an accumulator? Is it always an accumulator? Is it sometimes an accumulator? I was reading a blog entry about using Clojure to parse big files…
Cedric Martin
  • 5,798
  • 4
  • 26
  • 61
10
votes
3 answers

Scheme / Racket Best Practice - Recursion vs Variable Accumulation

I'm new to Scheme (via Racket) and (to a lesser extent) functional programming, and could use some advise on the pros and cons of accumulation via variables vs recursion. For the purposes of this example, I'm trying to calculate a moving average. …
Scott Klarenbach
  • 31,139
  • 14
  • 58
  • 87
10
votes
3 answers

Python 3.2 - readline() is skipping lines in source file

I have a .txt file that I created with multiple lines. when I run a for loop, with a count accumulator, it skips lines. It skips the top line, and starts with the second, prints the fourth, the sixth, etc... What is it I'm missing? ** for your…
Alli OGrady
  • 285
  • 2
  • 4
  • 13
8
votes
2 answers

Recursive state monad for accumulating a value while building a list?

I'm totally new to Haskell so apologies if the question is silly. What I want to do is recursively build a list while at the same time building up an accumulated value based on the recursive calls. This is for a problem I'm doing for a Coursera…
Russell
  • 11,742
  • 3
  • 47
  • 72
7
votes
4 answers

Incrementing Variable in a foldLeft

I have Scala code like this var i = 1 for(e <- array) { acc += e * i i += 1 } I need to multiply the first element in the array by 1, the next by 2, the next by 3 and so on adding it all into an accumulator. I feel that there is a better…
chrissphinx
  • 364
  • 3
  • 13
1
2 3
16 17