Rosetta Code/Rank languages by number of users: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added a ranking sorted by the name of the computer programming language.)
Line 162: Line 162:
44= 25 REXX
44= 25 REXX
</pre>
</pre>

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

const
Action = "action=query"
Format = "format=json"
FormatVersion = "formatversion=2"
Generator = "generator=categorymembers"
GcmTitle = "gcmtitle=Category:Language%20users"
GcmLimit = "gcmlimit=500"
Prop = "prop=categoryinfo"

Page = "http://rosettacode.org/mw/api.php?" &
[Action, Format, Formatversion, Generator, GcmTitle, GcmLimit, Prop].join("&")

type Counts = tuple[lang: string, count: int]

proc cmp(a, b: Counts): int =
## Compare two count tuples. Place the one with greater count field first and when
## count fields are equal, place the one with the first name in alphabetic order.
result = cmp(b.count, a.count)
if result == 0: result = cmp(a.lang, b.lang)

var client = newHttpClient()
var counts: seq[Counts]
var url = Page

while true:

# Access to the page and load the JSON representation.
let response = client.get(url)
if response.status != $Http200: break
let root = response.body.parseJson()

# Extract language names and number of users.
for node in root{"query", "pages"}:
var lang: string
if node["title"].getStr().scanf("Category:$+ User", lang):
let count = node{"categoryinfo", "size"}.getInt()
counts.add (lang, count)
if "continue" notin root: break

# Load continuation page.
let gcmcont = root{"continue", "gcmcontinue"}.getStr()
let cont = root{"continue", "continue"}.getStr()
url = Page & fmt"&gcmcontinue={gcmcont}&continue={cont}"

# Sort and display.
counts.sort(cmp)
var rank, lastCount = 0
for i, (lang, count) in counts:
if count != lastCount:
rank = i + 1
lastCount = count
echo &"{rank:3} {count:4} - {lang}"</lang>

{{out}}
<pre> 1 424 - C
2 308 - Java
3 302 - C++
4 299 - Python
5 278 - JavaScript
6 183 - PHP
7 181 - Perl
8 158 - SQL
9 148 - UNIX Shell
10 130 - C sharp
10 130 - Pascal
12 128 - BASIC
13 111 - Haskell
14 102 - Ruby
15 92 - Fortran
16 73 - Visual Basic
17 70 - Prolog
17 70 - Scheme
19 69 - AWK
20 64 - Lua
21 62 - Common Lisp
22 59 - HTML
23 51 - X86 Assembly
24 49 - Batch File
24 49 - Forth
26 47 - Assembly
27 44 - MATLAB
28 42 - Bash
29 41 - Lisp
30 40 - Delphi
30 40 - Erlang
32 39 - R
33 38 - Visual Basic .NET
34 37 - APL
34 37 - J
34 37 - Tcl
37 36 - COBOL
38 35 - Go
38 35 - Objective-C
38 35 - Smalltalk
41 34 - Brainf***
42 32 - Clojure
42 32 - Mathematica
44 29 - OCaml
45 27 - PostScript
45 27 - REXX
47 26 - Emacs Lisp
47 26 - LaTeX
47 26 - Perl 6
47 26 - Sed
51 25 - AutoHotkey
51 25 - CSS
53 23 - MySQL
53 23 - Racket
53 23 - Scala
53 23 - VBScript
53 23 - XSLT
58 22 - 6502 Assembly
59 21 - 8086 Assembly
60 19 - F Sharp
60 19 - Rust
60 19 - VBA
60 19 - Z80 Assembly
64 18 - Factor
64 18 - Logo
64 18 - Make
67 17 - Apex
67 17 - PL/I
67 17 - Standard ML
67 17 - TI-83 BASIC
71 16 - D
71 16 - Julia
71 16 - Modula-2
74 15 - ARM Assembly
74 15 - ActionScript
74 15 - AppleScript
74 15 - Icon
78 14 - Maxima
78 14 - PowerShell
78 14 - SNOBOL4
81 13 - Befunge
81 13 - Nim
83 12 - 80386 Assembly
83 12 - CoffeeScript
83 12 - Euphoria
83 12 - Unicon
87 11 - Eiffel
87 11 - Elixir
87 11 - Groovy
87 11 - Octave
87 11 - PARI/GP</pre>


=={{header|Perl}}==
=={{header|Perl}}==