Home

Awesome

Standard License windows macos linux codecov

🅕🅛🅤🅧

Flux is a C++20 library for sequence-orientated programming, similar in spirit to C++20 ranges, Python itertools, Rust iterators and others.

Flux offers:

A Quick Example

constexpr auto result = flux::ints()                        // 0,1,2,3,...
                         .filter(flux::pred::even)          // 0,2,4,6,...
                         .map([](int i) { return i * 2; })  // 0,4,8,12,...
                         .take(3)                           // 0,4,8
                         .sum();                            // 12

static_assert(result == 12);

Try it on Compiler Explorer!

Getting Started

Single header

Flux can be used with any C++ build system by downloading the latest automatically generated single header file and #include-ing it along with your own sources.

CMake

Flux can be used with CMake's FetchContent to download the library and keep it up to date. Add the following to your CMakeLists.txt:

include(FetchContent)

FetchContent_Declare(
    flux
    GIT_REPOSITORY https://github.com/tcbrindle/flux.git
    GIT_TAG main # Replace with a git commit id to fix a particular revision
)

FetchContent_MakeAvailable(flux)

and then add

target_link_libraries(my_target PUBLIC flux::flux)

where my_target is the name of the library or executable target that you want to build with Flux.

If you don't have an existing CMake project or just want to play around, you can find a starter project in this repository.

vcpkg

Flux is available in vcpkg and can be installed with

vcpkg install flux

See the vcpkg documentation for more details.

Compiler support

Flux requires a recent compiler with good support for the C++20 standard. It is tested with:

The Flux difference

Flux provides a broadly equivalent feature set to C++20 Ranges, but uses a slightly different iteration model based around cursors rather than iterators. Flux cursors are a generalisation of array indices, whereas STL iterators are a generalisation of array pointers.

A Flux sequence provides four basis operations:

These basis operations are equivalent to the basis operations on STL iterators (begin(), iter == end(), ++iter and *iter respectively). The crucial difference is that in the Flux model, you need to provide both the sequence and the cursor to each function call, whereas in the STL model the iterator must know how to increment and dereference itself.

STL iterators are "smart", but Flux cursors are not!

This seemingly small change has some far-reaching consequences. In particular:

Like STL input ranges, basic Flux sequences are assumed to be single-pass by default. Flux also provides various far more powerful sequences, closely modeled on their STL counterparts:

The close correspondence between Flux's sequence concepts and their ranges counterparts means that we can easily bridge the gap between the two libraries. In particular, we provide STL-compatible iterators to ensure that every Flux sequence is also a C++20 range, meaning they can be used with existing STL algorithms (and with range-for loops!) just like any other range.

Documentation

Work-in-progress reference documentation can be found at tristanbrindle.com/flux

Audio and Video

Conference Talks

ConferenceYearTitle
C++ on Sea2023Iteration Revisited: A Safer Iteration Model for C++
CppNorth2023Lightning Talk: Faster Filtering with Flux

Podcasts

PodcastEpisodeDateTitle
ADSP1252023-04-14NanoRange with Tristan Brindle
ADSP1262023-04-21Flux (and Flow) with Tristan Brindle
ADSP1272023-04-28Flux, ChatGPT & More with Tristan Brindle
CppCast3642023-07-07Sequence-Oriented Programming

Stability

Flux is still pre-1.0 and in rapid development. As such, there are no API stability guarantees at this time.

Once Flux 1.0 is released we aim to follow semantic versioning.

License

Flux is available under the Boost Software License 1.0