partition_tf t ~f returns a pair t1, t2, where t1 is all elements of t that satisfy f, and t2 is all elements of t that do not satisfy f. The "tf" suffix is mnemonic to remind readers that the result is (trues, falses).
fold_result t ~init ~f is a short-circuiting version of fold that runs in the Result monad. If f returns an Error _, that value is returned without any additional invocations of f.
fold_until t ~init ~f ~finish is a short-circuiting version of fold. If f returns Stop _ the computation ceases and results in that value. If f returns Continue _, the fold will proceed. If f never returns Stop _, the final result is computed by finish.
Example:
type maybe_negative =
| Found_negative of int
| All_nonnegative of { sum : int }
(** [first_neg_or_sum list] returns the first negative number in [list], if any,
otherwise returns the sum of the list. *)
let first_neg_or_sum =
List.fold_until ~init:0
~f:(fun sum x ->
if x < 0
then Stop (Found_negative x)
else Continue (sum + x))
~finish:(fun sum -> All_nonnegative { sum })
;;
let x = first_neg_or_sum [1; 2; 3; 4; 5]
val x : maybe_negative = All_nonnegative {sum = 15}
let y = first_neg_or_sum [1; 2; -3; 4; 5]
val y : maybe_negative = Found_negative -3
Returns a min (resp. max) element from the collection using the provided compare function. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold so it has the same complexity as fold. Returns None iff the collection is empty.
ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~compare:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.
String append. Also available unqualified, but re-exported here for documentation purposes.
Note that a ^ b must copy both a and b into a newly-allocated result string, so a ^ b ^ c ^ ... ^ z is quadratic in the number of strings. String.concat does not have this problem -- it allocates the result buffer only once.
Caseless compares and hashes strings ignoring case, so that for example Caseless.equal "OCaml" "ocaml" and Caseless.("apple" < "Banana") are true.
index gives the index of the first appearance of char in the string when searching from left to right, or None if it's not found. rindex does the same but searches from the right.
For example, String.index "Foo" 'o' is Some 1 while String.rindex "Foo" 'o' is Some 2.
The _exn versions return the actual index (instead of an option) when char is found, and raise Stdlib.Not_found or Not_found_s otherwise.
Substring search and replace convenience functions. They call Search_pattern.create and then forget the preprocessed pattern when the search is complete. pos < 0 or pos >= length t result in no match (hence substr_index returns None and substr_index_exn raises). may_overlap indicates whether to report overlapping matches, see Search_pattern.index_all.
Sourceval substr_index_exn : ?pos:int ->t->pattern:t-> int
Sourceval substr_index_all : t->may_overlap:bool ->pattern:t->int list
If the string s contains the character on, then lsplit2_exn s ~on returns a pair containing s split around the first appearance of on (from the left). Raises Stdlib.Not_found or Not_found_s when on cannot be found in s.
If the string s contains the character on, then rsplit2_exn s ~on returns a pair containing s split around the first appearance of on (from the right). Raises Stdlib.Not_found or Not_found_s when on cannot be found in s.
split s ~on returns a list of substrings of s that are separated by on. Consecutive on characters will cause multiple empty strings in the result. Splitting the empty string returns a list of the empty string, not the empty list.
Sourceval split_on_chars : t->on:char list->t list
split_on_chars s ~on returns a list of all substrings of s that are separated by one of the chars from on. on are not grouped. So a grouping of on in the source string will produce multiple empty string splits in the result.
lstrip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning of s.
rstrip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the end of s.
strip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning and end of s.
tr_multi ~target ~replacement returns a function that replaces every instance of a character in target with the corresponding character in replacement.
If replacement is shorter than target, it is lengthened by repeating its last character. Empty replacement is illegal unless target also is.
If target contains multiple copies of the same character, the last corresponding replacement character is used. Note that character ranges are not supported, so ~target:"a-z" means the literal characters 'a', '-', and 'z'.
pad_left ?char s ~len returns s padded to the length len by adding characters char to the beginning of the string. If s is already longer than len it is returned unchanged.
pad_right ?char ~s len returns s padded to the length len by adding characters char to the end of the string. If s is already longer than len it is returned unchanged.
Reports the Levenshtein edit distance between two strings. Computes the minimum number of single-character insertions, deletions, and substitutions needed to transform one into the other.
For strings of length M and N, its time complexity is O(M*N) and its space complexity is O(min(M,N)).
Operations for escaping and unescaping strings, with parameterized escape and escapeworthy characters. Escaping/unescaping using this module is more efficient than using Pcre. Benchmark code can be found in core/benchmarks/string_escaping.ml.