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/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json" |
|||
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\)" |
||
type Rank = tuple[lang: string, count: int] |
|||
⚫ | |||
⚫ | |||
proc cmp(a, b: Rank): int = |
|||
result = cmp(b.count, a.count) |
|||
⚫ | |||
if result == 0: result = cmp(a.lang, b.lang) |
|||
proc add(langs: var seq[string]; fromJson: JsonNode) = |
|||
⚫ | |||
⚫ | |||
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] |
|||
⚫ | |||
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 |
ranks.add (lang, count) |
||
ranks.sort( |
ranks.sort(cmp) |
||
for i, |
for i, rank in ranks: |
||
echo |
echo &"{i + 1:3} {rank.count:4} - {rank.lang}" |
||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre>1 1344 - Go |
||
2 |
2 1329 - Phix |
||
3 |
3 1323 - Julia |
||
4 |
4 1303 - Raku |
||
5 |
5 1252 - Perl |
||
6 |
6 1224 - Python |
||
7 |
7 1120 - Kotlin |
||
8 |
8 1109 - C |
||
9 |
9 1095 - Java |
||
10 |
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> |
||