Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Query = struct
- let grep_arg (name : string) lst =
- let rec grep' acc = function
- | [] -> raise Not_found
- | (title, arg)::tl ->
- if title = name
- then (arg, (List.rev acc) @ tl)
- else grep' ((title, arg)::acc) tl
- in grep' [] lst
- let split s =
- let split_pair s =
- match String.split_on_char '=' s with
- | [l;r] -> (l, r)
- | _ -> failwith "bad_input"
- in
- String.split_on_char '&' s
- |> List.map split_pair
- module type Convert = sig
- type t
- val to_string : t -> string
- val of_string : string -> t
- end
- module String = struct
- type t = string
- let of_string x = x
- let to_string x = x
- end
- module Int = struct
- type t = int
- let of_string = int_of_string
- let to_string = string_of_int
- end
- module List (E : Convert) : Convert = struct
- type t = E.t list
- let of_string s = List.map E.of_string (StringLabels.split_on_char ~sep:',' s)
- let to_string l = StringLabels.concat ~sep:"," (List.map E.to_string l)
- end
- type (_,_) compose =
- | (::) : (string * (module Convert with type t = 'a)) * ('b, 'c) compose -> ('a -> 'b, 'c) compose
- | [] : ('c, 'c) compose
- let rec make_q : type ty v. (string -> v) -> (ty, v) compose -> ty =
- fun k ->
- function
- | [] -> k ""
- | (q, (module C)) :: rest ->
- let f x = make_q (function "" -> k @@ Printf.sprintf "%s=%s" q (C.to_string x)
- | str -> k @@ Printf.sprintf "%s=%s&%s" q (C.to_string x) str) rest
- in f
- let make_query = make_q (fun x -> x)
- let rec parse_q : type ty v. ty -> (ty, v) compose -> (string * string) list -> v =
- fun k ->
- function
- | [] ->
- fun sl -> k
- | (q, (module C)) :: rest ->
- fun sl ->
- let (arg, args) = grep_arg q sl in
- parse_q (k (C.of_string arg)) rest args
- let parse_query lst f s =
- parse_q f lst (split s)
- (*
- let rec parse_query : type ty v. ty -> (ty, v) compose -> string -> v = fun f -> function
- | [] -> fun _ -> ()
- | (q, (module C)) :: rest -> fun _ -> ()
- *)
- (*
- let () = parse_q [ "name", (module String); "age", (module Int)]
- *)
- end
Advertisement
Add Comment
Please, Sign In to add comment