···132132//// [try_each](#try_each "Loop until error"),
133133//// [each_right](#each_right "Loop end to start"),
134134//// [fold](#fold "Loop start to end, with state"),
135135-//// [index_fold](#index_fold "Loop with the index, start to end"),
135135+//// [index_fold](#index_fold "Loop start to end, with index and state"),
136136//// [try_fold](#try_fold "Loop until error, with state"),
137137-//// [fold_right](#fold_right "Loop end to start, with state")
137137+//// [fold_right](#fold_right "Loop end to start, with state"),
138138+//// [index_fold_right](#index_fold_right "Loop end to start, with index and state")
138139139140import gleam/list
140141import gleam/yielder.{type Yielder}
···20692070 case array {
20702071 Empty -> state
20712072 Array(root:, ..) -> node.fold_right(root, state, fun)
20732073+ }
20742074+}
20752075+20762076+/// Like `fold`, but pass the current index to the accumulator function.
20772077+///
20782078+/// <div style="text-align: right;">
20792079+/// <a href="#">
20802080+/// <small>Back to top ↑</small>
20812081+/// </a>
20822082+/// </div>
20832083+pub fn index_fold_right(
20842084+ over array: Array(item),
20852085+ from state: state,
20862086+ with fun: fn(state, item, Int) -> state,
20872087+) {
20882088+ case array {
20892089+ Empty -> state
20902090+ Array(root:, ..) -> node.index_fold_right(0, root, state, fun)
20722091 }
20732092}
20742093