Rosetta Code/Rank languages by popularity: Difference between revisions
Rosetta Code/Rank languages by popularity (view source)
Revision as of 17:39, 3 February 2024
, 4 months ago→{{header|Wren}}: Minor tidy and rerun
m (→{{header|Wren}}: Minor tidy and rerun) |
|||
(6 intermediate revisions by 4 users not shown) | |||
Line 1,176:
=={{header|C sharp|C#}}==
Sorting only programming languages.<br>
Note that this does not distinguish between tasks and non-task categories, so the counts can exceed the total number of tasks.<br>
Note this may need to set the Security Protocol type to Tls12 - see the section Problem running the C# entry in [[Talk:Rosetta_Code/Count_examples]]
<syntaxhighlight lang="csharp">
using System;
using System.Collections;
using System.Collections.Generic;
Line 1,188 ⟶ 1,191:
static void Main(string[] args)
{
string
);
ArrayList langs = new ArrayList();
Dictionary<string, int> qtdmbr = new Dictionary<string, int>();
string cmcontinue = "";
do
{
string get1 = new WebClient().DownloadString("http://www.rosettacode.org/w/api.php?"
+"action=query&list=categorymembers"
+"&cmtitle=Category:Programming_Languages"
+"&cmlimit=500&format=json"
+cmcontinue
);
cmcontinue = "";
MatchCollection languageMatch = new Regex("\"title\":\"Category:(.+?)\"").Matches(get1);
MatchCollection cmcontinueMatch = new Regex("cmcontinue\":\"([a-z0-9A-Z|]*)\"").Matches(get1);
foreach (Match lang in languageMatch) langs.Add(lang.Groups[1].Value);
foreach (Match more in cmcontinueMatch) cmcontinue = "&cmcontinue=" + more.Groups[1].Value;
}
while( cmcontinue != "" );
MatchCollection match2 =
new Regex("title=\"Category:(.+?)\">.+?</a>[^(]*\\(([\\d,.]+) members\\)").Matches(get2);
foreach (Match match in match2)
Line 1,203 ⟶ 1,223:
if (langs.Contains(match.Groups[1].Value))
{
qtdmbr.Add(match.Groups[1].Value, Int32.Parse(match.Groups[2].Value
.Replace(",",string.Empty)
.Replace(".",string.Empty)));
}
}
string[] test =
qtdmbr.OrderByDescending(x => x.Value).Select(x => String.Format("{0,4} {1}", x.Value, x.Key)).ToArray();
int count = 1;
Line 1,213 ⟶ 1,236:
foreach (string i in test)
{
Console.WriteLine("{0,
count++;
}
}
}
</syntaxhighlight>
{{out|Output (as of September 11, 2023)}}
<pre>
7: 1537 Perl
8: 1487 Python
9: 1376 J
10: 1287 C
. . .
</pre>
===Object-oriented solution===
<syntaxhighlight lang="csharp">using System;
Line 3,116 ⟶ 3,143:
=={{header|Nim}}==
<syntaxhighlight lang="nim">import std/[Algorithm, httpclient, json, re, strformat, strutils
const
LangSite = "http://www.rosettacode.org/
CatSite = "http://www.rosettacode.org/
let regex = re"title=""Category:(.*?)"">.+?</a>.*\((.*) members\)"
Line 3,131 ⟶ 3,158:
proc add(langs: var seq[string]; fromJson: JsonNode) =
for entry in fromJson{"query", "categorymembers"}:
if title.startsWith("Category:"):
langs.add title[9..^1]
var client = newHttpClient()
Line 3,158 ⟶ 3,187:
</syntaxhighlight>
Output:
<pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 896 - Rust
32 893 - Lua
33 849 - PicoLisp
34 843 - F Sharp
35 824 - AutoHotkey
36 818 - Ring
37 793 - Delphi
38 785 - JavaScript
39 778 - Arturo
40 776 - XPL0
...</pre>
Line 4,007 ⟶ 4,046:
$languages = [regex]::matches($response,'title="Category:(.*?)">') | foreach {$_.Groups[1].Value}
$response = [Net.WebClient]::new().DownloadString("http://rosettacode.org/
$response = [regex]::Replace($response,'(\d+),(\d+)','$1$2')
Line 4,039 ⟶ 4,078:
Rank: 10 (960 entries) Zkl
</pre>
===PowerShell: Using MediaWiki API method===
{{trans|Python}}
Line 4,504 ⟶ 4,544:
{{out|Output (as of October, 24, 2022)}}
<pre>
1 Rank: 1 (1,589 entries) Phix
2 Rank: 1 (1,589 entries) Wren
Line 4,515 ⟶ 4,554:
9 Rank: 9 (1,293 entries) J
10 Rank: 10 (1,213 entries) C
</pre>
=={{header|Racket}}==
{{trans|Python}}
<syntaxhighlight lang="racket">#lang racket
(require racket/hash
net/url
json)
(define limit 15)
(define (replacer cat) (regexp-replace #rx"^Category:(.*?)$" cat "\\1"))
(define category "Category:Programming Languages")
(define entries "entries")
(define api-url (string->url "http://rosettacode.org/mw/api.php"))
(define (make-complete-url gcmcontinue)
(struct-copy url api-url
[query `([format . "json"]
[action . "query"]
[generator . "categorymembers"]
[gcmtitle . ,category]
[gcmlimit . "200"]
[gcmcontinue . ,gcmcontinue]
[continue . ""]
[prop . "categoryinfo"])]))
(define @ hash-ref)
(define table (make-hash))
(let loop ([gcmcontinue ""])
(define resp (read-json (get-pure-port (make-complete-url gcmcontinue))))
(hash-union! table
(for/hash ([(k v) (in-hash (@ (@ resp 'query) 'pages))])
(values (@ v 'title #f) (@ (@ v 'categoryinfo (hash)) 'size 0))))
(cond [(@ resp 'continue #f) => (λ (c) (loop (@ c 'gcmcontinue)))]))
(for/fold ([prev #f] [rank #f] #:result (void))
([item (in-list (sort (hash->list table) > #:key cdr))] [i (in-range limit)])
(match-define (cons cat size) item)
(define this-rank (if (equal? prev size) rank (add1 i)))
(printf "Rank: ~a ~a ~a\n"
(~a this-rank #:align 'right #:min-width 2)
(~a (format "(~a ~a)" size entries) #:align 'right #:min-width 14)
(replacer cat))
(values size this-rank))</syntaxhighlight>
Output on September 4, 2019:
<pre>
Rank: 1 (1176 entries) Go
Rank: 2 (1112 entries) Phix
Rank: 3 (1107 entries) Perl 6
Rank: 4 (1099 entries) Julia
Rank: 5 (1079 entries) Python
Rank: 6 (1053 entries) Kotlin
Rank: 7 (1038 entries) Racket
Rank: 8 (1037 entries) Perl
Rank: 9 (968 entries) C
Rank: 10 (960 entries) Zkl
Rank: 11 (945 entries) J
Rank: 12 (935 entries) REXX
Rank: 13 (930 entries) Tcl
Rank: 14 (910 entries) Java
Rank: 15 (904 entries) Ruby
</pre>
Recent, occasionally (hourly-ish) updated output also available at:
http://www.timb.net/popular-languages.html.
=={{header|Raku}}==
Line 6,250 ⟶ 6,361:
0 Examples - WML
0 Examples - VAX Assembly</pre>
=={{header|Visual Basic .NET}}==
Uses Web scrapping to get the information from the Languages category page - as this now shows the number of pages in each category with the non-task pages separated, there is no need to access other pages.<br>
It may be necessary to set the Security Protocol to Tls12 at the start of the Main method in order to run this - see the "Problem running the C# entry" section on the [[Talk:Rosetta Code/Count examples]] page.
<syntaxhighlight lang="vbnet">
Imports System.Collections
Imports System.Collections.Generic
Imports System.Net
Imports System.Text.RegularExpressions
Module RankLanguagesByPopularity
Private Class LanguageStat
Public name As String
Public count As Integer
Public Sub New(name As String, count As Integer)
Me.name = name
Me.count = count
End Sub
End Class
Private Function CompareLanguages(x As LanguageStat, y As languageStat) As Integer
Dim result As Integer = 0
If x IsNot Nothing Or y IsNot Nothing Then
If x Is Nothing Then
result = -1
ElseIf y Is Nothing
result = 1
Else
result = - x.count.CompareTo(y.count) ' Sort descending count
If result = 0 Then
result = x.name.CompareTo(y.name) ' Sort ascending name
End If
End If
End If
Return result
End Function
Public Sub Main(ByVal args As String())
Dim languages As New List(Of LanguageStat)
Dim basePage As String = "https://rosettacode.org/wiki/Category:Programming_Languages"
Dim nextPage As String = basePage
Dim nextPageEx = New RegEx("<a href=""/wiki/Category:Programming_Languages([?]subcatfrom=[^""]*)""")
Dim languageStatEx = _
New Regex(">([^<]+?)</a>[^<]*<span title=""Contains *[0-9,.]* *[a-z]*, *([0-9,.]*) *page")
Do While nextPage <> ""
Dim wc As New WebClient()
Dim page As String = wc.DownloadString(nextPage)
nextPage = ""
For Each link In nextPageEx.Matches(page)
nextPage = basePage & link.Groups(1).Value
Next link
For Each language In languageStatEx.Matches(page)
Dim lCount As Integer = 0
Dim lName As String = language.Groups(1).Value
Dim countText As String = language.Groups(2).Value.Replace(",", "").Replace(".", "")
If Not Integer.TryParse(countText, lCount)
Console.Out.WriteLine(lName & "Invalid page count: <<" & countText & ">>")
Else
languages.Add(New LanguageStat(lName, lCount))
End If
Next language
Loop
languages.Sort(AddressOf CompareLanguages)
Dim prevCount As Integer = -1
Dim prevPosition As Integer = -1
Dim position As Integer = 0
For Each stat As LanguageStat In languages
position += 1
If stat.count <> prevCount Then
prevPosition = position
prevCount = stat.Count
End If
Console.Out.WriteLine(prevPosition.ToString.PadLeft(4) & ": " &
stat.count.ToString.PadLeft(4) & " " & stat.name)
Next stat
End Sub
End Module
</syntaxhighlight>
{{out}}
Sample output as at 11th September 2023
<pre>
1: 1652 Phix
1: 1652 Wren
3: 1614 Julia
4: 1595 Raku
5: 1576 Nim
6: 1546 Go
7: 1531 Perl
8: 1470 Python
9: 1375 J
10: 1285 C
. . .
</pre>
=={{header|Wren}}==
Line 6,256 ⟶ 6,470:
{{libheader|Wren-fmt}}
An embedded program so we can use libcurl.
<syntaxhighlight lang="
import "./pattern" for Pattern
Line 6,317 ⟶ 6,531:
var langs = findLangs.call()
langs.sort { |a, b| a[1] > b[1] }
System.print("Languages with most examples as at
var rank = 0
var lastScore = 0
Line 6,336 ⟶ 6,550:
<br>
We now embed this script in the following C program, build and run.
<syntaxhighlight lang="c">/* gcc
#include <stdio.h>
Line 6,510 ⟶ 6,724:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 6,531 ⟶ 6,745:
Showing just the top thirty.
<pre>
Languages with most examples as at
1 Wren 1,
3 Julia 1,
4 Raku 1,
5
6
7
8
9 J 1,
10
11
12
13
14
15
16
17
18
19
20
21 Sidef 1,
22
23
24
25
26
27
28 Scala
29
30
30 = Rust 940
</pre>
|