Rosetta Code/Count examples: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: lines too long)
Line 689: Line 689:
<lang ocaml>open Http_client.Convenience
<lang ocaml>open Http_client.Convenience


let repl_quote s =
let repl_quote s =
let reg = Str.regexp_string "&#039;" in
let reg = Str.regexp_string "&#039;" in
(Str.global_replace reg "%27" s)
(Str.global_replace reg "%27" s)

let repl_space s =
let repl_space s =
let s = String.copy s in
let s = String.copy s in
Line 699: Line 700:
done;
done;
(s)
(s)

let count_ex s =
let count_ex s =
let pat = Str.regexp_string "=={{header|" in
let pat = Str.regexp_string "=={{header|" in
Line 709: Line 710:
in
in
aux 0 0
aux 0 0

let get_child child xml =

let child =
List.find
(function Xml.Element (tag,_,_) when tag = child -> true | _ -> false) xml
in
Xml.children child
let () =
let () =
let url = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&\
let url = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&\
cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml" in
cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml" in

let xml = Xml.parse_string (http_get url) in
let xml = Xml.parse_string (http_get url) in

let total = ref 0 in
let total = ref 0 in
at_exit (fun () -> Printf.printf "\n Total: %d\n" !total);
at_exit (fun () -> Printf.printf "\n Total: %d\n" !total);

let f = function
let f = function
| Xml.Element ("cm", attrs, _) ->
| Xml.Element ("cm", attrs, _) ->
Line 732: Line 739:
| _ -> ()
| _ -> ()
in
in

match xml with
match xml with
| Xml.Element ("api", [],
| Xml.Element ("api", _, ch) ->
[Xml.Element ("query", [],
let query = get_child "query" ch in
[Xml.Element ("categorymembers", [], cms)])]) -> List.iter f cms
let catmb = get_child "categorymembers" query in
List.iter f catmb
| _ -> ()</lang>
| _ -> ()</lang>