| layout | api-command | ||||
|---|---|---|---|---|---|
| language | Python | ||||
| permalink | api/python/concat_map/ | ||||
| command | concat_map | ||||
| related_commands |
|
{% apibody %} stream.concat_map(function) → stream array.concat_map(function) → array {% endapibody %}
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(lambda 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(lambda 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(lambda hero: hero['defeatedMonsters']).run(conn)Example: Simulate an eq_join using concat_map. (This is how ReQL joins are implemented internally.)
r.table('posts').concat_map(
lambda post: r.table('comments').get_all(
post['id'], index='post_id'
).map(
lambda comment: { 'left': post, 'right': comment}
)
).run(conn)