Menu: Difference between revisions

19 bytes added ,  20 days ago
m
(→‎{{header|D}}: Fix type mismatch)
(3 intermediate revisions by 2 users not shown)
Line 792:
try {
immutable n = input.to!int;
 
return typeof(return)((n >= 0 && n <= nEntries) ? n : -1);
} catch (Exception e) // Very generic
Line 805 ⟶ 806:
writefln(" %d) %s", i, entry);
"> ".write;
 
immutable input = readln.chomp;
 
immutable choice = validChoice(input, cast(int) (entries.length - 1));
 
if (choice.isNull)
"Wrong choice.".writeln;
Line 817 ⟶ 821:
immutable items = ["fee fie", "huff and puff",
"mirror mirror", "tick tock"];
 
writeln("You chose '", items.menuSelect, "'.");
}
Line 1,823 ⟶ 1,828:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .select = impure ffn(.entries) {
if .entries is not list: throw "invalid args"
if len(.entries) == 0: return ""
 
# print the menu
writeln join "\n", map(ffn(.e, .i) { $"\.i:2;: \.e;" }, .entries, 1..len .entries)
 
val .idx = number read(
"Select entry #: ",
ffn(.x) {
if not( .x -> RE/^[0-9]+$/): return false
val .y = number .x
.y > 0 and .y <= len(.entries)
885

edits