{"id":6293,"date":"2022-12-20T18:58:06","date_gmt":"2022-12-20T21:58:06","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/apply-mann\/"},"modified":"2022-12-20T18:58:06","modified_gmt":"2022-12-20T21:58:06","slug":"apply-mann","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/apply-mann\/","title":{"rendered":"apply (mann)"},"content":{"rendered":"<h1 align=\"center\">apply<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#KEYWORDS\">KEYWORDS<\/a> <\/p>\n<hr>\n<p>______________________________________________________________________________<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">apply \u2212 Apply an anonymous function<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>apply<\/b> <i>func<\/i> ?<i>arg1 arg2 &#8230;<\/i>? ______________________________________________________________________________<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The command <b>apply<\/b> applies the function <i>func<\/i> to the arguments <i>arg1 arg2 &#8230;<\/i> and returns the result.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The function <i>func<\/i> is a two element list <i>{args body}<\/i> or a three element list <i>{args body namespace}<\/i> (as if the <b>list<\/b> command had been used). The first element <i>args<\/i> specifies the formal arguments to <i>func<\/i>. The specification of the formal arguments <i>args<\/i> is shared with the <b>proc<\/b> command, and is described in detail in the corresponding manual page.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The contents of <i>body<\/i> are executed by the Tcl interpreter after the local variables corresponding to the formal arguments are given the values of the actual parameters <i>arg1 arg2 &#8230;<\/i>. When <i>body<\/i> is being executed, variable names normally refer to local variables, which are created automatically when referenced and deleted when <b>apply<\/b> returns. One local variable is automatically created for each of the function\u2019s arguments. Global variables can only be accessed by invoking the <b>global<\/b> command or the <b>upvar<\/b> command. Namespace variables can only be accessed by invoking the <b>variable<\/b> command or the <b>upvar<\/b> command.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The invocation of <b>apply<\/b> adds a call frame to Tcl\u2019s evaluation stack (the stack of frames accessed via <b>uplevel<\/b>). The execution of <i>body<\/i> proceeds in this call frame, in the namespace given by <i>namespace<\/i> or in the global namespace if none was specified. If given, <i>namespace<\/i> is interpreted relative to the global namespace even if its name does not start with \u201c::\u201d.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The semantics of <b>apply<\/b> can also be described by:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">proc apply {fun args} { <br \/> set len [llength $fun] <br \/> if {($len < 2) || ($len > 3)} { <br \/> error &#8220;can\u2019t interpret &#8220;$fun&#8221; as anonymous function&#8221; <br \/> } <br \/> lassign $fun argList body ns <br \/> set name ::$ns::[getGloballyUniqueName] <br \/> set body0 { <br \/> rename [lindex [info level 0] 0] {} <br \/> } <br \/> proc $name $argList ${body0}$body <br \/> set code [catch {uplevel 1 $name $args} res opt] <br \/> return -options $opt $res <br \/> }<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This shows how to make a simple general command that applies a transformation to each element of a list.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">proc map {lambda list} { <br \/> set result {} <br \/> foreach item $list { <br \/> lappend result [<b>apply<\/b> $lambda $item] <br \/> } <br \/> return $result <br \/> } <br \/> map {x {return [string length $x]:$x}} {a bb ccc dddd} <i><br \/> \u2192 1:a 2:bb 3:ccc 4:dddd<\/i> <br \/> map {x {expr {$x**2 + 3*$x &#8211; 2}}} {-4 -3 -2 -1 0 1 2 3 4} <i><br \/> \u2192 2 -2 -4 -4 -2 2 8 16 26<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>apply<\/b> command is also useful for defining callbacks for use in the <b>trace<\/b> command:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">set vbl &#8220;123abc&#8221; <br \/> trace add variable vbl write {<b>apply<\/b> {{v1 v2 op} { <br \/> upvar 1 $v1 v <br \/> puts &#8220;updated variable to &#8220;$v&#8221;&#8221; <br \/> }}} <br \/> set vbl 123 <br \/> set vbl abc<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">proc(n), uplevel(n)<\/p>\n<h2>KEYWORDS <a name=\"KEYWORDS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">anonymous function, argument, lambda, procedure,<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  apply \u2212 Apply an anonymous function <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3783,1],"tags":[2895,2635],"class_list":["post-6293","post","type-post","status-publish","format-standard","hentry","category-n-comandos-tcl-tk","category-sin-categoria","tag-apply","tag-mann"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6293"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6293\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}