[alg.fold]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.6 Non-modifying sequence operations [[alg.nonmodifying]](alg.nonmodifying#alg.fold)

### 26.6.18 Fold [alg.fold]

[ð](#lib:fold_left)

`template<[input_iterator](iterator.concept.input#concept:input_iterator "24.3.4.9 Concept input_­iterator [iterator.concept.input]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class T = iter_value_t<I>,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, I> F>
  constexpr auto ranges::fold_left(I first, S last, T init, F f);
template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, class T = range_value_t<R>,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, iterator_t<R>> F>
  constexpr auto ranges::fold_left(R&& r, T init, F f);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6188)

*Returns*: ranges::fold_left_with_iter(std::move(first), last, std::move(init), f).value

[ð](#lib:fold_left_first)

`template<[input_iterator](iterator.concept.input#concept:input_iterator "24.3.4.9 Concept input_­iterator [iterator.concept.input]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<iter_value_t<I>, I> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<iter_value_t<I>, iter_reference_t<I>>
  constexpr auto ranges::fold_left_first(I first, S last, F f);
template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<range_value_t<R>, iterator_t<R>> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<range_value_t<R>, range_reference_t<R>>
  constexpr auto ranges::fold_left_first(R&& r, F f);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6207)

*Returns*: ranges::fold_left_first_with_iter(std::move(first), last, f).value

[ð](#lib:fold_right)

`template<[bidirectional_iterator](iterator.concept.bidir#concept:bidirectional_iterator "24.3.4.12 Concept bidirectional_­iterator [iterator.concept.bidir]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class T = iter_value_t<I>,
         [indirectly-binary-right-foldable](algorithm.syn#concept:indirectly-binary-right-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, I> F>
  constexpr auto ranges::fold_right(I first, S last, T init, F f);
template<[bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]") R, class T = range_value_t<R>,
         [indirectly-binary-right-foldable](algorithm.syn#concept:indirectly-binary-right-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, iterator_t<R>> F>
  constexpr auto ranges::fold_right(R&& r, T init, F f);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6225)

*Effects*: Equivalent to:using U = decay_t<invoke_result_t<F&, iter_reference_t<I>, T>>;if (first == last)return U(std::move(init));
I tail = ranges::next(first, last);
U accum = invoke(f, *--tail, std::move(init));while (first != tail) accum = invoke(f, *--tail, std::move(accum));return accum;

[ð](#lib:fold_right_last)

`template<[bidirectional_iterator](iterator.concept.bidir#concept:bidirectional_iterator "24.3.4.12 Concept bidirectional_­iterator [iterator.concept.bidir]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S,
         [indirectly-binary-right-foldable](algorithm.syn#concept:indirectly-binary-right-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<iter_value_t<I>, I> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<iter_value_t<I>, iter_reference_t<I>>
  constexpr auto ranges::fold_right_last(I first, S last, F f);
template<[bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]") R,
         [indirectly-binary-right-foldable](algorithm.syn#concept:indirectly-binary-right-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<range_value_t<R>, iterator_t<R>> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<range_value_t<R>, range_reference_t<R>>
  constexpr auto ranges::fold_right_last(R&& r, F f);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6253)

Let U bedecltype(ranges​::​fold_right(first, last, iter_value_t<I>(*first), f))[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6257)

*Effects*: Equivalent to:if (first == last)return optional<U>();
I tail = ranges::prev(ranges::next(first, std::move(last)));return optional<U>(in_place,
 ranges::fold_right(std::move(first), tail, iter_value_t<I>(*tail), std::move(f)));

[ð](#lib:fold_left_with_iter)

`template<[input_iterator](iterator.concept.input#concept:input_iterator "24.3.4.9 Concept input_­iterator [iterator.concept.input]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class T = iter_value_t<I>,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, I> F>
  constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, class T = range_value_t<R>,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<T, iterator_t<R>> F>
  constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6280)

Let U be decay_t<invoke_result_t<F&, T, iter_reference_t<I>>>[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6283)

*Effects*: Equivalent to:if (first == last)return {std::move(first), U(std::move(init))};
U accum = invoke(f, std::move(init), *first);for (++first; first != last; ++first) accum = invoke(f, std::move(accum), *first);return {std::move(first), std::move(accum)};

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6295)

*Remarks*: The return type isfold_left_with_iter_result<I, U> for the first overload andfold_left_with_iter_result<borrowed_iterator_t<R>, U> for the second overload[.](#8.sentence-1)

[ð](#lib:fold_left_first_with_iter)

`template<[input_iterator](iterator.concept.input#concept:input_iterator "24.3.4.9 Concept input_­iterator [iterator.concept.input]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S,
         [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<iter_value_t<I>, I> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<iter_value_t<I>, iter_reference_t<I>>
  constexpr see below ranges::fold_left_first_with_iter(I first, S last, F f);
template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, [indirectly-binary-left-foldable](algorithm.syn#concept:indirectly-binary-left-foldable "26.4 Header <algorithm> synopsis [algorithm.syn]")<range_value_t<R>, iterator_t<R>> F>
  requires [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<range_value_t<R>, range_reference_t<R>>
  constexpr see below ranges::fold_left_first_with_iter(R&& r, F f);
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6315)

Let U bedecltype(ranges::fold_left(std::move(first), last, iter_value_t<I>(*first), f))

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6321)

*Effects*: Equivalent to:if (first == last)return {std::move(first), optional<U>()};
optional<U> init(in_place, *first);for (++first; first != last; ++first)*init = invoke(f, std::move(*init), *first);return {std::move(first), std::move(init)};

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L6333)

*Remarks*: The return type isfold_left_first_with_iter_result<I, optional<U>> for the first overload andfold_left_first_with_iter_result<borrowed_iterator_t<R>, optional<U>> for the second overload[.](#11.sentence-1)
