Rosetta Code/Count examples: Difference between revisions

Go solution
({{omit from|ZX Spectrum Basic}})
(Go solution)
Line 347:
 
This is 21× faster than the python thanks to the concurrency.
=={{header|Go}}==
<lang go>package main
 
import (
"bytes"
"fmt"
"http"
"io/ioutil"
"os"
"strings"
"xml"
)
 
func req(url string, foundCm func(string)) string {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err) // connection or request fail
return ""
}
defer resp.Body.Close()
for p := xml.NewParser(resp.Body); ; {
t, err := p.RawToken()
switch s, ok := t.(xml.StartElement); {
case err == os.EOF:
return ""
case err != nil:
fmt.Println(err)
return ""
case !ok:
continue
case s.Name.Local == "cm":
for _, a := range s.Attr {
if a.Name.Local == "title" {
foundCm(a.Value)
}
}
case s.Name.Local == "categorymembers" && len(s.Attr) > 0 &&
s.Attr[0].Name.Local == "cmcontinue":
return http.URLEscape(s.Attr[0].Value)
}
}
return ""
}
 
func main() {
taskQuery := "http://rosettacode.org/mw/api.php?action=query" +
"&format=xml&list=categorymembers&cmlimit=500" +
"&cmtitle=Category:Programming_Tasks"
continueAt := req(taskQuery, count)
for continueAt > "" {
continueAt = req(taskQuery+"&cmcontinue="+continueAt, count)
}
fmt.Printf("Total: %d examples.\n", total)
}
 
var marker = []byte("=={{header|")
var total int
 
func count(cm string) {
taskFmt := "http://rosettacode.org/mw/index.php?title=%s&action=raw"
taskEsc := http.URLEscape(strings.Replace(cm, " ", "_", -1))
resp, err := http.Get(fmt.Sprintf(taskFmt, taskEsc))
var page []byte
if err == nil {
page, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
if err != nil {
fmt.Println(err)
return
}
examples := bytes.Count(page, marker)
fmt.Printf("%s: %d\n", cm, examples)
total += examples
}</lang>
Output: (May 25, 2011)
<pre>
...
Y combinator: 40
Yahoo! search interface: 10
Yin and yang: 18
Zig-zag matrix: 50
Total: 18290 examples.
</pre>
 
=={{header|Haskell}}==
1,707

edits