Enumerations: Difference between revisions

no edit summary
(Added Odin variant)
No edit summary
Line 673:
fruits = ~w(apple banana cherry)a |> Enum.with_index |> Map.new
#=> %{apple: 0, banana: 1, cherry: 2}</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
in Org:RosettaCode
type Fruits
enum
int APPLE, BANANA, CHERRY
fun asText = <|me.name + "(" + me.value + ")"
end
type ExplicitFruits
enum
int APPLE = 10
int BANANA = 20
int CHERRY = 1
fun asText = <|me.name + "(" + me.value + ")"
end
type Main
for each generic enumeration in generic[Fruits, ExplicitFruits]
writeLine("[" + Generic.name(enumeration) + "]")
writeLine("getting an object with value = 1:")
writeLine(:enumeration.byValue(1))
writeLine("iterating over the items:")
for each var fruit in :enumeration
writeLine(fruit)
end
writeLine()
end
</syntaxhighlight>
{{out}}
<pre>
[Org:RosettaCode:Fruits]
getting an object with value = 1:
BANANA(1)
iterating over the items:
APPLE(0)
BANANA(1)
CHERRY(2)
 
[Org:RosettaCode:ExplicitFruits]
getting an object with value = 1:
CHERRY(1)
iterating over the items:
APPLE(10)
BANANA(20)
CHERRY(1)
</pre>
 
=={{header|Erlang}}==
214

edits