Rosetta Code/Count examples: Difference between revisions

m (→‎{{header|Racket}}: Fix <lang> tag)
Line 806:
 
==Icon and {{header|Unicon}}==
The following code uses features exclusive to Unicon. This version handles all tasks, not just the first 500.
 
<lang Unicon>$define RCINDEX "http://rosettacode.org/mw/api.php?format=xml&action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500"
Line 814:
$define TASKTOT "* Total Tasks *"
$define TOTTOT "* Total Headers*"
 
link strings
link hexcvt
 
procedure main(A) # simple single threaded read all at once implementation
Tasks := table(0)
every task := taskNames() do {
Tasks[TASKTOT] +:= 1 # count tasks
every lang := languages(task) do { # count languages
Tasks[task] +:= 1
Tasks[TOTTOT] +:= 1
}
}
every insert(O := set(),key(Tasks)) # extract & sort keys
O := put(sort(O--set(TOTTOT,TASKTOT)),TASKTOT,TOTTOT) # move totals to end
every write(k := !O, " : ", Tasks[k]," examples.") # report
end
 
# Generate task names
index := ReadURL(RCINDEX) # 1. read the index
procedure taskNames()
 
continue := ""
pages := []
while \(txt := ReadURL(RCINDEX||continue)) do {
index ? while tab(find("<cm ") & find(s :="title=\"")+*s) do
txt ? {
put(pages,tab(find("\""))) # 2. extract the pages
while tab(find("<cm ") & find(s :="title=\"")+*s) do
Tasks := table(0) suspend tab(find("\""))\1
if tab(find("cmcontinue=")) then {
every p := !pages do { # 3. process each page
continue := "&"||tab(upto(' \t'))
 
if p << A[1] then next # for tests on small #s}
else break
}
page := ReadURL(url := RCTASK||CleanURI(p))
}
Tasks[TASKTOT] +:= 1 # . count pages (tasks)
every find("=={{header|",page) do { # . count headers
Tasks[p] +:= 1
Tasks[TOTTOT] +:= 1
}
}
 
every insert(O := set(),key(Tasks)) # 4. extract & sort keys
O := put(sort(O--set(TOTTOT,TASKTOT)),TASKTOT,TOTTOT) # move totals at the end
 
every write(k := !O, " : ", Tasks[k]," examples.") # 5. report
end
 
# Generate language headers in a task
procedure CleanURI(u) #: clean up a URI
procedure languages(task)
static tr,dxml # xml & http translation
static WS
initial {
tr initial WS := table()' \t'
page := ReadURL(RCTASK||CleanURI(task))
every c := !string(~(&digits++&letters++'-_.!~*()/\'')) do
page ? while (tab(find("\n==")),tab(many(WS))|"",tab(find("{{"))) do {
tr[c] := "%"||hexstring(ord(c),2)
header := tab(find("=="))
every /tr[c := !string(&cset)] := c
tr[" "] := "_" header ? # wiki convention{
while tab(find("{{header|")) do {
every push(dxml := [],"&#"||right(ord(c := !"&<>'\""),3,"0")||";",c)
suspend 2(="{{header|",tab(find("}}")))\1
}
}
 
dxml[1] := u # insert URI as 1st arg}
u := replacem!dxml # de-xml it}
every (c := "") ||:= tr[!u] # reencode everything
return c
end
 
procedure ReadURLCleanURI(urlu) #: readclean URLup intoa stringURI
static tr,dxml # xml & http translation
write(&errout,"Opening ",image(url))
initial {
page := open(url,"m",RCUA,RCXUA) | stop("Unable to open ",url)
tr := table()
text := ""
every c := !string(~(&digits++&letters++'-_.!~*()/\'`')) do
if page["Status-Code"] < 300 then
tr[c] := "%"||hexstring(ord(c),2)
while text ||:= reads(page,-1)
every /tr[c := !string(&cset)] := c
else
tr[" "] := "_" # wiki convention
stop(page["Status-Code"]," ",page["Reason-Phrase"])
every push(dxml := [],"&#"||right(ord(c := !"&<>'\""),3,"0")||";",c)
close(page)
}
return text
dxml[1] := u # insert URI as 1st arg
u := replacem!dxml # de-xml it
every (c := "") ||:= tr[!u] # reencode everything
c := replace(c,"%3E","'") # Hack to put single quotes back in
c := replace(c,"%26quot%3B","\"") # Hack to put double quotes back in
return c
end
procedure ReadURL(url) #: read URL into string
page := open(url,"m",RCUA,RCXUA) | stop("Unable to open ",url)
text := ""
if page["Status-Code"] < 300 then while text ||:= reads(page,-1)
else write(&errout,image(url),": ",
page["Status-Code"]," ",page["Reason-Phrase"])
close(page)
return text
end</lang>
 
Line 878 ⟶ 895:
[http://www.cs.arizona.edu/icon/library/src/procs/strings.icn hexcvt provides hexstring]
 
Sample Output (Mayfor 26July 6, 20112013 (abridged):<pre>100 doors : 111 examples.
<pre>
24 game : 36 examples.
24100 game/Solvedoors : 20171 examples.
9924 Bottles of Beergame : 13360 examples.
A+B24 game/Solve : 8737 examples.
Abstract9 typebillion names of God the integer : 3812 examples.
Accumulator99 factoryBottles of Beer : 50199 examples.
Ackermann functionA+B : 102137 examples.
Abstract type : 54 examples.
Accumulator factory : 67 examples.
Ackermann function : 137 examples.
...
Y combinator : 4056 examples.
Yahoo! search interface : 1018 examples.
Yin and yang : 1839 examples.
Zig-zagZebra matrixpuzzle : 5012 examples.
*Zeckendorf Total Tasks *arithmetic : 4983 examples.
*Zeckendorf Totalnumber Headers*representation : 1807921 examples.</pre>
Zig-zag matrix : 67 examples.
* Total Tasks * : 676 examples.
* Total Headers* : 31146 examples.
</pre>
 
=={{header|J}}==