Tcl apply Command
last modified October 20, 2026
The Tcl apply command creates and evaluates anonymous functions.
These are functions without names that can be created and used on the fly.
Basic Definition
The apply command takes a list containing arguments and body of
a function, plus optional arguments to pass to the function. It then executes
the function immediately.
Syntax: apply {argList body ?namespace?} ?arg1 arg2 ...?. The
argList specifies parameters, body contains the function code, and namespace
is optional.
Simple Anonymous Function
This example shows the most basic usage of apply to create and
execute a simple anonymous function.
set result [apply {{x y} {
expr {$x + $y}
}} 5 3]
puts "The sum is $result"
Here we create an anonymous function that takes two arguments and returns their sum. The function is immediately called with arguments 5 and 3.
Using apply with lmap
The apply command is often used with higher-order functions
like lmap to transform lists. The lambda is a plain value, so the
same one can be applied to every element.
set numbers {1 2 3 4 5}
set squares [lmap x $numbers {
apply {{n} {expr {$n * $n}}} $x
}]
puts "Squares: $squares"
This example squares each number in a list using lmap and an
anonymous function created with apply. The function takes one
argument and returns its square.
Accessing Global State
A lambda has no access to the variables of the code that calls it, but it can
read global variables through the :: prefix.
set multiplier 3
set times {{x} {
expr {$x * $::multiplier}
}}
puts "5 times 3 is [apply $times 5]"
The lambda is stored in the times variable as a plain list of an
argument list and a body. It reads the global multiplier variable
each time it is applied; Tcl lambdas are not true closures and do not capture
variables.
apply with Optional Arguments
Anonymous functions can handle optional arguments with default values.
set greet {{name {greeting "Hello"}} {
return "$greeting, $name!"
}}
puts [apply $greet "John"]
puts [apply $greet "Jane" "Hi"]
This shows an anonymous function with an optional greeting
parameter. When not provided, it defaults to "Hello". The function is
called both with and without the optional argument.
Recursive Anonymous Functions
Anonymous functions can be recursive by using the apply command
within their own body.
set factorial {{n} {
if {$n <= 1} {
return 1
} else {
expr {$n * [apply $::factorial [expr {$n - 1}]]}
}
}}
puts "5! = [apply $factorial 5]"
This implements a recursive factorial function using apply. An
anonymous function has no name to call itself by, so the body applies the
global variable factorial that holds the lambda. The
:: prefix is needed because the body runs in its own scope, where
the variable is not visible. The function therefore works only while that
global variable exists.
apply with Namespace
The optional namespace argument allows control over where the anonymous function executes.
namespace eval myns {
variable secret 42
}
set get_secret {{} {
variable secret
return $secret
} ::myns}
puts "The secret is [apply $get_secret]"
This example creates an anonymous function that executes in a specific
namespace (::myns). The variable secret command inside the body
links the local name to ::myns::secret, so the function can read it
without a qualified name.
Best Practices
- Readability: Keep anonymous functions short for clarity.
- Scope: A lambda sees only its own arguments and local variables; pass data as arguments or use fully qualified names.
- Performance: Keep the lambda in a variable when applying it repeatedly, so its compiled form is reused.
- Documentation: Comment complex anonymous functions.
- Testing: Test anonymous functions like regular ones.
This tutorial covered the Tcl apply command with practical
examples showing its usage for anonymous functions.
Author
List all Tcl Tutorials.