Why is this not possible?

Why is this not possible?

am 11.04.2008 17:30:50 von r.p.levy

#!/bin/sh


# this doesn't work in bash:
#
# usage: $1 is body of function, use inline
# if it worked it would make it possible to simulate an anonymous
function inline
function lambda {
name=tmp$(perl -e "print rand()*(2**50);") # make up unique
function name
$("function $name { $1 }")
echo $name
}

# this works
# shell can do callbacks, but unfortunately no anonymous functions
# usage: function funa { ... }; function funb { ... }; percent funa
funb
function percent {
fun1=$1
fun2=$2
echo $(echo "scale=2;"$($fun1)" / "$($fun2)" * 100" | bc)"%"
}

#example of callbacks
function fun1 { ls *.png | wc -l }
function fun2 { ls *.bmp | wc -l }
percent fun1 fun2
# result is output of percent, the callbacks are not called until the
function uses them

#non working example of anonymous functions as callbacks
# this doesn't work!
percent $(lambda "ls *.png | wc -l;") $(lambda "ls *.bmp | wc -l;")

#wouldn't the anonymous version be much better?

Re: Why is this not possible?

am 11.04.2008 17:35:02 von r.p.levy

Sorry for some reason it got wrapped around and was unreadable here it
is again:

#!/bin/sh

# this doesn't work in bash:
#
# usage: $1 is body of function, use inline
# if it worked it would
# make it possible to
# simulate an
# anonymous function
# inline
function lambda {
name=tmp$(perl -e "print rand()*(2**50);")
$("function $name { $1 }")
echo $name
}

# this works
# shell can do callbacks, but unfortunately no anonymous functions
# usage: function funa { ... }; function funb { ... }; percent funa
funb
function percent {
fun1=$1
fun2=$2
echo $(echo "scale=2;"$($fun1)" / "$($fun2)" * 100" | bc)"%"

}

#example of callbacks
function fun1 { ls *.png | wc -l }
function fun2 { ls *.bmp | wc -l }
percent fun1 fun2
# result is output of percent, the callbacks are not called until the
# function uses them

#non working example of anonymous functions as callbacks
# this doesn't work!
percent $(lambda "ls *.png | wc -l;") $(lambda "ls *.bmp | wc -l;")

#wouldn't the anonymous version be much better?

Re: Why is this not possible?

am 11.04.2008 18:38:25 von PK

r.p.levy@gmail.com wrote:

> #non working example of anonymous functions as callbacks
> # this doesn't work!
> percent $(lambda "ls *.png | wc -l;") $(lambda "ls *.bmp | wc -l;")

You can do this instead:

percent () {
echo $(echo "scale=2;"$(bash -c "$1")" / "$(bash -c "$2")" * 100" | bc)"%"
}

$ percent 'ls *.png | wc -l' 'ls *.bmp | wc -l'
200.00%
$ percent 'seq 1 10 | wc -l' 'cat /etc/issue | wc -l'
76.00%

etc.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: Why is this not possible?

am 13.04.2008 03:52:31 von r.p.levy

On Apr 11, 12:38 pm, pk wrote:
> r.p.l...@gmail.com wrote:
> > #non working example of anonymous functions as callbacks
> > # this doesn't work!
> > percent $(lambda "ls *.png | wc -l;") $(lambda "ls *.bmp | wc -l;")
>
> You can do this instead:
>
> percent () {
> echo $(echo "scale=2;"$(bash -c "$1")" / "$(bash -c "$2")" * 100" | bc)"%"
>
> }
>
> $ percent 'ls *.png | wc -l' 'ls *.bmp | wc -l'
> 200.00%
> $ percent 'seq 1 10 | wc -l' 'cat /etc/issue | wc -l'
> 76.00%
>
> etc.
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.

Thanks! Someone else pointed out that eval works too:

http://tinyurl.com/5dnnbl

Rob