Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

set.flatMap()

Apply a provided function to each Set element and flatten the resulting Set by one level.

Loading strategy:

Signature

flatMap(mapper: (A => Set<B>)) => Set<B>

Description

Creates a Set by invoking a provided mapper function on each element of the calling Set and flattening the resulting Set one level. The Set elements are passed as a parameter to the mapper function, sequentially.

The calling Set isn’t changed.

Iterator methods

FQL provides several methods for iterating over a Set. set.forEach(), set.map(), set.flatMap() are similar but used for different purposes:

Method Primary use Notes

Perform in-place writes on Set elements.

Doesn’t return a value.

Returns a new Set.

Can’t perform writes.

Similar to set.map(), but flattens the resulting Set by one level.

Can’t perform writes.

For examples, see:

Parameters

Parameter Type Required Description

mapper

Function

Yes

Function to invoke on each element of the calling Set. Each element is passed to the mapper function as an argument. The function must return a Set.

Return value

Type Description

Set<Generic>

Set containing the result of invoking the mapper function on each element of the calling Set. The resulting Set is flattened by one level.

Examples

Basic example

// Returns a Set
let topCustomer = Customer.byEmail("alice.appleseed@example.com")

// Returns a flattened Set
topCustomer.flatMap((customer) => Order.byCustomer(customer))
{
  data: [
    {
      id: "410674590662000717",
      coll: Order,
      ts: Time("2099-10-02T22:37:39.583Z"),
      items: "hdW...",
      total: 5392,
      status: "cart",
      customer: Customer("111"),
      createdAt: Time("2099-10-02T22:37:39.434810Z"),
      payment: {}
    }
  ]
}

set.map() vs. set.flatMap()

set.flatMap() is similar to set.map(), except set.flatMap() also flattens the resulting Set by one level.

In the following example, set.map() returns a two-dimensional Set:

// Get a Set of all `Category` collection documents.
let categories = Category.all()

// Use `map()` to get a Set of `Product` documents
// for each category.
categories.map(category => {
  Product.byCategory(category)
})
// Two-dimensional Set.
{
  data: [
    {
      data: [
        {
          id: "111",
          coll: Product,
          ts: Time("2099-10-02T22:37:39.583Z"),
          name: "cups",
          description: "Translucent 9 Oz, 100 ct",
          price: 698,
          stock: 100,
          category: Category("123")
        },
        ...
      ]
    },
    {
      data: [
        {
          id: "333",
          coll: Product,
          ts: Time("2099-10-02T22:37:39.583Z"),
          name: "pizza",
          description: "Frozen Cheese",
          price: 499,
          stock: 100,
          category: Category("456")
        }
      ]
    },
    {
      data: [
        {
          id: "444",
          coll: Product,
          ts: Time("2099-10-02T22:37:39.583Z"),
          name: "avocados",
          description: "Conventional Hass, 4ct bag",
          price: 399,
          stock: 1000,
          category: Category("789")
        },
        ...
      ]
    }
  ]
}

To flatten the result to a one-dimensional array, use set.flatMap() instead:

// Get a Set of all `Category` collection documents.
let categories = Category.all()

// Use `flatMap()` to get a Set of `Product` documents
// for each category. Then flatten the resulting Set.
categories.flatMap(category => {
  Product.byCategory(category)
})
// One-dimensional Set.
{
  data: [
    {
      id: "111",
      coll: Product,
      ts: Time("2099-10-02T22:37:39.583Z"),
      name: "cups",
      description: "Translucent 9 Oz, 100 ct",
      price: 698,
      stock: 100,
      category: Category("123")
    },
    ...
    {
      id: "333",
      coll: Product,
      ts: Time("2099-10-02T22:37:39.583Z"),
      name: "pizza",
      description: "Frozen Cheese",
      price: 499,
      stock: 100,
      category: Category("456")
    },
    ...
    {
      id: "444",
      coll: Product,
      ts: Time("2099-10-02T22:37:39.583Z"),
      name: "avocados",
      description: "Conventional Hass, 4ct bag",
      price: 399,
      stock: 1000,
      category: Category("789")
    }
  ]
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!