Menu: Difference between revisions

981 bytes added ,  8 years ago
Added Elixir
(Added Elixir)
Line 620:
> 2
You chose 'mirror mirror'.</pre>
 
=={{header|Elixir}}==
<lang elixir>defmodule Menu do
def select(_, []), do: ""
def select(prompt, items) do
IO.puts ""
Enum.with_index(items) |> Enum.each(fn {item,i} -> IO.puts " #{i}. #{item}" end)
answer = IO.gets("#{prompt}: ") |> String.strip
case Integer.parse(answer) do
{num, ""} when num in 0..length(items)-1 -> Enum.at(items, num)
_ -> select(prompt, items)
end
end
end
 
# test empty list
response = Menu.select("Which is empty", [])
IO.puts "empty list returns: #{inspect response}"
# "real" test
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
response = Menu.select("Which is from the three pigs", items)
IO.puts "you chose: #{inspect response}"</lang>
 
{{out}}
<pre>
empty list returns: ""
 
0. fee fie
1. huff and puff
2. mirror mirror
3. tick tock
Which is from the three pigs: 4
 
0. fee fie
1. huff and puff
2. mirror mirror
3. tick tock
Which is from the three pigs: 3
you chose: "tick tock"
</pre>
 
=={{header|ERRE}}==
Line 664 ⟶ 705:
 
printf(1,"You chose %s.\n",{menu_select(items,prompt)})</lang>
 
 
 
 
=={{header|Factor}}==
Line 1,088 ⟶ 1,126:
 
choice{"fee fie", "huff and puff", "mirror mirror", "tick tock"}</lang>
 
 
=={{header|Mathematica}}==
Anonymous user