rangeless::fn
rangeless::fn::by Namespace Reference

Common key-functions to use with sort_by/unque_by/group_all_by. More...

Classes

struct  dereferenced
 
struct  first
 
struct  get
 e.g. for tuples or pairs, fn::group_adjacent_by(fn::by::get<string>{}) More...
 
struct  identity
 
struct  second
 

Functions

template<typename T >
impl::gt< T > decreasing (T x)
 Wraps the passed value and exposes inverted operator<. More...
 
template<typename T >
impl::gt< const T & > decreasing (std::reference_wrapper< T > x)
 
template<typename F >
impl::comp< F > make_comp (F key_fn)
 Make binary comparison predicate from a key-function. More...
 

Detailed Description

Common key-functions to use with sort_by/unque_by/group_all_by.

ptrs = %= fn::sort_by(fn::by::dereferenced{});
std::move(pairs) % fn::group_all_by(fn::by::second{});

Function Documentation

◆ decreasing() [1/2]

template<typename T >
impl::gt<T> rangeless::fn::by::decreasing ( x)

Wraps the passed value and exposes inverted operator<.

{{
// sort by longest-first, then lexicographically
const auto inp = std::vector<std::string>{ "2", "333", "1", "222", "3" };
const auto expected = std::vector<std::string>{ "222", "333", "1", "2", "3" };
auto ret1 = inp % fn::sort_by([](const string& s)
{
return std::make_tuple(fn::by::decreasing(s.size()), std::ref(s));
});
VERIFY(ret1 == expected);
auto ret2 = inp % fn::sort_by([](const string& s)
{
return std::make_tuple(s.size(), fn::by::decreasing(std::ref(s)));
}) % fn::reverse();
VERIFY(ret2 == expected);
// we can also compose by::decreasing with a key-function
auto ret3 = inp % fn::sort_by(fn::by::decreasing([](const std::string& s)
{
return std::make_tuple(s.size(), fn::by::decreasing(std::ref(s)));
}));
VERIFY(ret3 == expected);
// we can also create a comparator from a key-function.
auto ret4 = inp;
gfx::timsort(ret4.begin(), ret4.end(), fn::by::make_comp([](const std::string& s)
{
return std::make_tuple(fn::by::decreasing(s.size()), std::ref(s));
}));
VERIFY(ret4 == expected);
// NB: we can't use std::tie because fn::by::decreasing returns an rvalue,
// so we use std::make_tuple and capture s by std::ref.
}}

Definition at line 898 of file fn.hpp.

◆ decreasing() [2/2]

template<typename T >
impl::gt<const T&> rangeless::fn::by::decreasing ( std::reference_wrapper< T >  x)

Definition at line 917 of file fn.hpp.

◆ make_comp()

template<typename F >
impl::comp<F> rangeless::fn::by::make_comp ( key_fn)

Make binary comparison predicate from a key-function.

Definition at line 927 of file fn.hpp.