ocaml: phase 6 String.iter/iteri/fold_left/fold_right/to_seq/of_seq (+3 tests, 501 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Six new String functions, all in OCaml syntax inside runtime.sx: iter : index-walk with side-effecting f iteri : iter with index fold_left : thread accumulator left-to-right fold_right: thread accumulator right-to-left to_seq : return a char list (lazy in real OCaml; eager here) of_seq : concat a char list back to a string Round-trip: String.of_seq (List.rev (String.to_seq "hello")) = "olleh" Note: real OCaml's Seq is lazy. We return a plain list because the existing stdlib already provides exhaustive list operations and we don't yet have lazy sequences. If a baseline needs Seq.unfold or similar, we'll graduate to a proper Seq module then.
This commit is contained in:
@@ -374,6 +374,43 @@
|
||||
else aux (i + 1) (acc ^ f (_string_get s i))
|
||||
in
|
||||
aux 0 \"\"
|
||||
let iter f s =
|
||||
let rec aux i =
|
||||
if i >= _string_length s then ()
|
||||
else (f (_string_get s i); aux (i + 1))
|
||||
in
|
||||
aux 0
|
||||
let iteri f s =
|
||||
let rec aux i =
|
||||
if i >= _string_length s then ()
|
||||
else (f i (_string_get s i); aux (i + 1))
|
||||
in
|
||||
aux 0
|
||||
let fold_left f init s =
|
||||
let rec aux i acc =
|
||||
if i >= _string_length s then acc
|
||||
else aux (i + 1) (f acc (_string_get s i))
|
||||
in
|
||||
aux 0 init
|
||||
let fold_right f s init =
|
||||
let rec aux i acc =
|
||||
if i < 0 then acc
|
||||
else aux (i - 1) (f (_string_get s i) acc)
|
||||
in
|
||||
aux (_string_length s - 1) init
|
||||
let to_seq s =
|
||||
let rec aux i =
|
||||
if i >= _string_length s then []
|
||||
else _string_get s i :: aux (i + 1)
|
||||
in
|
||||
aux 0
|
||||
let of_seq xs =
|
||||
let rec aux ys acc =
|
||||
match ys with
|
||||
| [] -> acc
|
||||
| h :: t -> aux t (acc ^ h)
|
||||
in
|
||||
aux xs \"\"
|
||||
end ;;
|
||||
|
||||
module Bytes = struct
|
||||
|
||||
Reference in New Issue
Block a user