This D entry uses Tango, but it should also show a version for Phobos:

http://rosettacode.org/wiki/Rosetta_Code/Count_examples#D

Two versions


The Mathematica solution is short:

TaskList = Flatten[
Import["http://rosettacode.org/wiki/Category:Programming_Tasks";, "Data"][[1, 1]]];
Print["Task \"", StringReplace[#, "_" -> " "], "\" has ",
Length@Select[Import["http://rosettacode.org/wiki/"; <> #, "Data"][[1,2]], StringFreeQ[#, __ ~~ "Programming Task" | __ ~~ "Omit"]& ], " example(s)"]&
  ~Map~ StringReplace[TaskList, " " -> "_"]


This Perl solution is compact:

use v5.10;
use Mojo::UserAgent;

my $site = "http://rosettacode.org";;
my $list_url = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";

my $ua = Mojo::UserAgent->new;
$ua->get("$site$list_url")->res->dom->find('cm')->each(sub {
    (my $slug = $_->{title}) =~ tr/ /_/;
my $count = $ua->get("$site/wiki/$slug")->res->dom->find("#toc .toclevel-1")->size;
    say "$_->{title}: $count examples";
});



The F# solution performs downloads concurrently and it's said to be fast:

#r "System.Xml.Linq.dll"

let uri1 = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"; let uri2 task = sprintf "http://www.rosettacode.org/w/index.php?title=%s&action=raw"; task

[|for xml in (System.Xml.Linq.XDocument.Load uri1).Root.Descendants() do
    for attrib in xml.Attributes() do
      if attrib.Name.LocalName = "title" then
        yield async {
let uri = uri2 (attrib.Value.Replace(" ", "_") |> System.Web.HttpUtility.UrlEncode)
          use client = new System.Net.WebClient()
          let! html = client.AsyncDownloadString(System.Uri uri)
let sols' = html.Split([|"{{header|"|], System.StringSplitOptions.None).Length - 1 lock stdout (fun () -> printfn "%s: %d examples" attrib.Value sols')
          return sols' }|]
|> Async.Parallel
|> Async.RunSynchronously
|> fun xs -> printfn "Total: %d examples" (Seq.sum xs)


Bye,
bearophile

Reply via email to