Letter frequency: Difference between revisions

→‎{{header|Elixir}}: File.open -> File.read
(→‎{{header|Elixir}}: File.open -> File.read)
Line 699:
<lang elixir>file = hd(System.argv)
 
case File.open(file, [:read], fn(file) ->do
{:ok, binary} -> String.upcase(binary)
text = IO.read(file, :all)
|> String.codepoints(text)
|> Enum.filter_mapfilter(fn c -> c =~ ~r/[aA-zZ]/i end, &String.upcase(&1))
|> Enum.reduce(Map.new, fn c,acc -> Dict.update(acc, c, 1, &(&1+1)) end)
|> Enum.sort_by(fn {_k,v} -> -v end)
|> Enum.each(fn {k,v} -> IO.puts "#{k}\t #{v}" end)
{:error, reason} -> IO.inspect reason
end)</lang>
 
{{out}}
<pre>
C:\Elixir>elixir letterfrequency.exs \work\unixdict.txt
E 20144
A 16421
I 13980
R 13436
T 12836
O 12738
N 12097
S 10210
L 10061
C 8216
U 6489
M 5828
D 5799
P 5516
H 5208
G 4129
B 4115
Y 3633
F 2662
W 1968
K 1925
V 1902
X 617
Z 433
J 430
Q 378
</pre>
 
Anonymous user