Rosetta Code/Rank languages by popularity: Difference between revisions

Content added Content deleted
m (Updated the number of computer programming languages that are used on Rosetta Code based on the number of members..)
(Updated to work with Nim 1.4: changed the way to get data from an URL. Major changes to take in account "continue" and "cmcontinue".)
Line 3,137: Line 3,137:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import httpclient, json, re, strutils, algorithm
<lang nim>import httpclient, json, re, strformat, strutils, algorithm


const
const
langSite = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json"
LangSite = "http://www.rosettacode.org/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json"
catSize = "http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=5000"
CatSize = "http://www.rosettacode.org/mw/index.php?title=Special:Categories&limit=5000"
let regex = re"title=""Category:(.*?)"">.+?</a>.*\((.*) members\)"
let regex = re"title=""Category:(.*?)"">.+?</a>.*\((.*) members\)"


var langs: seq[string] = @[]
type Rank = tuple[lang: string, count: int]
for l in parseJson(getContent(langSite))["query"]["categorymembers"]:
langs.add(l["title"].str.split("Category:")[1])


var ranks: seq[tuple[lang: string, count: int]] = @[]
proc cmp(a, b: Rank): int =
result = cmp(b.count, a.count)
for line in getContent(catSize).findAll(regex):
if result == 0: result = cmp(a.lang, b.lang)

proc add(langs: var seq[string]; fromJson: JsonNode) =
for entry in fromJson{"query", "categorymembers"}:
langs.add entry["title"].getStr.split("Category:")[1]

var client = newHttpClient()
var langs: seq[string]
var url = LangSite
while true:
let response = client.get(url)
if response.status != $Http200: break
let fromJson = response.body.parseJson()
langs.add fromJson
if "continue" notin fromJson: break
let cmcont = fromJson{"continue", "cmcontinue"}.getStr
let cont = fromJson{"continue", "continue"}.getStr
url = LangSite & fmt"&cmcontinue={cmcont}&continue={cont}"

var ranks: seq[Rank]
for line in client.getContent(CatSize).findAll(regex):
let lang = line.replacef(regex, "$1")
let lang = line.replacef(regex, "$1")
if lang in langs:
if lang in langs:
let count = parseInt(line.replacef(regex, "$2").strip())
let count = parseInt(line.replacef(regex, "$2").replace(",", "").strip())
ranks.add((lang, count))
ranks.add (lang, count)


ranks.sort(proc (x, y): int = cmp[int](y.count, x.count))
ranks.sort(cmp)
for i, l in ranks:
for i, rank in ranks:
echo align($(i+1), 3), align($l.count, 5), " - ", l.lang</lang>
echo &"{i + 1:3} {rank.count:4} - {rank.lang}"
</lang>
Output:
Output:
<pre> 1 833 - Tcl
<pre>1 1344 - Go
2 781 - Racket
2 1329 - Phix
3 770 - Python
3 1323 - Julia
4 730 - Perl 6
4 1303 - Raku
5 725 - J
5 1252 - Perl
6 712 - C
6 1224 - Python
7 708 - Ruby
7 1120 - Kotlin
8 698 - D
8 1109 - C
9 674 - Go
9 1095 - Java
10 656 - Perl
10 1066 - Wren
11 1064 - REXX
12 1061 - Racket
13 1021 - J
14 1012 - Zkl
15 1007 - Ruby
16 1001 - C++
17 993 - Nim
18 989 - Haskell
19 966 - D
20 961 - Tcl
21 915 - Scala
22 877 - C sharp
23 870 - Sidef
24 852 - Factor
25 830 - PicoLisp
26 792 - Lua
27 780 - Ada
28 778 - Rust
29 761 - Mathematica
30 721 - Common Lisp
...</pre>
...</pre>