ReQL command: concat_map

Command syntax

stream.concat_map(function) → stream
array.concat_map(function) → array

Description

Concatenate one or more elements into a single sequence using a mapping function.

concat_map works in a similar fashion to map, applying the given function to each element in a sequence, but it will always return a single sequence. If the mapping function returns a sequence, map would produce a sequence of sequences:

r.expr([1, 2, 3]).map { |x| [x, x.mul(2)] }.run(conn)

Result:

[[1, 2], [2, 4], [3, 6]]

Whereas concat_map with the same mapping function would merge those sequences into one:

r.expr([1, 2, 3]).concat_map { |x| [x, x.mul(2)] }.run(conn)

Result:

[1, 2, 2, 4, 3, 6]

The return value, array or stream, will be the same type as the input.

Example: Construct a sequence of all monsters defeated by Marvel heroes. The field “defeatedMonsters” is an array of one or more monster names.

r.table('marvel').concat_map { |hero|
    hero[:defeated_monsters]
}.run(conn)

Example: Simulate an eq_join using concat_map. (This is how ReQL joins are implemented internally.)

r.table('posts').concat_map { |post|
    r.table('comments').get_all(
        post['id'], :index => 'post_id'
    ).map { |comment| { :left => post, :right => comment} }
}.run(conn)

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/ruby/concat_map/