Generator-related operations

Lazy and compositional value processing.

Name Description
each each generates values from an input container
collect Collect input values into the container T, e.g., collect<std::vector>.
from(T n, T by=T{1}) Generate a numeric sequence starting at n
from_to(T first, T limit, T by=T{1}) Generate a half-open numeric sequence from first to before limit.
take(size_t count) Generate the first n values input.
skip(size_t n) Immediately skip the first n values from input. May empty input.
filter(const F &f) Generate values from input when f(v) returns true.
reduce(F f, T initial) Reduce/fold input given an initial value.
reduce(F f) Reduce/fold input. Input is a generator.
zip(Ts &&... vs) Combine multiple generators' output into a std::tuple.

each

<piped/util.hpp>

constexpr detail::each_ piped::each

Description

each generates values from an input container

Example:

std::vector<int> v{1,2,3};
auto g = v | each
  | map([](auto i){ return i+1; })
  | collect<std::vector>;

<T,...Ts> collect

<piped/util.hpp>

template<template< typename... > typename T, typename... Ts>
constexpr detail::collect_<T, Ts...> piped::collect

Description

Collect input values into the container T, e.g., collect<std::vector>.

Element type is determined by input. Use map_to to convert things along the way. Elements are inserted by emplace_back() by default.

auto v  = from_to(0,5)            | collect<std::vector>;   // std::vector<int>
auto v1 = from_to<size_t>(10,100) | collect<std::vector>;   // std::vector<size_t>
auto v2 = from_to(0.0f,-10.0f)    | collect<std::vector>;   // std::vector<float>

There is explicit support for building std::map if the input is a std::tuple of at least 2 values; the first will be the key, the second the value. Elements are inserted by try_emplace(). See zip for an example.

<T> from(T n, T by=T{1})

<piped/util.hpp>

template<typename T>
detail::generator_seq<T> piped::from(T n, T by=T{1})

Description

Generate a numeric sequence starting at n

T can represent.

Parameter
n T Starting value
by T Increment, defaults to T{1}

<T> from_to(T first, T limit, T by=T{1})

<piped/util.hpp>

template<typename T>
detail::generator_seq<T> piped::from_to(T first, T limit, T by=T{1})

Description

Generate a half-open numeric sequence from first to before limit.

Values proceed from first to limit, incremented by by. limit does not have to be a value in the sequence. incr may be negative.

Parameter
first T Starting value
limit T Output will stop before this
by T Increment, defaults to T{1}

take(size_t count)

<piped/util.hpp>

detail::take_ piped::take(size_t count)

Description

Generate the first n values input.

Example:

auto v = from(5) | take(3) | collect<std::vector>;
// => [5,6,7]
//
// Normally just use from_to() here, though.
Parameter
count size_t

skip(size_t n)

<piped/util.hpp>

detail::skip_ piped::skip(size_t n)

Description

Immediately skip the first n values from input. May empty input.

Example:

auto v = from_to(5,10) | skip(1) | collect<std::vector>;
// => [6,7,8,9]

Note, values are skipped immediately because it would otherwise be impossible to tell the correct value of empty(). It is required that if empty() returns false, next() return a non-empty value.

Parameter
n size_t

<F> filter(const F &f)

<piped/util.hpp>

template<typename F>
detail::filter_<F> piped::filter(const F &f)

Description

Generate values from input when f(v) returns true.

If f(v) returns false, values will be generated repeatedly until input is exhausted or true is returned.

auto v = from_to(0,10)
  | filter([](auto v) { return (v % 2) != 0; })
  | collect<std::vector>;

// => [1,3,5,7,9]
Parameter
f const F &

<F,T> reduce(F f, T initial)

<piped/util.hpp>

template<typename F undefined, typename T undefined>
detail::reduce_init_<F, T> piped::reduce(F f, T initial)

Description

Reduce/fold input given an initial value.

Example:

auto v = from_to(1,10) | collect<std::vector>;
auto i = v | each | reduce(std::plus<>(), 1);
// -> 46
Returns
The result of repeatedly calling r = f(r,v), or r, where r is the initial value.
Parameter
f F
initial T

<F> reduce(F f)

<piped/util.hpp>

template<typename F>
detail::reduce_<F> piped::reduce(F f)

Description

Reduce/fold input. Input is a generator.

Example:

auto v = from_to(1,10) | collect<std::vector>;
auto i = v | each | reduce(std::plus<>());
// -> 45
Returns
The result of repeatedly calling r = f(r,v), or r, where r is a default-initialized value.
Parameter
f F

<...Ts> zip(Ts &&... vs)

<piped/zip.hpp>

template<typename... Ts>
auto piped::zip(Ts &&... vs)

Description

Combine multiple generators' output into a std::tuple.

std::tuple based on the value_type of each specified generator. It will be empty when any of the generators are empty.

Example:

auto m = zip("a,b,c" | split(","), from(1)) | collect<std::map>;
// Produces {{"a",1}, {"b",2}, {"c",3}}
//
// Note however this is a map<string_view, int>
Parameter
vs Ts &&... A series of generators.