Re: lambda... again
- From: "slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx>
- Date: Mon, 22 Oct 2007 18:22:27 -0700
On Oct 23, 6:48 am, yuriy.ers...@xxxxxxxxx wrote:
Yes, I have read about ithttp://wiki.tcl.tk/519http://wiki.tcl.tk/10782
Why didn't anyone use this obvious variant?
proc lambda {p b} {
set name [list lambda $p $b]
if {[info procs $name] eq ""} {
proc $name $p $b
}
return $name
}
General lambda use cases work well:
% [lambda {s1 s2} {puts "s1=$s1 s2=$s2"}] asd cvb
s1=asd s2=cvb
% set aaa [lambda {s1 s2} {puts "s1=$s1 s2=$s2"}]
lambda {s1 s2} {puts "s1=$s1 s2=$s2"}
% $aaa q w
s1=q s2=w
Personally I think the [apply] method that we now have in 8.5 is the
way to go. Lambdas are after all merely values. So there is no reason
to treat a lambda as anything other than a string until you want to
use it as a function. So:
# In tcl a lambda is simply a string:
set aaa {{s1 s2} {puts "s1=$s1 s2=$s2"}}
# Only when you want to use it does it need special treatment:
apply $aaa q w
The advantage is that unlike the proc work-arounds the apply method
has all the advantages of regular variables: local scope, passable as
first class citizens etc.
The disadvantage is that calling a lambda is different from calling a
regular proc (you need to prepend apply to it). Now... if someone
feels like modifying [unknown] so that lambdas can be called like a
proc..... ;-)
.
- Follow-Ups:
- Re: lambda... again
- From: Yuriy Ershov
- Re: lambda... again
- From: suchenwi
- Re: lambda... again
- References:
- lambda... again
- From: yuriy . ershov
- lambda... again
- Prev by Date: Re: New to this tcl lark... Why won't the exec do what I want?
- Next by Date: Re: exec issues with globbing
- Previous by thread: lambda... again
- Next by thread: Re: lambda... again
- Index(es):
Relevant Pages
|