Anonymous functions

Anonymous functions

am 11.04.2008 17:55:34 von r.p.levy

I just posted this question, but I realized I could state the question
more clearly and with less words.

This is the function that I want to write. It defines a new function
and then returns the name that it gave to it. That allows you to
assign callbacks without having to make up meaningless function names:

function lambda {
name=tmp$(perl -e "print rand()*(2**50);")
$("function $name { $1 }")
echo $name
}

Here is the problem: it doesn't allow the evaluation of a new
function definition when the script is running apparently.

$("function $name { $1 }")

Is this a limitation of Shell, or am I just doing it wrong? I'm just
curious about the limitations of Shell, it's not really a practical
problem, because there are other languages to do this in. But it
would add some convenience to shell scripts though.

Thanks,
Rob

Re: Anonymous functions

am 11.04.2008 18:22:32 von Bill Marcum

On 2008-04-11, r.p.levy@gmail.com wrote:
>
>
> I just posted this question, but I realized I could state the question
> more clearly and with less words.
>
> This is the function that I want to write. It defines a new function
> and then returns the name that it gave to it. That allows you to
> assign callbacks without having to make up meaningless function names:
>
> function lambda {
> name=tmp$(perl -e "print rand()*(2**50);")
> $("function $name { $1 }")
> echo $name
> }
>
> Here is the problem: it doesn't allow the evaluation of a new
> function definition when the script is running apparently.
>
> $("function $name { $1 }")
>
> Is this a limitation of Shell, or am I just doing it wrong? I'm just
> curious about the limitations of Shell, it's not really a practical
> problem, because there are other languages to do this in. But it
> would add some convenience to shell scripts though.
>
$( ) invokes a subshell, which cannot change its parent's environment.

Re: Anonymous functions

am 11.04.2008 22:15:36 von Barry Margolin

In article
,
r.p.levy@gmail.com wrote:

> I just posted this question, but I realized I could state the question
> more clearly and with less words.
>
> This is the function that I want to write. It defines a new function
> and then returns the name that it gave to it. That allows you to
> assign callbacks without having to make up meaningless function names:
>
> function lambda {
> name=tmp$(perl -e "print rand()*(2**50);")
> $("function $name { $1 }")
> echo $name
> }
>
> Here is the problem: it doesn't allow the evaluation of a new
> function definition when the script is running apparently.
>
> $("function $name { $1 }")
>
> Is this a limitation of Shell, or am I just doing it wrong? I'm just
> curious about the limitations of Shell, it's not really a practical
> problem, because there are other languages to do this in. But it
> would add some convenience to shell scripts though.

I think what you're looking for is eval:

eval "function $name { $1 }"

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Anonymous functions

am 12.04.2008 18:19:12 von Dan Mercer

"Barry Margolin" wrote in message
news:barmar-1C5C97.16153611042008@newsgroups.comcast.net...
> In article
> ,
> r.p.levy@gmail.com wrote:
>
>> I just posted this question, but I realized I could state the question
>> more clearly and with less words.
>>
>> This is the function that I want to write. It defines a new function
>> and then returns the name that it gave to it. That allows you to
>> assign callbacks without having to make up meaningless function names:
>>
>> function lambda {
>> name=tmp$(perl -e "print rand()*(2**50);")
>> $("function $name { $1 }")
>> echo $name
>> }
>>
>> Here is the problem: it doesn't allow the evaluation of a new
>> function definition when the script is running apparently.
>>
>> $("function $name { $1 }")
>>
>> Is this a limitation of Shell, or am I just doing it wrong? I'm just
>> curious about the limitations of Shell, it's not really a practical
>> problem, because there are other languages to do this in. But it
>> would add some convenience to shell scripts though.
>
> I think what you're looking for is eval:
>
> eval "function $name { $1 }"
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE don't copy me on replies, I'll read them in the group ***

Barry,

Don't enable bad design.

Dan Mercer

Re: Anonymous functions

am 13.04.2008 03:45:21 von r.p.levy

> > I think what you're looking for is eval:
>
> > eval "function $name { $1 }"
>
> Barry,
>
> Don't enable bad design.
>
> Dan Mercer

Dan

Just curious, why is this bad design?
As far as the practical results of this go, it sems to add a level of
convenient abstraction that is otherwise not present. "eval" or "bash
-c" both work. This function can either be used with

$ function percent { echo $(echo "scale=2;"$(eval "$1")" / "$(eval
"$2")" * 100" | bc)"%"; }
$ percent \
"ls *e* | wc -l" \
"ls * | wc -l"
56.00%
$ echo "words ending in ING: " \
$(percent \
'grep -c "ING^M$" twl.txt' \
'cat twl.txt | wc -l)'
7.00%

I think I'll find myself using this new function and others like it
often. It seems like it makes shell slightly more like a functional
programming language, which is what I like most about this idea.

Rob

Re: Anonymous functions

am 13.04.2008 05:48:24 von r.p.levy

There are two aspects that I can think of that one might find flaws
in. One is the idea itself of faking the ability to have callback
functions, and the other is evaluating stuff at run time (including
broken things). I know that 2. might make it harder to debug the code
if something goes wrong with one component, because unlike in
languages that support anonymous functions as part of the semantics of
the language, here it is just an eval, which could be dangerous
potentially if something were broken but the partially composed string
is evaluated. I'm very far from being an expert on anything related to
this, but I think having support in language allows for checking of
the code before run time which makes it safer. On the other hand,
since the shells don't have this, aspect 1 of this makes it possible
to a certain extent, or at least to a great degree than before, think
of shell in terms of functional programming language techniques. But
in a very limited way, unlike with Perl.

Re: Anonymous functions

am 16.04.2008 09:27:12 von pgas

r.p.levy@gmail.com wrote:
>> > I think what you're looking for is eval:
>
> Just curious, why is this bad design?
> As far as the practical results of this go, it sems to add a level of
> convenient abstraction that is otherwise not present. "eval" or "bash
> -c" both work. This function can either be used with
>
> $ function percent { echo $(echo "scale=2;"$(eval "$1")" / "$(eval
> "$2")" * 100" | bc)"%"; }
> $ percent \
> "ls *e* | wc -l" \
> "ls * | wc -l"
> 56.00%
> $ echo "words ending in ING: " \
> $(percent \
> 'grep -c "ING^M$" twl.txt' \
> 'cat twl.txt | wc -l)'
> 7.00%
>
> I think I'll find myself using this new function and others like it
> often. It seems like it makes shell slightly more like a functional
> programming language, which is what I like most about this idea.
>
> Rob

This example doesn't seem to bring any convenience.

percent () { printf "%d%%\n" "$(echo "scale=2;$1/$2*100 | bc)" ;}
percent $(ls *e* | wc -l) $(ls * | wc -l)
echo "words ending in ING: \
$(percent \
$(grep -c "ING^M$" twl.tx') \
$(cat twl.txt | wc -l))"

Passing the result of $( ) instead of the command makes a function
that is easier to read, that does what you would expect (ie percent 10
30 works while with your version you have to do percent "echo 10"
"echo 30".

using $( ) is also more convenient as far as quotes are concerned:
percent $( grep '$var' "$1") .. vs percent "grep '\$var' \"$1\"" ....
People already have trouble with quoting, so if you add another level
of quote removal...

It is probably more efficent to pass just numbers than commands

(not to mention that having the arguments evaluated before calling the
function and using parenthesis make it look more like lisp)

I'm not trying to say that functional programming is bad, but maybe
have a look at the "es" shell or at scsh if you want practical
functional shell scripts.

--
pgas @ SDF Public Access UNIX System - http://sdf.lonestar.org