Rosetta Code/Rank languages by popularity: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: updated the number of computer programming languages (on Rosetta Code).)
(→‎Perl 6: Using the API: Enhance output, style tweaks, exposition)
Line 3,155: Line 3,155:


===Perl 6: Using the API===
===Perl 6: Using the API===
Note that this counts '''only''' the tasks. It does not include other non-task categories in the counts yielding more realistic, non-inflated numbers. Perl 6 is unicode aware and handles non-ASCII names natively. This does not attempt to 'unify' different language names that are the same behind the scenes as a result of Rosettacodes' capitalization peculiarities. (E.G. μC++, UC++ & ΜC++)


<lang perl6>use HTTP::UserAgent;
<lang perl6>use HTTP::UserAgent;
use JSON::Tiny;
use URI::Escape;
use JSON::Fast;

my $client = HTTP::UserAgent.new;

my $url = 'http://rosettacode.org/mw';

my $start-time = now;

say "========= Generated: { DateTime.new(time) } =========";;

my $this = 1;
my $rank = 0;
my $last = 0;
my $tie = ' ';


.say for
.say for
mediawiki-query('http://rosettacode.org/mw', 'pages',
mediawiki-query(
generator => 'categorymembers',
$url, 'pages',
:generator<categorymembers>,
gcmtitle => "Category:Programming Languages",
:gcmtitle<Category:Programming Languages>,
prop => 'categoryinfo')
:gcmlimit<350>,
:rawcontinue(),
:prop<categoryinfo>
)

.map({ %( name => .<title>.subst(/^'Category:'/, ''),
.map({ %( name => .<title>.subst(/^'Category:'/, ''),
count => .<categoryinfo><pages> || 0 ) })
count => .<categoryinfo><pages> || 0 ) })

.sort({ -.<count>, .<name> })
.sort({ -.<count>, .<name> })

.map({ last if .<count>==0; sprintf "%3d. %3d - %s", ++$, .<count name> });
.map( { display(.<count>, .<name>) } );

say "========= elapsed: {(now - $start-time).round(.01)} seconds =========";

sub display ($count, $lang) {
if $last != $count {
$last = $count;
$rank = $this;
$tie = ' '
} else {
$tie = 'T'
};
sprintf "#%-4s Rank: %3d %s with %-4s %s: %s", $this++, $rank,
$tie, $count, ($count == 1 ?? 'entry ' !! 'entries'), $lang;
}


sub mediawiki-query ($site, $type, *%query) {
sub mediawiki-query ($site, $type, *%query) {
my $url = "$site/api.php?" ~ uri-query-string(
my $url = "$site/api.php?" ~ uri-query-string(
:action<query>, :format<json>, :gcmlimit<350>, :rawcontinue(), |%query);
:action<query>, :format<json>, :formatversion<2>, |%query);
my $continue = '';
my $continue = '';

my $client = HTTP::UserAgent.new;
gather loop {
gather loop {
my $response = $client.get("$url&$continue");
my $response = $client.get("$url&$continue");
my $data = from-json($response.content);
my $data = from-json($response.content);
take $_ for $data.<query>.{$type}.values;
take $_ for $data.<query>.{$type}.values;
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
}
}
Line 3,189: Line 3,219:


sub uri-query-string (*%fields) {
sub uri-query-string (*%fields) {
%fields.map({ "{.key}={uri-encode .value}" }).join("&")
join '&', %fields.map: { "{.key}={uri-escape .value}" }
}

sub uri-encode ($str) {
$str.subst(/<[\x00..\xff]-[a..zA..Z0..9_.~-]>/, *.ord.fmt('%%%02X'), :g)
}</lang>
}</lang>

{{out|Abridged output - First and last 10 lines}}
<pre>========= Generated: 2017-12-23T14:26:59Z =========
#1 Rank: 1 with 958 entries: Racket
#2 Rank: 2 with 943 entries: Python
#3 Rank: 3 with 922 entries: Perl 6
#4 Rank: 4 with 914 entries: Tcl
#5 Rank: 5 with 888 entries: Kotlin
#6 Rank: 6 with 880 entries: J
#7 Rank: 7 with 872 entries: C
#8 Rank: 8 with 858 entries: Zkl
#9 Rank: 9 with 843 entries: Ruby
#10 Rank: 10 with 824 entries: Go

...

#666 Rank: 589 T with 0 entries: UC++
#667 Rank: 589 T with 0 entries: UScript
#668 Rank: 589 T with 0 entries: UserRPL
#669 Rank: 589 T with 0 entries: VAX Assembly
#670 Rank: 589 T with 0 entries: VRML
#671 Rank: 589 T with 0 entries: WML
#672 Rank: 589 T with 0 entries: WebAssembly
#673 Rank: 589 T with 0 entries: X10
#674 Rank: 589 T with 0 entries: XBase
#675 Rank: 589 T with 0 entries: XS
#676 Rank: 589 T with 0 entries: ΜC++
========= elapsed: 3.04 seconds =========
</pre>


===Perl 6: Using web scraping===
===Perl 6: Using web scraping===