let foldi t ~init ~f = let x = ref init in for i = 0 to length t - 1 do x := f i !x (S.get t i) done; !x