Menu: Difference between revisions

(→‎{{header|D}}: Fix type mismatch)
 
(5 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 f(.entries) {
val choose = impure fn(entries) {
if .entries is not list: throw "invalid args"
if len(.entries) ==is 0not list: returnthrow "invalid args"
if not entries: 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 -> number
.y > 0 and .y <= len(.entries)
},
"invalid selection\n", -1,
) -> number
 
.entries[.idx]
}
 
writeln .selectchoose(["fee fie", "eat pi", "huff and puff", "tick tock"])</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
1,007

edits