3

So essentially I have this variable question[1] where question[1] is: [[1, 0, 0], [1, 0, 0], [0,1,0] ...]
I want to be able to add them vertically so I get one array like so

[1,0,0]+[1,0,0]=[2,0,0] + [0,1,0] = [2,1,0] + ....

Additionally, the arrays might be longer or shorter (but will be at least two long)

How could I do this?
The API Doc has the following example:

 sequence1 = [100, 200, 300, 400]  
 sequence2 = [10, 20, 30, 40]  
 sequence3 = [1, 2, 3, 4]  
 r.map(sequence1, sequence2, sequence3,  
 lambda val1, val2, val3: (val1 + val2 + val3)).run(conn)

with result: [111, 222, 333, 444]

But this won't account for a variable amount of inputs as I want. Answer in python please!

Brady Auen
  • 215
  • 3
  • 13
  • I'm trying this `question[1].fold([], lambda acc, entry: acc + entry ).run(self.conn)` But still no luck, getting a strange error that 'Bracket' has no attribute fold, why? – Brady Auen Apr 14 '16 at 05:09

1 Answers1

2

From @mglukov

r.expr([[100, 200, 300, 400],[10, 20, 30, 40],[1, 2, 3, 4]]).reduce((left,right) => {
return left.map(right, (leftVal, rightVal) => { return leftVal.add(rightVal); });
})

Good question!

  • Hi Neil! Thanks for trying to help me out last night. ```r.expr(question[1]).reduce(lambda left, right: left.map(right, lambda leftVal, rightVal: leftVal + rightVal))``` is the python translation if anyone's interested! – Brady Auen Apr 14 '16 at 14:36