Guest User

Untitled

a guest
Jun 8th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.36 KB | None | 0 0
  1.  
  2. module Query = struct
  3.  
  4.   let grep_arg (name : string) lst =
  5.     let rec grep' acc = function
  6.       | [] -> raise Not_found
  7.       | (title, arg)::tl ->
  8.          if title = name
  9.          then (arg, (List.rev acc) @ tl)
  10.          else grep' ((title, arg)::acc) tl
  11.     in grep' [] lst
  12.  
  13.   let split s =
  14.     let split_pair s =
  15.       match String.split_on_char '=' s with
  16.       | [l;r] -> (l, r)
  17.       | _     -> failwith "bad_input"
  18.     in
  19.     String.split_on_char '&' s
  20.     |> List.map split_pair
  21.  
  22.   module type Convert = sig
  23.     type t
  24.     val to_string : t -> string
  25.     val of_string : string -> t
  26.   end
  27.  
  28.   module String = struct
  29.     type t = string
  30.     let of_string x = x
  31.     let to_string x = x
  32.   end
  33.  
  34.   module Int = struct
  35.     type t = int
  36.     let of_string = int_of_string
  37.     let to_string = string_of_int
  38.   end
  39.  
  40.   module List (E : Convert) : Convert = struct
  41.     type t = E.t list
  42.     let of_string s = List.map E.of_string (StringLabels.split_on_char ~sep:',' s)
  43.     let to_string l = StringLabels.concat ~sep:"," (List.map E.to_string l)
  44.   end
  45.  
  46.   type (_,_) compose =
  47.     | (::) : (string * (module Convert with type t = 'a)) * ('b, 'c) compose -> ('a -> 'b, 'c) compose
  48.     | []   : ('c, 'c) compose
  49.  
  50.   let rec make_q : type ty v. (string -> v) -> (ty, v) compose -> ty =
  51.     fun k ->
  52.     function
  53.     | [] -> k ""
  54.     | (q, (module C)) :: rest ->
  55.        let f x = make_q (function ""  -> k @@ Printf.sprintf "%s=%s" q (C.to_string x)
  56.                                 | str -> k @@ Printf.sprintf "%s=%s&%s" q (C.to_string x) str) rest
  57.        in f
  58.  
  59.   let make_query = make_q (fun x -> x)
  60.                  
  61.   let rec parse_q : type ty v. ty -> (ty, v) compose -> (string * string) list -> v =
  62.     fun k ->
  63.     function
  64.     | [] ->
  65.        fun sl -> k
  66.     | (q, (module C)) :: rest ->
  67.        fun sl ->
  68.        let (arg, args) = grep_arg q sl in
  69.        parse_q (k (C.of_string arg)) rest args
  70.  
  71.   let parse_query lst f s =
  72.     parse_q f lst (split s)
  73.                                  
  74.        (*      
  75.   let rec parse_query : type ty v. ty -> (ty, v) compose -> string ->  v = fun f -> function
  76.     | [] -> fun _ -> ()
  77.     | (q, (module C)) :: rest -> fun _ -> ()
  78.         *)
  79.                                         (*
  80.   let () = parse_q [ "name", (module String); "age", (module Int)]
  81.                                          *)
  82. end
Advertisement
Add Comment
Please, Sign In to add comment