rangeless::fn
Folds and Loops

Functions

template<typename Result , typename Op >
impl::foldl< Result, Op > rangeless::fn::foldl (Result init, Op binary_op)
 Range-based version of c++20 (copy-free) std::accumulate More...
 
template<typename Op >
impl::foldl_d< Op > rangeless::fn::foldl_d (Op binary_op)
 Fold-Left-with-Default: this version uses default-initialized value for init. More...
 
template<typename Op >
impl::foldl_1< Op > rangeless::fn::foldl_1 (Op binary_op)
 Init-free version of foldl (first element is used as init); requires at least one element. More...
 
impl::sliding_window rangeless::fn::sliding_window (size_t win_size)
 
template<typename F >
impl::for_each< F > rangeless::fn::for_each (F fn)
 
template<typename F2 >
impl::for_each_adjacent< F2 > rangeless::fn::for_each_adjacent (F2 fn2)
 

Detailed Description

Function Documentation

◆ foldl()

template<typename Result , typename Op >
impl::foldl<Result, Op> rangeless::fn::foldl ( Result  init,
Op  binary_op 
)

Range-based version of c++20 (copy-free) std::accumulate

See also
foldl_d
foldl_1
using namespace fn::operators;
const std::string x = vec_t{ 1, 2, 3 }
% fn::foldl(std::string{"^"}, // init
[](std::string out, int in) // fold-op
{
return std::move(out) + "|" + std::to_string(in);
});
VERIFY(x == "^|1|2|3");

NB: The body of the fold-loop is init = binary_op(std::move(init), *it); If it is a move-ing iterator, like that of an seq, you'd take in by value or rvalue-reference in your binary operator, whereas if it's a regular iterator yielding lvalue-references, you'd take it by const or non-const reference.

Definition at line 3729 of file fn.hpp.

◆ foldl_1()

template<typename Op >
impl::foldl_1<Op> rangeless::fn::foldl_1 ( Op  binary_op)

Init-free version of foldl (first element is used as init); requires at least one element.

See also
foldl
foldl_d
const auto min_int =
std::vector<std::string>{{ "11", "-333", "22" }}
% fn::transform([](const string& s) { return std::stoi(s); })
% fn::foldl_1([](int out, int in) { return std::min(out, in); });
VERIFY(min_int == -333);

Definition at line 3778 of file fn.hpp.

◆ foldl_d()

template<typename Op >
impl::foldl_d<Op> rangeless::fn::foldl_d ( Op  binary_op)

Fold-Left-with-Default: this version uses default-initialized value for init.

See also
foldl
foldl_1
const std::string x = vec_t{ 1, 2, 3 }
[](std::string out, int in)
{
return std::move(out) + "|" + std::to_string(in);
});
VERIFY(x == "|1|2|3");

Definition at line 3757 of file fn.hpp.

◆ for_each()

template<typename F >
impl::for_each<F> rangeless::fn::for_each ( fn)

Invoke fn on each element.

NB: this is similar to a left-fold with binary-op (nullptr_t, x) -> nullptr_t where x is consumed by-side-effect, and the binary-op is simplified to unary (x)->void.

Definition at line 3811 of file fn.hpp.

◆ for_each_adjacent()

template<typename F2 >
impl::for_each_adjacent<F2> rangeless::fn::for_each_adjacent ( F2  fn2)

Invoke binary fn on each pair of adjacent elements.

NB: the inputs must satisfy ForwardRange or stronger.

Definition at line 3821 of file fn.hpp.

◆ sliding_window()

impl::sliding_window rangeless::fn::sliding_window ( size_t  win_size)
inline

Return a seq yielding a view of a fixed-sized sliding window over elements.

If the input is a seq, last win_size elements are internally cached in a deque.

const auto result =
make_inputs({ 1,2,3,4 })
% fn::foldl(0L, [](long out, auto v)
{
VERIFY(std::distance(v.begin(), v.end()) == 2);
auto it = v.begin();
return (out * 1000) + (*it * 10) + *std::next(it);
});
VERIFY(result == 12023034);

Definition at line 3801 of file fn.hpp.