1apply(n) Tcl Built-In Commands apply(n)
2
3
4
5______________________________________________________________________________
6
8 apply - Apply an anonymous function
9
11 apply func ?arg1 arg2 ...?
12______________________________________________________________________________
13
15 The command apply applies the function func to the arguments arg1 arg2
16 ... and returns the result.
17
18 The function func is a two element list {args body} or a three element
19 list {args body namespace} (as if the list command had been used). The
20 first element args specifies the formal arguments to func. The specifi‐
21 cation of the formal arguments args is shared with the proc command,
22 and is described in detail in the corresponding manual page.
23
24 The contents of body are executed by the Tcl interpreter after the
25 local variables corresponding to the formal arguments are given the
26 values of the actual parameters arg1 arg2 .... When body is being exe‐
27 cuted, variable names normally refer to local variables, which are cre‐
28 ated automatically when referenced and deleted when apply returns. One
29 local variable is automatically created for each of the function's
30 arguments. Global variables can only be accessed by invoking the
31 global command or the upvar command. Namespace variables can only be
32 accessed by invoking the variable command or the upvar command.
33
34 The invocation of apply adds a call frame to Tcl's evaluation stack
35 (the stack of frames accessed via uplevel). The execution of body pro‐
36 ceeds in this call frame, in the namespace given by namespace or in the
37 global namespace if none was specified. If given, namespace is inter‐
38 preted relative to the global namespace even if its name does not start
39 with “::”.
40
41 The semantics of apply can also be described by:
42
43 proc apply {fun args} {
44 set len [llength $fun]
45 if {($len < 2) || ($len > 3)} {
46 error "can't interpret \"$fun\" as anonymous function"
47 }
48 lassign $fun argList body ns
49 set name ::$ns::[getGloballyUniqueName]
50 set body0 {
51 rename [lindex [info level 0] 0] {}
52 }
53 proc $name $argList ${body0}$body
54 set code [catch {uplevel 1 $name $args} res opt]
55 return -options $opt $res
56 }
57
59 This shows how to make a simple general command that applies a trans‐
60 formation to each element of a list.
61
62 proc map {lambda list} {
63 set result {}
64 foreach item $list {
65 lappend result [apply $lambda $item]
66 }
67 return $result
68 }
69 map {x {return [string length $x]:$x}} {a bb ccc dddd}
70 → 1:a 2:bb 3:ccc 4:dddd
71 map {x {expr {$x**2 + 3*$x - 2}}} {-4 -3 -2 -1 0 1 2 3 4}
72 → 2 -2 -4 -4 -2 2 8 16 26
73
74 The apply command is also useful for defining callbacks for use in the
75 trace command:
76
77 set vbl "123abc"
78 trace add variable vbl write {apply {{v1 v2 op} {
79 upvar 1 $v1 v
80 puts "updated variable to \"$v\""
81 }}}
82 set vbl 123
83 set vbl abc
84
86 proc(n), uplevel(n)
87
89 anonymous function, argument, lambda, procedure,
90
91
92
93Tcl apply(n)