Skip to main content

reduce-right ()

Reduces a list to a single value as the product of calling a specified function on every item in a list, starting with the last item to the first.

Parameters

$list
The list from which items will be reduced.
Type
List
$transformer
The transform function to apply to each item. The first parameter is the accumulated value previously returned in the prior transformation or the initial value. The second parameter is the current item being processed in the list. The third parameter is the index of the current item being processed in the list. The fourth parameter is the list being processed. The returned value will be the product of the prior and current transformation.
Type
Function
$initial-value
The value to use as the first parameter for the first call to the transformer. When not specified, this value is initialized to the last value in the list, and the second parameter of the transformer is initialized to the second-to-last value in the list. When specified, the second parameter of the transformer is initialized to the last value in the list. Must be passed by name.
Type
*
Default
%derived%

Return Value

*
The value that results from running the transformer function to completion over the entire list.

Example

@use 'sass:meta';
@use '@sass-fairy/list';

@function _concat-right($accumulator, $item, $void...) {
@return list.concat($accumulator, $item);
}

$list: [[0, 1], [2, 3], [4, 5]];

@debug list.reduce($list, meta.get-function('_concat-right'));
// [4, 5, 2, 3, 0, 1]