Skip to content

Commit

Permalink
Remove unnecessary parts of Pgx_aux
Browse files Browse the repository at this point in the history
- Option was added to Stdlib
- List.iteri, String.concat, and String.init exist
- String.starts_with was unused
  • Loading branch information
brendanlong committed May 8, 2020
1 parent e343713 commit 9a152e6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 74 deletions.
4 changes: 2 additions & 2 deletions pgx/src/pgx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ module Make (Thread : Io) = struct
| Message_in.CommandComplete _ -> loop ()
| Message_in.EmptyQueryResponse -> loop ()
| Message_in.DataRow fields ->
List.map (Option.bind Value.of_string) fields |> f >>= loop
List.map (fun v -> Option.bind v Value.of_string) fields |> f >>= loop
| Message_in.NoData -> loop ()
| Message_in.ParameterStatus _ ->
(* 43.2.6: ParameterStatus messages will be generated whenever
Expand Down Expand Up @@ -969,7 +969,7 @@ module Make (Thread : Io) = struct
| _, Message_in.CopyData row -> loop acc ([ row |> Value.of_string ] :: rows) state
| _, Message_in.CopyDone -> loop acc rows state
| `Rows, Message_in.DataRow row ->
let row = List.map (Option.bind Value.of_string) row in
let row = List.map (fun v -> Option.bind v Value.of_string) row in
loop acc (row :: rows) `Rows
| (`Row_desc | `Rows), Message_in.CommandComplete _ ->
let rows = List.rev rows in
Expand Down
58 changes: 1 addition & 57 deletions pgx/src/pgx_aux.ml
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
module String = struct
include String

let starts_with str prefix =
let len = length prefix in
if length str < len
then false
else (
let rec aux i =
if i >= len
then true
else if unsafe_get str i <> unsafe_get prefix i
then false
else aux (i + 1)
in
aux 0)
;;

let join = concat

let implode xs =
let buf = Buffer.create (List.length xs) in
List.iter (Buffer.add_char buf) xs;
Expand All @@ -29,36 +12,6 @@ module String = struct
let rec loop i accum = if i = len then accum else loop (i + 1) (f accum str.[i]) in
loop 0 init
;;

(* Only available in the standard library since OCaml 4.02 *)
let init n f =
let s = Bytes.create n in
for i = 0 to n - 1 do
Bytes.unsafe_set s i (f i)
done;
Bytes.to_string s
;;
end

module Option = struct
include Option

let equal ?(cmp = ( = )) a b =
match a, b with
| None, None -> true
| Some a, Some b -> cmp a b
| _ -> false
;;

let map f = function
| Some v -> Some (f v)
| None -> None
;;

let bind f = function
| Some v -> f v
| None -> None
;;
end

module List = struct
Expand Down Expand Up @@ -99,16 +52,7 @@ module List = struct
if res <> 0 then res else compare compare_elt xs ys
;;

let iteri f xs =
let rec loop i = function
| [] -> ()
| hd :: tl ->
f i hd;
loop (i + 1) tl
in
loop 0 xs
;;

(* The default List.map isn't tail recursive so we replace it with one that is *)
let map f xs = List.rev_map f xs |> List.rev
end

Expand Down
13 changes: 0 additions & 13 deletions pgx/src/pgx_aux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@
module String : sig
include module type of String

val starts_with : string -> string -> bool
val join : string -> string list -> string
val implode : char list -> string
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
val init : int -> (int -> char) -> string
end

module Option : sig
include module type of Option

val equal : ?cmp:('a -> 'a -> bool) -> 'a option -> 'a option -> bool
val map : ('a -> 'b) -> 'a option -> 'b option
val bind : ('a -> 'b option) -> 'a option -> 'b option
end

module List : sig
include module type of List

val iteri : (int -> 'a -> unit) -> 'a list -> unit

(** Like the built-in [List.map], but tail-recursive *)
val map : ('a -> 'b) -> 'a list -> 'b list
end
Expand Down
4 changes: 2 additions & 2 deletions pgx/src/pgx_value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let required f = function
| None -> raise (Conversion_failure "Expected not-null but got null")
;;

let opt = Option.bind
let opt f v = Option.bind v f
let null = None

let of_binary b =
Expand Down Expand Up @@ -100,7 +100,7 @@ let of_hstore hstore =
in
key_str ^ "=>" ^ value_str
in
Some (String.join ", " (List.map string_of_mapping hstore))
Some (String.concat ", " (List.map string_of_mapping hstore))
;;

let to_hstore' str =
Expand Down

0 comments on commit 9a152e6

Please sign in to comment.