Passing a fun that executes in repl to another function fails?

Showing 1-4 of 4 messages
Passing a fun that executes in repl to another function fails? Bill Robertson 2/27/11 6:36 PM
I wrote a timer function which, takes a number of iterations and a
fun, after executing N times, it returns the number of microseconds.
It was doing fine until I gave it the following fun.

168> F = fun() -> gaeqn:evaluate(42,
gaeqn:tokenize(gaeqn:random_chromosome(20))) end.
#Fun<erl_eval.20.67289768>
169> F().
{1.609082951444313e-6,
 [7,"+",6,"*",217909,"-",7,"+",2760820,"/",9],
 621514}

You can see that the fun executes w/o error, but when passing it to
the timer, something bombs.

170> gaeqn:timer(200, F).
** exception error: bad argument in an arithmetic expression
     in function  gaeqn:compute/2
     in call from gaeqn:evaluate/2
     in call from gaeqn:timer/3

The timer code appears to work.  Please see the code snippet below.

172> gaeqn:timer(200, fun() -> now() end).
307
173> N = fun() -> now() end.
#Fun<erl_eval.20.67289768>
174> gaeqn:timer(200, N).
224

Here is the timer code.

timer(N, Fun) when N > 0 ->
    timer(now(), N, Fun).

timer(Start, 0, Fun) ->
    Fun(),
    timer:now_diff(now(), Start);
timer(Start, N, Fun) ->
    Fun(),
    timer(Start, N-1, Fun).

Any ideas why the two wouldn't work together?
Re: Passing a fun that executes in repl to another function fails? Garrett Smith 2/27/11 10:58 PM
Does this fail each and every time you run it? You have a "random"
element in your example -- could that be throwing off an occasional
bad value?

I'd want to know what values were going into gaeqn:compute/2 that were
causing the error before thinking too hard about it. Can you throw in
an io:format/2 call somewhere to print your inputs?

Re: Passing a fun that executes in repl to another function fails? Bill Robertson 2/28/11 8:03 PM
*sheepish admission*  Yup, my problem.  Division by zero in there.
Thanks for the help.  What is it about learning a new language that
makes figuring out your own problems so difficult?  I think you end up
so focused on figuring out about the new stuff that you end up unable
to consider basic problems.

Thanks again.

Mon, Feb 28, 2011 at 2:06 AM
>> That's a good point.  I had run it a few dozen times w/o error by hand.
>> However, the timer is running it significantly many more times, so
>> probably encountering a flaw in the logic.  I will hit it harder tomorrow.


On Feb 28, 1:58 am, Garrett Smith <g...@rre.tt> wrote:
> Does this fail each and every time you run it? You have a "random"
> element in your example -- could that be throwing off an occasional
> bad value?
>
> I'd want to know what values were going into gaeqn:compute/2 that were
> causing the error before thinking too hard about it. Can you throw in
> an io:format/2 call somewhere to print your inputs?
>
> On Sun, Feb 27, 2011 at 8:36 PM, Bill Robertson
>
>
>
>
>
>
>
Re: Passing a fun that executes in repl to another function fails? jwilberding 3/1/11 3:47 AM
I have found that with any new language, there is always that period where you to build up your intuition to decipher what an error is being caused by.

Thanks!
JW