0

I have a sequence inside a document

{ errorTicker: [1,2,3,4,5,6,7,8,9] }

I would like to do a group by N elements and get

for N = 3 { errorTicker: [[1,2,3],[4,5,6],[7,8,9]] }

What would have been ideal solution:

r.expr([1,2,3,4,5,6,7,8,9]).group(function(entry, index){
    return r.expr(index).div(3).coerceTo('string').split('.')(0);
})

but group does not accept current index, only the entry being processed...

What magic could I use here to achieve the desired result?

P.S. order of the sequence is important to me i.e. group [1,2,3] is not the same as [1,3,5]

ekad
  • 14,436
  • 26
  • 44
  • 46
let4be
  • 1,048
  • 11
  • 30

1 Answers1

1

I'm almost on the right track, thanks to .range() command

Here is the intermediate version i'm working on right now

r.expr({tickers: [9,6,3,9,6,7,8,1,2]})
  .do(function (e){
    return r.range(e('tickers').count())
      .map(function(index){
        return [index, e('tickers')(index)]
      })
      .group(function(indexValue){
        return r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0);
      })
    })

The final version that seems to do what I need:

r.expr({tickers: [9,6,3,9,6,7,8,1,2]})
  .do(function (e){
    return r.range(e('tickers').count())
      .map(function(index){
        return [index, e('tickers')(index)]
      })
      .group(function(indexValue){
        return r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0);
      })
      .ungroup()
      .map(function(e){
        return e('reduction').map(function(e){
          return e(1)
        })
      })      
    })

Would appreciate any improvements ;)

updated: to get more efficience we can replace r.expr(indexValue(0)).div(3).coerceTo('string').split('.')(0)

with

r.expr(index).sub(r.expr(index).mod(3)).div(3);

P.S. rethinkdb totally rocks! it's the most powerful and intuitive NoSQL database I've ever used :)

let4be
  • 1,048
  • 11
  • 30