[erlang-questions] one-time evaluated functions

Sverker Eriksson sverker.eriksson@REDACTED
Mon Oct 30 22:19:47 CET 2017


And you should do ets:lookup_element *first*
as that is the normal case
and it only requires a read lock.

/Sverker

On 10/30/2017 06:29 PM, Chandrashekhar Mullaparthi wrote:
> Actually this won’t work properly if you have a bunch of requests come 
> in concurrently the first time around. Some of them will see undefined 
> as the result. So you’ll need extra code to sleep and retry if the ets 
> lookup returns undefined (unless undefined is a valid value!)
>
> On 30 Oct 2017, at 17:20, Chandrashekhar Mullaparthi 
> <chandrashekhar.mullaparthi@REDACTED 
> <mailto:chandrashekhar.mullaparthi@REDACTED>> wrote:
>
>> Here is the code for the boring solution.
>>
>> case ets:insert_new(EtsTab, {singleton, undefined}) of
>>     false ->
>>         ets:lookup_element(EtsTab, singleton, 2);
>>     true ->
>>         Val = evaluate_one_time_function(),
>>          ets:insert(EtsTab, {singleton, Val}),
>>         Val
>>     end.
>>
>> You have to make sure that the ETS table is owned by some long lived 
>> process and is of type public if you are going to invoke this 
>> function from multiple processes.
>>
>> Cheers
>> Chandru
>>
>>
>> On 30 Oct 2017, at 17:05, Mikael Pettersson <mikpelinux@REDACTED 
>> <mailto:mikpelinux@REDACTED>> wrote:
>>
>>> Yes.
>>>
>>> 1. (Boring) Have a persistent store somewhere, and have the function 
>>> check if the value is stored there or not.  If not, compute it and 
>>> store.  An ETS table would work fine, but you need to ensure it's 
>>> lifetime matches your requirements.  A plain file containing 
>>> term_to_binary/1 of the value should also work.  Or use a DB.
>>>
>>> 2. (More fun) Place the function in a module M. Initial version 
>>> computes the value and generates a new version of the module where 
>>> the function now just returns that constant value.  There will 
>>> probably be some restrictions on what kinds of values you can return 
>>> this way, i.e. I wouldn't expect a function value with closed over 
>>> free variables to work, but anything you could include in a pattern 
>>> match should work.
>>>
>>> On Mon, Oct 30, 2017 at 4:41 PM, Jarosław Preś 
>>> <jaroslaw.p.75@REDACTED <mailto:jaroslaw.p.75@REDACTED>> wrote:
>>>
>>>     Hi,
>>>
>>>     Is it possible to make an erlang function be evaluated only once
>>>     at first call and then return this value each time?
>>>
>>>     BR/Jarek
>>>
>>>     _______________________________________________
>>>     erlang-questions mailing list
>>>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>>     http://erlang.org/mailman/listinfo/erlang-questions
>>>     <http://erlang.org/mailman/listinfo/erlang-questions>
>>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>> http://erlang.org/mailman/listinfo/erlang-questions
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171030/81008194/attachment.htm>


More information about the erlang-questions mailing list