Associative array/Iteration: Difference between revisions

Content added Content deleted
(→‎{{header|Babel}}: Updated to interactive-mode syntax)
(→‎{{header|Elixir}}: remove deprecated module (Dict, HashDict), and then simplify)
Line 725: Line 725:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule RC do
<lang elixir>IO.inspect d = Map.new([foo: 1, bar: 2, baz: 3])
Enum.each(d, fn kv -> IO.inspect kv end)
def test_iterate(dict_impl \\ Map) do
Enum.each(d, fn {k,v} -> IO.puts "#{inspect k} => #{v}" end)
d = dict_impl.new |> Dict.put(:foo,1) |> Dict.put(:bar,2)
Enum.each(Map.keys(d), fn key -> IO.inspect key end)
print_vals(d)
Enum.each(Map.values(d), fn value -> IO.inspect value end)</lang>
end
defp print_vals(d) do
IO.inspect d
Enum.each(d, fn {k,v} -> IO.puts "#{k}: #{v}" end)
Enum.each(Dict.keys(d), fn key -> IO.inspect key end)
Enum.each(Dict.values(d), fn value -> IO.inspect value end)
end
end

IO.puts "< iterate Map >"
RC.test_iterate
IO.puts "\n< iterate HashDict >"
RC.test_iterate(HashDict)</lang>


{{out}}
{{out}}
<pre>
<pre>
%{bar: 2, baz: 3, foo: 1}
< iterate Map >
%{bar: 2, foo: 1}
{:bar, 2}
{:baz, 3}
bar: 2
foo: 1
{:foo, 1}
:bar => 2
:baz => 3
:foo => 1
:bar
:bar
:baz
:foo
:foo
2
2
3
1
1

< iterate HashDict >
#HashDict<[foo: 1, bar: 2]>
foo: 1
bar: 2
:foo
:bar
1
2
</pre>
</pre>