Back to top

ReQL command: concatMap

Command syntax

stream.concatMap(function) → stream

array.concatMap(function) → array

Description

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

concatMap 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(r.array(1, 2, 3)).map(x -> r.array(x, x.mul(2))).run(conn);

Result:

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

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

r.expr(r.array(1, 2, 3)).concatMap(x -> r.array(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").concatMap(hero -> hero.g("defeatedMonsters")).run(conn);

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

r.table("posts").concatMap(
    post -> r.table("comments").getAll(post.g("id")).optArg("index", "post_id")
             .map(comment -> r.hashMap("left", post).with("right", comment))
).run(conn);

Get more help

Couldn't find what you were looking for?