summaryrefslogtreecommitdiff
path: root/lib/util.ml
blob: b86119b14ea7271aa4753030d181e11b46b750bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(** The infamous [Util] module. A graveyard of broken expressions. *)

open Base

(** [f &> g] composes [g] with [f] to obrain [fun x -> g (f x)];
    in other words, it applies [f] {i and then} [g].

    @param f the first funtion to apply.
    @param g the second function to apply.
    @return the composition of [g] with [f]. *)
let ( &> ) f g = Fn.compose g f

(** Turn a sequence of bytes into a sequence.

    @param b the input bytes.
    @return a sequence of bytes. *)
let sequence_of_bytes (b : Bytes.t) : char Sequence.t =
  let open Sequence.Generator in
  let traverse b =
    let len = Bytes.length b in
    let rec loop i _ =
      if i >= len then return () else yield (Bytes.get b i) >>= loop (i + 1)
    in
    loop 0 ()
  in
  traverse b |> run

(** Turn a string into a sequence.

    @param s the input string.
    @return a sequence of bytes. *)
let sequence_of_string (s : string) : char Sequence.t =
  s |> String.to_list |> Sequence.of_list