Anonymous user
Rosetta Code/Count examples: Difference between revisions
→Icon and {{header|Unicon}}
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
procedure taskNames()
continue := ""
while \(txt := ReadURL(RCINDEX||continue)) do {
txt ? {
while tab(find("<cm ") & find(s :="title=\"")+*s) do
if tab(find("cmcontinue=")) then {
continue := "&"||tab(upto(' \t'))
else break
}
}
end
# Generate language headers in a task
procedure languages(task)
static WS
page := ReadURL(RCTASK||CleanURI(task))
page ? while (tab(find("\n==")),tab(many(WS))|"",tab(find("{{"))) do {
header := tab(find("=="))
while tab(find("{{header|")) do {
suspend 2(="{{header|",tab(find("}}")))\1
}
end
procedure
static tr,dxml # xml & http translation
initial {
tr := table()
every c := !string(~(&digits++&letters++'-_.!~*()/\'`')) do
tr[c] := "%"||hexstring(ord(c),2)
every /tr[c := !string(&cset)] := c
tr[" "] := "_" # wiki convention
every push(dxml := [],"&#"||right(ord(c := !"&<>'\""),3,"0")||";",c)
}
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
<pre>
Abstract type : 54 examples.
Accumulator factory : 67 examples.
Ackermann function : 137 examples.
...
Y combinator :
Yahoo! search interface :
Yin and yang :
Zig-zag matrix : 67 examples.
* Total Tasks * : 676 examples.
* Total Headers* : 31146 examples.
</pre>
=={{header|J}}==
|