Jump to content

Rosetta Code/Count examples: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 647:
categorymembers Categorymembers;
end</lang>
 
 
=={{header|Erlang}}==
Line 756 ⟶ 755:
 
This is 21&#215; faster than the python thanks to the concurrency.
 
=={{header|Factor}}==
Runs in about a minute. The number of threads is limited to 10 avoid cloudfare's protection mechanism.
Line 810:
 
Total: 30745 examples.</pre>
 
=={{header|Go}}==
<lang go>package main
Line 1,167 ⟶ 1,168:
</lang>
[[Count programming examples/Java/ScreenScrape|ScreenScrape class]]
 
 
 
=={{header|jq}}==
Line 1,851 ⟶ 1,850:
say "$_->{title}: $count examples";
});</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.12}}
Retrieves counts for both Tasks and Draft Tasks. Save / Display results as a sortable wikitable rather than a static list. Click on a column header to sort on that column. To do a secondary sort, hold down the shift key and click on a second column header. Tasks have a gray (default) background, Draft Tasks have a yellow background.
 
For a full output, see [[Rosetta Code/Count examples/Full list]]
<lang perl6>use HTTP::UserAgent;
use URI::Escape;
use JSON::Fast;
 
unit sub MAIN ( Str $no-fetch = '' );
 
# Friendlier descriptions for task categories
my %cat = (
'Programming_Tasks' => 'Task',
'Draft_Programming_Tasks' => 'Draft'
);
 
my $client = HTTP::UserAgent.new;
 
my $url = 'http://rosettacode.org/mw';
 
my $hashfile = './RC_Task_count.json';
my $tablefile = './RC_Task_count.txt';
 
my %tasks;
 
# clear screen
run($*DISTRO.is-win ?? 'cls' !! 'clear');
 
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;
 
# dump a copy to STDOUT, mostly for debugging purposes
say '<pre>Top ten programming languages by number of task examples completed:';
say "{('①' .. '⑩')[$_]} {@top10[$_]}" for ^@top10;
say "</pre>\n";
 
unless $no-fetch {
 
note 'Retrieving task information...';
 
mkdir('./pages') unless './pages'.IO.e;
 
for %cat.keys.sort -> $cat {
mediawiki-query(
$url, 'pages',
:generator<categorymembers>,
:gcmtitle("Category:$cat"),
:gcmlimit<350>,
:rawcontinue(),
:prop<title>
).map({
my $page =
$client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
"./pages/{ uri-escape .<title>.subst(/' '/, '_', :g) } ".IO.spurt($page);
my $lc = $page.lc;
my $count = +$lc.comb(/ ^^'==' <-[\n=]>* '{{header|' <-[}]>+? '}}==' \h* $$ /);
%tasks{.<title>} = {'cat' => %cat{$cat}, :$count};
%tasks{.<title>}<top10> = (^@top10).map( {
$lc.contains("\n==\{\{header|{@top10[$_].lc}}}==") ??
('①' .. '⑩')[$_] !! ' '
} ).join;
print clear, 1 + $++, ' ', %cat{$cat}, ' ', .<title>;
})
}
 
print clear;
 
note "\nTask information saved to local file: {$hashfile.IO.absolute}";
$hashfile.IO.spurt(%tasks.&to-json);
 
}
 
# Load information from local file
%tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( );
 
# Convert saved task info to a table
note "\nBuilding table...";
my $count = +%tasks;
my $taskcnt = +%tasks.grep: *.value.<cat> eq %cat<Programming_Tasks>;
my $draftcnt = $count - $taskcnt;
my $total = sum %tasks{*}»<count>;
 
# Dump table to a file
my $out = open($tablefile, :w) or die "$!\n";
 
$out.say: '<pre>Top ten programming languages by number of task examples completed:';
$out.say: "{('①' .. '⑩')[$_]} {@top10[$_]}" for ^@top10;
$out.say: "</pre>\n\n<div style=\"height:40em;overflow:scroll;\">";
 
# Add table boilerplate and caption
$out.say:
'{|class="wikitable sortable"', "\n",
"|+ As of { DateTime.new(time) } :: Tasks: { $taskcnt } ::<span style=\"background-color:#ffd\"> Draft Tasks:",
"{ $draftcnt } </span>:: Total Tasks: { $count } :: Total Examples: { $total }\n",
"!Count!!Task!!{('①' .. '⑩').join('!!')}"
;
 
# Sort tasks by count then add row
for %tasks.sort: { [-.value<count>, .key] } -> $task {
$out.say:
( $task.value<cat> eq 'Draft'
?? "|- style=\"background-color: #ffc\"\n"
!! "|-\n"
),
"| { $task.value<count> }\n",
( $task.key ~~ /\d/
?? "|data-sort-value=\"{ $task.key.&naturally }\"| [[{uri-escape $task.key}|{$task.key}]]\n"
!! "| [[{uri-escape $task.key}|{$task.key}]]\n"
),
"|{ $task.value<top10>.comb.join('||') }"
}
 
$out.say( "|}\n</div>" );
$out.close;
 
note "Table file saved as: {$tablefile.IO.absolute}";
 
sub mediawiki-query ($site, $type, *%query) {
my $url = "$site/api.php?" ~ uri-query-string(
:action<query>, :format<json>, :formatversion<2>, |%query);
my $continue = '';
 
gather loop {
my $response = $client.get("$url&$continue");
my $data = from-json($response.content);
take $_ for $data.<query>.{$type}.values;
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
}
}
 
sub uri-query-string (*%fields) { %fields.map({ "{.key}={uri-escape .value}" }).join("&") }
 
sub naturally ($a) { $a.lc.subst(/(\d+)/, ->$/ {0~(65+$0.chars).chr~$0},:g) }
 
sub clear { "\r" ~ ' ' x 116 ~ "\r" }</lang>
 
{{out|Abridged output}}
<pre>Top ten programming language by number of task examples completed:
① Go
② Perl 6
③ Kotlin
④ Python
⑤ Phix
⑥ Racket
⑦ Perl
⑧ C
⑨ Julia
⑩ Tcl
</pre>
 
<div style="height:40em;overflow:scroll;">
{|class="wikitable sortable"
|+ As of 2019-01-21T22:41:28Z :: Tasks: 924 ::<span style="background-color:#ffd"> Draft Tasks:215 </span>:: Total Tasks: 1139 :: Total Examples: 59984
!Count!!Task!!①!!②!!③!!④!!⑤!!⑥!!⑦!!⑧!!⑨!!⑩
|-
| 405
| [[Hello%20world%2FText|Hello world/Text]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 283
|data-sort-value="0C99 bottles of beer"| [[99%20Bottles%20of%20Beer|99 Bottles of Beer]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 276
| [[FizzBuzz|FizzBuzz]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 273
|data-sort-value="0D100 doors"| [[100%20doors|100 doors]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 251
| [[Fibonacci%20sequence|Fibonacci sequence]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 246
| [[Comments|Comments]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 239
| [[Factorial|Factorial]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 223
| [[Empty%20program|Empty program]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 222
| [[A%2BB|A+B]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 214
| [[Function%20definition|Function definition]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|}
</div>
 
=={{header|Phix}}==
Line 2,454 ⟶ 2,245:
s))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.12}}
Retrieves counts for both Tasks and Draft Tasks. Save / Display results as a sortable wikitable rather than a static list. Click on a column header to sort on that column. To do a secondary sort, hold down the shift key and click on a second column header. Tasks have a gray (default) background, Draft Tasks have a yellow background.
 
For a full output, see [[Rosetta Code/Count examples/Full list]]
<lang perl6>use HTTP::UserAgent;
use URI::Escape;
use JSON::Fast;
 
unit sub MAIN ( Str $no-fetch = '' );
 
# Friendlier descriptions for task categories
my %cat = (
'Programming_Tasks' => 'Task',
'Draft_Programming_Tasks' => 'Draft'
);
 
my $client = HTTP::UserAgent.new;
 
my $url = 'http://rosettacode.org/mw';
 
my $hashfile = './RC_Task_count.json';
my $tablefile = './RC_Task_count.txt';
 
my %tasks;
 
# clear screen
run($*DISTRO.is-win ?? 'cls' !! 'clear');
 
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;
 
# dump a copy to STDOUT, mostly for debugging purposes
say '<pre>Top ten programming languages by number of task examples completed:';
say "{('①' .. '⑩')[$_]} {@top10[$_]}" for ^@top10;
say "</pre>\n";
 
unless $no-fetch {
 
note 'Retrieving task information...';
 
mkdir('./pages') unless './pages'.IO.e;
 
for %cat.keys.sort -> $cat {
mediawiki-query(
$url, 'pages',
:generator<categorymembers>,
:gcmtitle("Category:$cat"),
:gcmlimit<350>,
:rawcontinue(),
:prop<title>
).map({
my $page =
$client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
"./pages/{ uri-escape .<title>.subst(/' '/, '_', :g) } ".IO.spurt($page);
my $lc = $page.lc;
my $count = +$lc.comb(/ ^^'==' <-[\n=]>* '{{header|' <-[}]>+? '}}==' \h* $$ /);
%tasks{.<title>} = {'cat' => %cat{$cat}, :$count};
%tasks{.<title>}<top10> = (^@top10).map( {
$lc.contains("\n==\{\{header|{@top10[$_].lc}}}==") ??
('①' .. '⑩')[$_] !! ' '
} ).join;
print clear, 1 + $++, ' ', %cat{$cat}, ' ', .<title>;
})
}
 
print clear;
 
note "\nTask information saved to local file: {$hashfile.IO.absolute}";
$hashfile.IO.spurt(%tasks.&to-json);
 
}
 
# Load information from local file
%tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( );
 
# Convert saved task info to a table
note "\nBuilding table...";
my $count = +%tasks;
my $taskcnt = +%tasks.grep: *.value.<cat> eq %cat<Programming_Tasks>;
my $draftcnt = $count - $taskcnt;
my $total = sum %tasks{*}»<count>;
 
# Dump table to a file
my $out = open($tablefile, :w) or die "$!\n";
 
$out.say: '<pre>Top ten programming languages by number of task examples completed:';
$out.say: "{('①' .. '⑩')[$_]} {@top10[$_]}" for ^@top10;
$out.say: "</pre>\n\n<div style=\"height:40em;overflow:scroll;\">";
 
# Add table boilerplate and caption
$out.say:
'{|class="wikitable sortable"', "\n",
"|+ As of { DateTime.new(time) } :: Tasks: { $taskcnt } ::<span style=\"background-color:#ffd\"> Draft Tasks:",
"{ $draftcnt } </span>:: Total Tasks: { $count } :: Total Examples: { $total }\n",
"!Count!!Task!!{('①' .. '⑩').join('!!')}"
;
 
# Sort tasks by count then add row
for %tasks.sort: { [-.value<count>, .key] } -> $task {
$out.say:
( $task.value<cat> eq 'Draft'
?? "|- style=\"background-color: #ffc\"\n"
!! "|-\n"
),
"| { $task.value<count> }\n",
( $task.key ~~ /\d/
?? "|data-sort-value=\"{ $task.key.&naturally }\"| [[{uri-escape $task.key}|{$task.key}]]\n"
!! "| [[{uri-escape $task.key}|{$task.key}]]\n"
),
"|{ $task.value<top10>.comb.join('||') }"
}
 
$out.say( "|}\n</div>" );
$out.close;
 
note "Table file saved as: {$tablefile.IO.absolute}";
 
sub mediawiki-query ($site, $type, *%query) {
my $url = "$site/api.php?" ~ uri-query-string(
:action<query>, :format<json>, :formatversion<2>, |%query);
my $continue = '';
 
gather loop {
my $response = $client.get("$url&$continue");
my $data = from-json($response.content);
take $_ for $data.<query>.{$type}.values;
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
}
}
 
sub uri-query-string (*%fields) { %fields.map({ "{.key}={uri-escape .value}" }).join("&") }
 
sub naturally ($a) { $a.lc.subst(/(\d+)/, ->$/ {0~(65+$0.chars).chr~$0},:g) }
 
sub clear { "\r" ~ ' ' x 116 ~ "\r" }</lang>
 
{{out|Abridged output}}
<pre>Top ten programming language by number of task examples completed:
① Go
② Perl 6
③ Kotlin
④ Python
⑤ Phix
⑥ Racket
⑦ Perl
⑧ C
⑨ Julia
⑩ Tcl
</pre>
 
<div style="height:40em;overflow:scroll;">
{|class="wikitable sortable"
|+ As of 2019-01-21T22:41:28Z :: Tasks: 924 ::<span style="background-color:#ffd"> Draft Tasks:215 </span>:: Total Tasks: 1139 :: Total Examples: 59984
!Count!!Task!!①!!②!!③!!④!!⑤!!⑥!!⑦!!⑧!!⑨!!⑩
|-
| 405
| [[Hello%20world%2FText|Hello world/Text]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 283
|data-sort-value="0C99 bottles of beer"| [[99%20Bottles%20of%20Beer|99 Bottles of Beer]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 276
| [[FizzBuzz|FizzBuzz]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 273
|data-sort-value="0D100 doors"| [[100%20doors|100 doors]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 251
| [[Fibonacci%20sequence|Fibonacci sequence]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 246
| [[Comments|Comments]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 239
| [[Factorial|Factorial]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 223
| [[Empty%20program|Empty program]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 222
| [[A%2BB|A+B]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|-
| 214
| [[Function%20definition|Function definition]]
|①||②||③||④||⑤||⑥||⑦||⑧||⑨||⑩
|}
</div>
 
=={{header|Ring}}==
Line 2,609:
puts
puts "Total: #{total_examples}"</lang>
 
=={{header|Run BASIC}}==
<lang runbasic>html "<table border=1><tr bgcolor=wheat align=center><td>Num</td><td>Task</td><td>Examples</td></tr>"
 
a$ = httpGet$("http://rosettacode.org/wiki/Category:Programming_Tasks")
a$ = word$(a$,1,"</table></div>")
i = instr(a$,"<a href=""/wiki/")
i = instr(a$,"<a href=""/wiki/",i+1)
while i > 0
count = count + 1
i = instr(a$,"<a href=""/wiki/",i+1)
j = instr(a$,">",i+5)
a1$ = mid$(a$,i+15,j-i)
taskId$ = word$(a1$,1,"""")
task$ = word$(a1$,3,"""")
url$ = "http://rosettacode.org/wiki/";taskId$
a2$ = httpGet$(url$)
ii = instr(a2$,"<span class=""tocnumber"">")
jj = 0
while ii > 0
jj = ii
ii = instr(a2$,"<span class=""tocnumber"">",ii+10)
wend
if jj = 0 then
examp = 0
else
kk = instr(a2$,"<",jj+24)
examp = int(val(mid$(a2$,jj+24,kk-jj-24)))
end if
html "<tr><td align=right>";count;"</td><td>";task$;"</td><td align=right>";examp;"</td></tr>"
totExamp = totExamp + examp
wend
html "<tr bgcolor=wheat><td>**</td><td>** Total **</td><td align=right>";totExamp;"</td></tr></table>"
end</lang>
<table border=1><tr bgcolor=wheat align=center><td>Num</td><td>Task</td><td>Examples</td></tr>
<tr><td align=right>1</td><td>100 doors</td><td align=right>165</td></tr>
<tr><td align=right>2</td><td>24 game</td><td align=right>56</td></tr>
<tr><td align=right>3</td><td>24 game/Solve</td><td align=right>34</td></tr>
<tr><td align=right>4</td><td>99 Bottles of Beer</td><td align=right>192</td></tr>
<tr><td align=right>5</td><td>A+B</td><td align=right>129</td></tr>
<tr><td align=right>6</td><td>Abstract type</td><td align=right>51</td></tr>
<tr><td align=right>7</td><td>Accumulator factory</td><td align=right>65</td></tr>
<tr><td align=right>8</td><td>Ackermann function</td><td align=right>132</td></tr>
<tr><td align=right>9</td><td>Active Directory/Connect</td><td align=right>13</td></tr>
<tr><td align=right>10</td><td>Active Directory/Search for a user</td><td align=right>13</td></tr>
<tr><td align=right>11</td><td>Active object</td><td align=right>22</td></tr>
<tr><td align=right>12</td><td>Add a variable to a class instance at runtime</td><td align=right>37</td></tr>
<tr><td align=right>...</td><td>...</td><td align=right>...</td></tr>
<tr><td align=right>655</td><td>Y combinator</td><td align=right>53</td></tr>
<tr><td align=right>656</td><td>Yahoo! search interface</td><td align=right>16</td></tr>
<tr><td align=right>657</td><td>Yin and yang</td><td align=right>38</td></tr>
<tr><td align=right>658</td><td>Zebra puzzle</td><td align=right>12</td></tr>
<tr><td align=right>659</td><td>Zeckendorf number representation</td><td align=right>18</td></tr>
<tr><td align=right>660</td><td>Zig-zag matrix</td><td align=right>65</td></tr>
<tr bgcolor=wheat><td>**</td><td>** Total **</td><td align=right>28611</td></tr></table>
 
=={{header|Rust}}==
Line 2,734 ⟶ 2,789:
}
}</lang>
 
=={{header|Run BASIC}}==
<lang runbasic>html "<table border=1><tr bgcolor=wheat align=center><td>Num</td><td>Task</td><td>Examples</td></tr>"
 
a$ = httpGet$("http://rosettacode.org/wiki/Category:Programming_Tasks")
a$ = word$(a$,1,"</table></div>")
i = instr(a$,"<a href=""/wiki/")
i = instr(a$,"<a href=""/wiki/",i+1)
while i > 0
count = count + 1
i = instr(a$,"<a href=""/wiki/",i+1)
j = instr(a$,">",i+5)
a1$ = mid$(a$,i+15,j-i)
taskId$ = word$(a1$,1,"""")
task$ = word$(a1$,3,"""")
url$ = "http://rosettacode.org/wiki/";taskId$
a2$ = httpGet$(url$)
ii = instr(a2$,"<span class=""tocnumber"">")
jj = 0
while ii > 0
jj = ii
ii = instr(a2$,"<span class=""tocnumber"">",ii+10)
wend
if jj = 0 then
examp = 0
else
kk = instr(a2$,"<",jj+24)
examp = int(val(mid$(a2$,jj+24,kk-jj-24)))
end if
html "<tr><td align=right>";count;"</td><td>";task$;"</td><td align=right>";examp;"</td></tr>"
totExamp = totExamp + examp
wend
html "<tr bgcolor=wheat><td>**</td><td>** Total **</td><td align=right>";totExamp;"</td></tr></table>"
end</lang>
<table border=1><tr bgcolor=wheat align=center><td>Num</td><td>Task</td><td>Examples</td></tr>
<tr><td align=right>1</td><td>100 doors</td><td align=right>165</td></tr>
<tr><td align=right>2</td><td>24 game</td><td align=right>56</td></tr>
<tr><td align=right>3</td><td>24 game/Solve</td><td align=right>34</td></tr>
<tr><td align=right>4</td><td>99 Bottles of Beer</td><td align=right>192</td></tr>
<tr><td align=right>5</td><td>A+B</td><td align=right>129</td></tr>
<tr><td align=right>6</td><td>Abstract type</td><td align=right>51</td></tr>
<tr><td align=right>7</td><td>Accumulator factory</td><td align=right>65</td></tr>
<tr><td align=right>8</td><td>Ackermann function</td><td align=right>132</td></tr>
<tr><td align=right>9</td><td>Active Directory/Connect</td><td align=right>13</td></tr>
<tr><td align=right>10</td><td>Active Directory/Search for a user</td><td align=right>13</td></tr>
<tr><td align=right>11</td><td>Active object</td><td align=right>22</td></tr>
<tr><td align=right>12</td><td>Add a variable to a class instance at runtime</td><td align=right>37</td></tr>
<tr><td align=right>...</td><td>...</td><td align=right>...</td></tr>
<tr><td align=right>655</td><td>Y combinator</td><td align=right>53</td></tr>
<tr><td align=right>656</td><td>Yahoo! search interface</td><td align=right>16</td></tr>
<tr><td align=right>657</td><td>Yin and yang</td><td align=right>38</td></tr>
<tr><td align=right>658</td><td>Zebra puzzle</td><td align=right>12</td></tr>
<tr><td align=right>659</td><td>Zeckendorf number representation</td><td align=right>18</td></tr>
<tr><td align=right>660</td><td>Zig-zag matrix</td><td align=right>65</td></tr>
<tr bgcolor=wheat><td>**</td><td>** Total **</td><td align=right>28611</td></tr></table>
 
=={{header|Scala}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.