1apply(n)                     Tcl Built-In Commands                    apply(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       apply - Apply an anonymous function
9

SYNOPSIS

11       apply func ?arg1 arg2 ...?
12_________________________________________________________________
13

DESCRIPTION

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

EXAMPLES

59       This  shows  how to make a simple general command that applies a trans‐
60       formation to each element of a list.
61              proc map {lambda list} {
62                 set result {}
63                 foreach item $list {
64                    lappend result [apply $lambda $item]
65                 }
66                 return $result
67              }
68              map {x {return [string length $x]:$x}} {a bb ccc dddd}
69                     1:a 2:bb 3:ccc 4:dddd
70              map {x {expr {$x**2 + 3*$x - 2}}} {-4 -3 -2 -1 0 1 2 3 4}
71                     2 -2 -4 -4 -2 2 8 16 26
72
73       The apply command is also useful for defining callbacks for use in  the
74       trace command:
75              set vbl "123abc"
76              trace add variable vbl write {apply {{v1 v2 op} {
77                 upvar 1 $v1 v
78                 puts "updated variable to \"$v\""
79              }}}
80              set vbl 123
81              set vbl abc
82

SEE ALSO

84       proc(n), uplevel(n)
85

KEYWORDS

87       argument, procedure, anonymous function
88
89
90
91Tcl                                                                   apply(n)
Impressum