Rosetta Code/Rank languages by popularity: Difference between revisions

Undo revision 332606 by KaiseR mayhem (talk)
(Undo revision 332606 by KaiseR mayhem (talk))
Tag: Undo
Line 4,501:
langs[1:10,]
 
</syntaxhighlight>
{{out|Output (as of October, 24, 2022)}}
<pre>
Line 4,515 ⟶ 4,514:
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}}==