Skip to content

Improve performance with lazy operations #19

@jkalias

Description

@jkalias

Consider the following example from the documentation

const auto employees_below_40 = ages
    .zip(names)
    .map<person>([](const auto& pair) {                     
        return person(pair.first, pair.second);
    })
    .filter([](const auto& person) {
        return person.age < 40;
    })
    .sort([](const auto& person1, const auto& person2) {
        return person1.age < person2.age;
    });

This snippet will iterate the corresponding instance of functional_vector 4 times, once for every call (zip, map, filter and sort). If ages would have been a significantly large vector, then the current implementation is really inefficient.

In such cases it would be really useful if we had lazy variants of these algorithms, such that for the final result only one iteration would be performed.

For example, a lazy zip could look like this

const auto employees_below_40 = ages
    .lazy_zip(names) // lazy on the top-level call, which propagates down the chain
    .map<person>([](const auto& pair) {                     
        return person(pair.first, pair.second);
    })
    .filter([](const auto& person) {
        return person.age < 40;
    })
    .sort([](const auto& person1, const auto& person2) {
        return person1.age < person2.age;
    })
    .get();

or applying an algorithm to a lazy vector could look like this

const auto employees_below_40 = ages
    .zip(names) // lazy on the top-level call, which propagates down the chain
    .map<person>([](const auto& pair) {                     
        return person(pair.first, pair.second);
    })
    .lazy() // from here on all calls are lazy
    .filter([](const auto& person) {
        return person.age < 40;
    })
    .sort([](const auto& person1, const auto& person2) {
        return person1.age < person2.age;
    })
    .get();

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions