a fast, general-purpose, persistent array structure for Gleam.
28

Configure Feed

Select the types of activity you want to include in your feed.

add index_fold_right

yoshi~ (May 7, 2025, 3:01 PM +0200) 45cf90cc 2127e863

+68 -2
+3
CHANGELOG.md
··· 1 + ## Unreleased 2 + 3 + Add `index_fold_right` 1 4 2 5 ## v1.2.0 3 6
+21 -2
src/iv.gleam
··· 132 132 //// [try_each](#try_each "Loop until error"), 133 133 //// [each_right](#each_right "Loop end to start"), 134 134 //// [fold](#fold "Loop start to end, with state"), 135 - //// [index_fold](#index_fold "Loop with the index, start to end"), 135 + //// [index_fold](#index_fold "Loop start to end, with index and state"), 136 136 //// [try_fold](#try_fold "Loop until error, with state"), 137 - //// [fold_right](#fold_right "Loop end to start, with state") 137 + //// [fold_right](#fold_right "Loop end to start, with state"), 138 + //// [index_fold_right](#index_fold_right "Loop end to start, with index and state") 138 139 139 140 import gleam/list 140 141 import gleam/yielder.{type Yielder} ··· 2069 2070 case array { 2070 2071 Empty -> state 2071 2072 Array(root:, ..) -> node.fold_right(root, state, fun) 2073 + } 2074 + } 2075 + 2076 + /// Like `fold`, but pass the current index to the accumulator function. 2077 + /// 2078 + /// <div style="text-align: right;"> 2079 + /// <a href="#"> 2080 + /// <small>Back to top ↑</small> 2081 + /// </a> 2082 + /// </div> 2083 + pub fn index_fold_right( 2084 + over array: Array(item), 2085 + from state: state, 2086 + with fun: fn(state, item, Int) -> state, 2087 + ) { 2088 + case array { 2089 + Empty -> state 2090 + Array(root:, ..) -> node.index_fold_right(0, root, state, fun) 2072 2091 } 2073 2092 } 2074 2093
+27
src/iv/internal/node.gleam
··· 948 948 } 949 949 } 950 950 951 + pub fn index_fold_right( 952 + offset: Int, 953 + node: Node(item), 954 + state: state, 955 + fun: fn(state, item, Int) -> state, 956 + ) -> state { 957 + case node { 958 + Balanced(children:, ..) -> 959 + vector.index_fold_right(children, state, fn(state, child, index) { 960 + let offset = offset + { index - 1 } * branch_factor 961 + index_fold_right(offset, child, state, fun) 962 + }) 963 + Unbalanced(children:, sizes:) -> 964 + vector.index_fold_right(children, state, fn(state, child, index) { 965 + let child_offset = case index { 966 + 1 -> 0 967 + _ -> vector.get(index - 1, sizes) 968 + } 969 + index_fold_right(offset + child_offset, child, state, fun) 970 + }) 971 + Leaf(children) -> 972 + vector.index_fold_right(children, state, fn(state, item, index) { 973 + fun(state, item, offset + index - 1) 974 + }) 975 + } 976 + } 977 + 951 978 pub fn try_fold(node: Node(_), state, fun) { 952 979 case node { 953 980 Balanced(children:, ..) | Unbalanced(children:, ..) ->
+17
src/iv/internal/vector.gleam
··· 118 118 } 119 119 } 120 120 121 + pub fn index_fold_right( 122 + xs: Vector(item), 123 + state: state, 124 + fun: fn(state, item, Int) -> state, 125 + ) -> state { 126 + let len = length(xs) 127 + index_fold_right_loop(xs, state, len, fun) 128 + } 129 + 130 + fn index_fold_right_loop(xs, state, idx, fun) { 131 + case idx > 0 { 132 + True -> 133 + index_fold_right_loop(xs, fun(state, get(idx, xs), idx), idx - 1, fun) 134 + False -> state 135 + } 136 + } 137 + 121 138 pub fn index_fold( 122 139 xs: Vector(item), 123 140 state: state,