Function frequency: Difference between revisions
Content added Content deleted
(Added Julia language) |
|||
Line 990: | Line 990: | ||
</lang> |
</lang> |
||
=={{header|Julia}}== |
|||
{{works with|Julia|0.6}} |
|||
<lang julia>using DataStructures |
|||
function funcfreqs(expr::Expr) |
|||
cnt = counter(Symbol) |
|||
expr.head == :call && |
|||
push!(cnt, expr.args[1]) |
|||
for e in expr.args |
|||
e isa Expr && merge!(cnt, funcfreqs(e)) |
|||
end |
|||
return cnt |
|||
end |
|||
function parseall(str::AbstractString) |
|||
exs = Any[] |
|||
pos = start(str) |
|||
while !done(str, pos) |
|||
ex, pos = parse(str, pos) # returns next starting point as well as expr |
|||
ex.head == :toplevel ? append!(exs, ex.args) : push!(exs, ex) |
|||
end |
|||
if isempty(exs) |
|||
throw(ParseError("end of input")) |
|||
elseif length(exs) == 1 |
|||
return exs[1] |
|||
else |
|||
return Expr(:block, exs...) |
|||
end |
|||
end |
|||
freqs = readstring("src/Function_frequency.jl") |> parseall |> funcfreqs |
|||
for (v, f) in freqs |
|||
@printf("%10s → %i\n", v, f) |
|||
end</lang> |
|||
{{out}} |
|||
<pre> append! → 1 |
|||
isa → 1 |
|||
start → 1 |
|||
push! → 2 |
|||
|> → 2 |
|||
funcfreqs → 2 |
|||
parseall → 1 |
|||
ParseError → 1 |
|||
! → 1 |
|||
length → 1 |
|||
throw → 1 |
|||
parse → 1 |
|||
readstring → 1 |
|||
== → 3 |
|||
counter → 1 |
|||
Expr → 1 |
|||
merge! → 1 |
|||
isempty → 1 |
|||
done → 1</pre> |
|||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |