Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #93 Pass a runnable procedure as parameter

Implement procedure control which receives one parameter f, and runs f.

import std.traits;
void control(alias f)()
if (isCallable!f)
{
    f();
}
void control(void function() f)
{
    f();
}
void control(invocable auto&& f)
{
 f();
}
T control(Func<T> f) {
	return f();
}
module x
  implicit none
contains
  subroutine control(f)
    interface
       subroutine f()
       end subroutine f
    end interface
    call f
  end subroutine control
end module x
func control(f func()) {
	f()
}
control f = f
function control(f){
	f();
}
static void control(Runnable f) {
    f.run();
}
(defun control (f)
   (funcall f))
function control(f)
	f()
end
function control($f) {
    $f();
}
procedure control(f: tprocedure);
begin
  if Assigned(f) then f;
end; 
sub control {
    my $f = shift;
    $f->();
}
from __future__ import print_function
def control(f):
    return f()
def control
    yield
end
fn control(f: impl Fn()) {
    f();
}
(define (control f) (f))

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms