Jump to content

Rosetta Code/Rank languages by popularity: Difference between revisions

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".
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:
 
=={{header|Nim}}==
<lang nim>import httpclient, json, re, strformat, strutils, algorithm
 
const
langSiteLangSite = "http://www.rosettacode.org/wmw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json"
catSizeCatSize = "http://www.rosettacode.org/wmw/index.php?title=Special:Categories&limit=5000"
let regex = re"title=""Category:(.*?)"">.+?</a>.*\((.*) members\)"
 
vartype langs:Rank seq= tuple[lang: string], =count: @[int]
for l in parseJson(getContent(langSite))["query"]["categorymembers"]:
langs.add(l["title"].str.split("Category:")[1])
 
varproc ranks:cmp(a, seq[tuple[langb: string, countRank): 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 lentry in parseJson(getContent(langSite))[fromJson{"query"][, "categorymembers"]}:
langs.add(l entry["title"].strgetStr.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(catSizeCatSize).findAll(regex):
let lang = line.replacef(regex, "$1")
if lang in langs:
let count = parseInt(line.replacef(regex, "$2").replace(",", "").strip())
ranks.add( (lang, count))
 
ranks.sort(proc (x, y): int = cmp[int](y.count, x.count))
for i, lrank in ranks:
echo align($(&"{i + 1), :3),} align($l{rank.count, 5), ":4} - ", l{rank.lang</lang>}"
</lang>
Output:
<pre> 1 8331344 - TclGo
2 7811329 - RacketPhix
3 7701323 - PythonJulia
4 7301303 - Perl 6Raku
5 7251252 - JPerl
6 7121224 - CPython
7 7081120 - RubyKotlin
8 6981109 - DC
9 6741095 - GoJava
10 6561066 - PerlWren
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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.