Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 22, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyoo/6407570 to your computer and use it in GitHub Desktop.
Save dyoo/6407570 to your computer and use it in GitHub Desktop.
A second version of the program in https://gist.github.com/dyoo/6407562. Make simple things more complicated! :P
package main
// A question from reading Barry Schwartz's "The Paradox of Choice".
//
// How many English words start with "t"?
//
// How many English words have "t" as the third letter?
import (
"bufio"
"fmt"
"io"
"os"
)
func tabulate(aReader io.Reader, preds []func(string) bool) []int {
counts := make([]int, len(preds), len(preds))
scanner := bufio.NewScanner(aReader)
for scanner.Scan() {
nextWord := scanner.Text()
for i, pred := range preds {
if pred(nextWord) {
counts[i]++
}
}
}
return counts
}
func startsWithT(s string) bool {
return len(s) > 0 && (s[0] == 't' || s[0] == 'T')
}
func hasTeeAsThirdLetter(s string) bool {
return len(s) > 2 && (s[2] == 't' || s[2] == 'T')
}
func main() {
counts := tabulate(os.Stdin, []func(string) bool{
startsWithT,
hasTeeAsThirdLetter})
for _, c := range counts {
fmt.Println(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment