Jump to content

Associative array/Iteration: Difference between revisions

→‎{{header|Elixir}}: remove deprecated module (Dict, HashDict), and then simplify
(→‎{{header|Babel}}: Updated to interactive-mode syntax)
(→‎{{header|Elixir}}: remove deprecated module (Dict, HashDict), and then simplify)
Line 725:
 
=={{header|Elixir}}==
<lang elixir>defmoduleIO.inspect RCd do= Map.new([foo: 1, bar: 2, baz: 3])
Enum.each(Dict.keys(d), fn keykv -> IO.inspect keykv 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(DictMap.valueskeys(d), fn valuekey -> IO.inspect valuekey 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}}
<pre>
%{bar: 2, baz: 3, foo: 1}
< iterate Map >
%{:bar:, 2, foo: 1}
{:baz, 3}
bar: 2
foo{:foo, 1}
bar:bar => 2
:baz => 3
foo:foo => 1
:bar
:baz
:foo
2
3
1
 
< iterate HashDict >
#HashDict<[foo: 1, bar: 2]>
foo: 1
bar: 2
:foo
:bar
1
2
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.