Function frequency: Difference between revisions

Content added Content deleted
(Go solution)
Line 16: Line 16:
"io/ioutil"
"io/ioutil"
"os"
"os"
"sort"
)
)


Line 44: Line 45:
return true
return true
})
})
cs := make(calls, 0, len(m))
for k, v := range m {
for k, v := range m {
fmt.Println(v, k)
cs = append(cs, &call{k, v})
}
}
sort.Sort(cs)
}</lang>
for i, c := range cs {
fmt.Printf("%-20s %4d\n", c.expr, c.count)
if i == 9 {
break
}
}
}

type call struct {
expr string
count int
}
type calls []*call

func (c calls) Len() int { return len(c) }
func (c calls) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c calls) Less(i, j int) bool { return c[i].count > c[j].count }</lang>
Output, when run on source code above:
Output, when run on source code above:
<pre>
<pre>
len 3
4 fmt.Println
fmt.Println 3
1 string
f.Offset 2
1 a.Pos
make 2
1 ast.Inspect
fmt.Printf 1
1 ioutil.ReadFile
ioutil.ReadFile 1
1 len
a.Pos 1
2 f.Offset
string 1
1 token.NewFileSet
skeys byron 81> 1
1 ce.Pos
append 1
1 make
1 fs.File
1 parser.ParseFile
</pre>
</pre>

=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(let Freq NIL
<lang PicoLisp>(let Freq NIL