June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -16,4 +16,3 @@ For test purposes use the following four phrases in a list:
|
|||
|
||||
;Note:
|
||||
This task is fashioned after the action of the [http://www.softpanorama.org/Scripting/Shellorama/Control_structures/select_statements.shtml Bash select statement].
|
||||
<br><br>
|
||||
|
|
|
|||
28
Task/Menu/Julia/menu.julia
Normal file
28
Task/Menu/Julia/menu.julia
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
function _menu(items)
|
||||
for (ind, item) in enumerate(items)
|
||||
@printf " %2i) %s\n" ind item
|
||||
end
|
||||
end
|
||||
|
||||
_ok(::Any,::Any) = false
|
||||
function _ok(reply::AbstractString, itemcount)
|
||||
n = tryparse(Int, reply)
|
||||
return isnull(n) || 0 ≤ get(n) ≤ itemcount
|
||||
end
|
||||
|
||||
"Prompt to select an item from the items"
|
||||
function _selector(items, prompt::AbstractString)
|
||||
isempty(items) && return ""
|
||||
reply = -1
|
||||
itemcount = length(items)
|
||||
while !_ok(reply, itemcount)
|
||||
_menu(items)
|
||||
print(prompt)
|
||||
reply = strip(readline(STDIN))
|
||||
end
|
||||
return items[parse(Int, reply)]
|
||||
end
|
||||
|
||||
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
|
||||
item = _selector(items, "Which is from the three pigs: ")
|
||||
println("You chose: ", item)
|
||||
47
Task/Menu/Rust/menu.rust
Normal file
47
Task/Menu/Rust/menu.rust
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
fn menu_select<'a>(items: &'a [&'a str]) -> &'a str {
|
||||
if items.len() == 0 {
|
||||
return "";
|
||||
}
|
||||
|
||||
let stdin = std::io::stdin();
|
||||
let mut buffer = String::new();
|
||||
|
||||
loop {
|
||||
for (i, item) in items.iter().enumerate() {
|
||||
println!("{}) {}", i + 1, item);
|
||||
}
|
||||
print!("Pick a number from 1 to {}: ", items.len());
|
||||
|
||||
// Read the user input:
|
||||
stdin.read_line(&mut buffer).unwrap();
|
||||
println!();
|
||||
|
||||
if let Ok(selected_index) = buffer.trim().parse::<usize>() {
|
||||
if 0 < selected_index {
|
||||
if let Some(selected_item) = items.get(selected_index - 1) {
|
||||
return selected_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The buffer will contain the old input, so we need to clear it before we can reuse it.
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Empty list:
|
||||
let selection = menu_select(&[]);
|
||||
println!("No choice: {:?}", selection);
|
||||
|
||||
// List with items:
|
||||
let items = [
|
||||
"fee fie",
|
||||
"huff and puff",
|
||||
"mirror mirror",
|
||||
"tick tock",
|
||||
];
|
||||
|
||||
let selection = menu_select(&items);
|
||||
println!("You chose: {}", selection);
|
||||
}
|
||||
28
Task/Menu/Scala/menu.scala
Normal file
28
Task/Menu/Scala/menu.scala
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import scala.util.Try
|
||||
|
||||
object Menu extends App {
|
||||
val choice = menu(list)
|
||||
|
||||
def menu(menuList: Seq[String]): String = {
|
||||
if (menuList.isEmpty) "" else {
|
||||
val n = menuList.size
|
||||
|
||||
def getChoice: Try[Int] = {
|
||||
println("\n M E N U\n")
|
||||
menuList.zipWithIndex.map { case (text, index) => s"${index + 1}: $text" }.foreach(println(_))
|
||||
print(s"\nEnter your choice 1 - $n : ")
|
||||
Try {
|
||||
io.StdIn.readInt()
|
||||
}
|
||||
}
|
||||
|
||||
menuList(Iterator.continually(getChoice)
|
||||
.dropWhile(p => p.isFailure || !(1 to n).contains(p.get))
|
||||
.next.get - 1)
|
||||
}
|
||||
}
|
||||
|
||||
def list = Seq("fee fie", "huff and puff", "mirror mirror", "tick tock")
|
||||
|
||||
println(s"\nYou chose : $choice")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue