[erlang-questions] How to make this work

Roelof Wobben r.wobben@REDACTED
Wed Aug 12 18:26:59 CEST 2015


Thanks, Now I see what went wrong and how to execute the fun with a 
argument.

Roelof


Op 12-8-2015 om 18:04 schreef Rick Pettit:
> Not sure this is *exactly* what you are after, but…
>
> -module(my_time).
> -export([my_time_func/2]).
>
> my_time_func(F, Arg) ->
>    T0 = erlang:now(),
>    io:format("Fun returned: ~p~n", [F(Arg)]),
>    T1 = erlang:now(),
>    Elapsed = timer:now_diff(T1, T0),
>    io:format("Elapsed time: ~p microseconds~n", [Elapsed]).
>
>
> ===
>
> rpettit-ltm:~ rpettit$ erl
> Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
>
> Eshell V6.4  (abort with ^G)
> 1> c(my_time).
> {ok,my_time}
> 2> my_time:my_time_func(fun(X) -> 2 * X end, 3).
> Fun returned: 6
> Elapsed time: 73 microseconds
> ok
>
> ===
>
> Note that erlang:now/0 is deprecated, the above was just to illustrate one way to work with that fun you are having trouble with.
>
> Again, the above may not *exactly* solve your problem, but the code works and you can feel free to play with it until you are comfortable with what is going on there.
>
> My recommendation would be to use an iterative approach—take something which works, make very small changes and continually test. This way when you break something, you immediately know where to look.
>
> When starting from scratch, build something *small*, make it work, then slowly add to it all the while going back to run tests, verify you didn’t introduce any bugs, etc.
>
> -Rick
>
>> On Aug 12, 2015, at 10:51 AM, Roelof Wobben <r.wobben@REDACTED> wrote:
>>
>> I think I have a too old version because I see this :
>>
>> exception error: undefined function erlang:monotonic_time/0 in function my_time:time_spend/1 (my_time.erl, line 6)
>>
>> I have R16B03,
>>
>> Roelof
>>
>>
>>
>> Op 12-8-2015 om 17:47 schreef Roelof Wobben:
>>> Correct but my question is how I can access the function with a fun.
>>>
>>> Roelof
>>>
>>>
>>> Op 12-8-2015 om 17:39 schreef Dmitry Kolesnikov:
>>>> Hello,
>>>>
>>>> You can use built-in function
>>>> http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1
>>>>
>>>> You can also check from OTP source the implementation of this function and compare it with your's
>>>> https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160
>>>>
>>>> It gives you hints :-)
>>>>
>>>> Best Regards,
>>>> Dmitry
>>>>
>>>>> On 12 Aug 2015, at 18:34, Roelof Wobben <r.wobben@REDACTED> wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> Im trying this exercise from the programming erlang book.
>>>>>
>>>>> Look up the definitions of erlang:now/0, erlang:date/0, and erlang:time/0. Write a
>>>>> function called my_time_func(F), which evaluates the fun F and times how
>>>>> long it takes.
>>>>>
>>>>> So I did this :
>>>>>
>>>>> -module(my_time).
>>>>>
>>>>> -export( [time_spend/1] ).
>>>>>
>>>>> time_spend(F) ->
>>>>>     Begintime = time:now(),
>>>>>     F(3),
>>>>>     Endtime = time:now(),
>>>>>     Endtime - Begintime.
>>>>>
>>>>>
>>>>> but then when I do this in erl :
>>>>>
>>>>> 11> my_time:time_spend(fun x -> 2 * X end).
>>>>> * 1: syntax error before: '->'
>>>>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>>>>> * 1: syntax error before: '->'
>>>>>
>>>>> how can I make this work ?
>>>>>
>>>>> Roelof
>>>>>
>>>>>
>>>>> ---
>>>>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>>>>> https://www.avast.com/antivirus
>>>>>
>>>>> _______________________________________________
>>>>> erlang-questions mailing list
>>>>> erlang-questions@REDACTED
>>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>> ---
>>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>>> https://www.avast.com/antivirus
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus




More information about the erlang-questions mailing list