Logo

Programming-Idioms

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.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
(some-> str {"foo" foo "bar" bar "baz" baz "barfl" barfl} (.call))
(def whitelist #{#'foo #'bar #'baz #'barfl})
(some-> str symbol resolve whitelist (.call))
IDENTIFICATION DIVISION.
PROGRAM-ID. reverse string.
PROCEDURE DIVISION.
    EVALUATE str
       WHEN foo
	  PERFORM foo
       WHEN bar
          PERFORM bar
       WHEN baz
	  PERFORM baz 
       WHEN barfl
	  PERFORM barfl
       WHEN OTHER
	  CONTINUE
    END-EVALUATE.	
STOP RUN.
switch (str)
{
    case nameof(Foo):
        Foo();
        break;
    case nameof(Bar):
        Bar();
        break;
    case nameof(Baz):
        Baz();
        break;
    case nameof(Barfl):
        Barfl();
        break;
}
var myProc = {"foo": foo, 
              "bar": bar, 
              "baz": baz, 
              "barfl": barfl};

myProc[str]?.call();
  select case (str)
  case ("foo")
     call foo
  case ("bar")
     call bar
  case ("baz")
     call baz
  case ("barfl")
     call barfl
  end select
switch str {
case "foo":
	foo()
case "bar":
	bar()
case "baz":
	baz()
case "barfl":
	barfl()
}
case str of
  "foo" -> foo
  "bar" -> bar
  "baz" -> baz
  "barfl" -> barfl
switch (str) {
  case "foo":
    foo();
    break;
  case "bar":
    bar();
    break;
  case "baz":
    baz();
    break;
  case "barfl":
    barfl();
    break;
}
switch (str) {
    case "foo" -> foo();
    case "bar" -> bar();
    case "baz" -> baz();
    case "barfl" -> barfl();
}
switch (str){
	case "bar":
            bar();
            break;
        case "baz":
            baz();
            break;
        case "foo":
            foo();
            break;
        case "barfl":
            barfl();
            break;
        default:
            somethingElse();
    }
local procedures={
 foo=function() end,
 bar=function() end,
 baz=function() end,
 barfl=function() end,
}
local function process(str)
 local procedure=procedures[str]
 if procedure~=nil then
  procedure()
 end
end
process(str)
switch ($str) {
  case "foo":
    foo();
    break;
  case "bar":
    bar();
    break;
  case "baz":
    baz();
    break;
  case "barfl":
    barfl();
    break;
}
case str of
  'foo': foo;
  'bar': bar;
  'baz': baz;
  'barfl': barfl;
end;
my %proc = map { $_ => \&$_ } qw(foo bar baz barfl);
$proc{$str}->() if exists $proc{$str};
switch = {'foo': foo, 
	'bar': bar, 
	'baz': baz, 
	'barfl': barfl
	}

switch_funct = switch.get(string)
if switch_funct : switch_funct()
match str:
    case 'foo': foo()
    case 'bar': bar()
    case 'baz': baz()
    case 'barfl': barfl()
method(str).call if ["foo", "bar", "baz", "barfl"].include?(str)
match str {
    "foo" => foo(),
    "bar" => bar(),
    "baz" => baz(),
    "barfl" => barfl(),
    _ => {}
}
Select Case str
    Case NameOf(Foo)
        Foo()
    Case NameOf(Bar)
        Bar()
    Case NameOf(Baz)
        Baz()
    Case NameOf(Barfl)
        Barfl()
End Select
Imports Microsoft.VisualBasic
CallByName(receiver, str, CallType.Method)