Rosetta Code/Count examples: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: fix file open error handling)
(→‎{{header|Perl 6}}: Modify to show the top 10 language completion rate instead of draft/task status which is denoted by color anyway)
Line 1,823: Line 1,823:
note 'Retrieving task information...';
note 'Retrieving task information...';


mkdir('./pages') unless './pages'.IO.e;
for %cat.keys -> $cat {

my %counts =
mediawiki-query(
$url, 'pages',
:generator<categorymembers>,
:gcmtitle<Category:Programming Languages>,
:gcmlimit<350>,
:rawcontinue(),
:prop<categoryinfo>
)
.map({ .<title>.subst(/^'Category:'/, '') => .<categoryinfo><pages> || 0 });

my @top10 = %counts.sort( -*.value ).head(10).map: *.key;

for %cat.keys.sort -> $cat {
mediawiki-query(
mediawiki-query(
$url, 'pages',
$url, 'pages',
Line 1,834: Line 1,849:
my $page =
my $page =
$client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
$client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
"./pages/{ uri-escape .<title>.subst(/' '/, '_', :g) } ".IO.spurt($page);
my $count = +$page.lc.comb(/ ^^'==' <-[\n=]>* '{{header|' \w+ \N+ '==' \h* $$ /);
my $count = +$page.lc.comb(/ ^^'==' <-[\n=]>* '{{header|' \w+ \N+ '==' \h* $$ /);
%tasks{.<title>} = {'cat' => %cat{$cat}, :$count};
%tasks{.<title>} = {'cat' => %cat{$cat}, :$count};
%tasks{.<title>}<top10> = (^@top10).map( {
$page.contains("\n==\{\{header|{@top10[$_]}}}==") ??
('①' .. '⑩')[$_] !! ' '
} ).join;
print clear, 1 + $++, ' ', %cat{$cat}, ' ', .<title>;
print clear, 1 + $++, ' ', %cat{$cat}, ' ', .<title>;
})
})
Line 1,850: Line 1,870:
%tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( );
%tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( );


# Convert saved task / author info to a table
# Convert saved task info to a table
note "\nBuilding table...";
note "\nBuilding table...";
my $count = +%tasks;
my $count = +%tasks;
Line 1,858: Line 1,878:


# Dump table to a file
# Dump table to a file
my $out = open($tablefile, :w) orelse .die;
my $out = open($tablefile, :w) or die "$!\n";

# Show top 10
$out.say: '<pre>Top ten programming language by number of task examples:';
$out.say: "{('①' .. '⑩')[$_]} {@top10[$_]}" for ^@top10;
$out.say: "</pre>\n";


# Add table boilerplate and caption
# Add table boilerplate and caption
$out.say:
$out.say:
'{|class="wikitable sortable"', "\n",
'{|class="wikitable sortable"', "\n",
"|+ As of { Date.today } :: Tasks: { $taskcnt } ::<span style=\"background-color:#ffd\"> Draft Tasks:",
"|+ As of { DateTime.new(time) } :: Tasks: { $taskcnt } ::<span style=\"background-color:#ffd\"> Draft Tasks:",
"{ $draftcnt } </span>:: Total Tasks: { $count } :: Total Examples: { $total }\n",
"{ $draftcnt } </span>:: Total Tasks: { $count } :: Total Examples: { $total }\n",
"! Count !! Task !! Category"
"! Count !! Task !! Top 10"
;
;


Line 1,880: Line 1,905:
!! "| [[{uri-escape $task.key}|{$task.key}]]\n"
!! "| [[{uri-escape $task.key}|{$task.key}]]\n"
),
),
"| { $task.value<cat> }"
"| { $task.value<top10> }"
}
}


Line 1,905: Line 1,930:
sub naturally ($a) { $a.lc.subst(/(\d+)/, ->$/ {0~(65+$0.chars).chr~$0},:g) }
sub naturally ($a) { $a.lc.subst(/(\d+)/, ->$/ {0~(65+$0.chars).chr~$0},:g) }


sub clear { "\r" ~ ' ' x 100 ~ "\r" }</lang>
sub clear { "\r" ~ ' ' x 116 ~ "\r" }</lang>


{{out|Abridged output}}
{{out|Abridged output}}
<pre>Top ten programming language by number of task examples:
{|class="wikitable sortable" style="width:60em;"
① Go
|+ As of 2018-04-10 :: Tasks: 871 ::<span style="background-color:#ffd"> Draft Tasks:209 </span>:: Total Tasks: 1080 :: Total Examples: 55787
② Perl 6
! Count !! Task !! Category
③ Kotlin
④ Python
⑤ Phix
⑥ Racket
⑦ Perl
⑧ C
⑨ Julia
⑩ Tcl
</pre>

{|class="wikitable sortable"
|+ As of 2019-01-20T16:46:21Z :: Tasks: 924 ::<span style="background-color:#ffd"> Draft Tasks:215 </span>:: Total Tasks: 1139 :: Total Examples: 59971
! Count !! Task !! Top 10
|-
|-
| 383
| 405
| [[Hello%20world%2FText|Hello world/Text]]
| [[Hello%20world%2FText|Hello world/Text]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 270
| 283
|data-sort-value="0C99 bottles of beer"| [[99%20Bottles%20of%20Beer|99 Bottles of Beer]]
|data-sort-value="0C99 bottles of beer"| [[99%20Bottles%20of%20Beer|99 Bottles of Beer]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 262
| 276
| [[FizzBuzz|FizzBuzz]]
| ①②③④⑤⑥⑦⑧⑨⑩
|-
| 273
|data-sort-value="0D100 doors"| [[100%20doors|100 doors]]
|data-sort-value="0D100 doors"| [[100%20doors|100 doors]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 262
| 251
| [[Fibonacci%20sequence|Fibonacci sequence]]
| [[FizzBuzz|FizzBuzz]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 239
| 246
| [[Comments|Comments]]
| [[Comments|Comments]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 239
| 239
| [[Fibonacci%20sequence|Fibonacci sequence]]
| Task
|-
| 230
| [[Factorial|Factorial]]
| [[Factorial|Factorial]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 215
| 223
| [[Empty%20program|Empty program]]
| [[Empty%20program|Empty program]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 214
| 222
| [[A%2BB|A+B]]
| [[A%2BB|A+B]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|-
|-
| 207
| 214
| [[Function%20definition|Function definition]]
| [[Function%20definition|Function definition]]
| ①②③④⑤⑥⑦⑧⑨⑩
| Task
|}
|}