This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #206 Switch statement with strings
Execute different procedures foo, bar, baz and barfl if the string str contains the name of the respective procedure. Do it in a way natural to the language.
- Clojure
- Clojure
- Cobol
- C#
- Dart
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Lua
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- VB
- VB
(some-> str {"foo" foo "bar" bar "baz" baz "barfl" barfl} (.call))
map is a function of a key, which return value for that key, if any.
There is case, but unlike map {} it is opaque, and you can't see which functions are whitelisted in runtime.
.call is a java.util.concurrent.Callable/call
There is case, but unlike map {} it is opaque, and you can't see which functions are whitelisted in runtime.
.call is a java.util.concurrent.Callable/call
(def whitelist #{#'foo #'bar #'baz #'barfl})
(some-> str symbol resolve whitelist (.call))
str can contain fully qualified function name, like "clojure.string/upper-case".
set #{} is a function of an item: returns it back, if contains it, or nil otherwise.
#' is a literal for var.
some-> takes care of nil checks after each step.
This will throw, if whitelisted var contains not callable (e.g. number) or expects arguments.
set #{} is a function of an item: returns it back, if contains it, or nil otherwise.
#' is a literal for var.
some-> takes care of nil checks after each step.
This will throw, if whitelisted var contains not callable (e.g. number) or expects arguments.
switch (str) {
case "foo":
foo();
break;
case "bar":
bar();
break;
case "baz":
baz();
break;
case "barfl":
barfl();
break;
}
my %proc = map { $_ => \&$_ } qw(foo bar baz barfl);
$proc{$str}->() if exists $proc{$str};
Hash _%proc is assigned pairs of values by iterating over the word list created by qw() using map.
Within the block { }, the pairs are created by using the fat-comma operator (=>) to form keyword/value pairs using the loop element ($_) as the keyword value on the left and to form a subroutine reference (\&$_) on the right.
%proc becomes a dispatch table with entries like 'foo' => \&foo. sub foo must exist.
Note that names are specified without repetition.
Within the block { }, the pairs are created by using the fat-comma operator (=>) to form keyword/value pairs using the loop element ($_) as the keyword value on the left and to form a subroutine reference (\&$_) on the right.
%proc becomes a dispatch table with entries like 'foo' => \&foo. sub foo must exist.
Note that names are specified without repetition.
switch = {'foo': foo,
'bar': bar,
'baz': baz,
'barfl': barfl
}
switch_funct = switch.get(string)
if switch_funct : switch_funct()
Python does not natively support switch statements, but we can use a dictionary to replicate one.
The get() method will return the value of the given key if it exists and None otherwise where foo, bar, baz, and barf1 are all declared functions.
We then can check if the get method returned a value, and if it did, execute the returned function.
The get() method will return the value of the given key if it exists and None otherwise where foo, bar, baz, and barf1 are all declared functions.
We then can check if the get method returned a value, and if it did, execute the returned function.
Select Case str
Case NameOf(Foo)
Foo()
Case NameOf(Bar)
Bar()
Case NameOf(Baz)
Baz()
Case NameOf(Barfl)
Barfl()
End Select
The NameOf() operator evaluates at compile-time to the name of an identifier and helps ensure correctness when refactoring.
VB identifiers are case-insensitive, but the Select Case statement is case-sensitive by default.
The
Option Compare Text statement makes Select Case compare strings case-insensitively.
VB identifiers are case-insensitive, but the Select Case statement is case-sensitive by default.
The
Option Compare Text statement makes Select Case compare strings case-insensitively.