Re: my immature thoughts on perl
On Tue, Apr 04, 2023 at 06:50:02AM +0200, tomas@tuxteam.de wrote:
> In Tcl, OTOH, EIAS (Everything Is A String), so you've got
> to eval strings (don't take me too seriously: modern Tcl
> cheats, and it's more "Everything looks like a string",
> but I disgress).
The introduction of the {*} operator eliminated a lot of the need for
eval. Instead of
eval myfunc $listvar
now, you can do
myfunc {*}$listvar
which "expands" the list variable and passes each element as a separate
argument. This is much cleaner, because it lets you decide where and
when you want to perform that expansion. eval forces you to expand
everything in the command where it's used.
In the context of this thread, you can do things like:
% namespace import ::tcl::mathop::+
% + 1 2 3 4
10
% set nums {1 2 3 4}
1 2 3 4
% + {*}$nums
10
% + {*}[lmap x $nums {+ $x 1}]
14
Which is the most elegant way to express the desired piece of code in Tcl,
as far as I'm aware. lmap returns a list, and {*} expands it for the
mathop + command to use.
Reply to: