<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Here is the code for the boring solution. </div><div><br></div>case ets:insert_new(EtsTab, {singleton, undefined}) of<div> false -></div><div> ets:lookup_element(<span style="background-color: rgba(255, 255, 255, 0);">EtsTab, singleton, 2);</span></div><div> true -></div><div> Val = evaluate_one_time_function(),</div><div> ets:insert(<span style="background-color: rgba(255, 255, 255, 0);">EtsTab, {singleton, Val}),</span></div><div> Val</div><div> end.</div><div><br></div><div>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. </div><div><br></div><div>Cheers</div><div>Chandru<br><div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);"></span></div></div><div><br><div></div>On 30 Oct 2017, at 17:05, Mikael Pettersson <<a href="mailto:mikpelinux@gmail.com">mikpelinux@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div><div>Yes.</div><div><br></div>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.<br><br></div>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.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 30, 2017 at 4:41 PM, Jarosław Preś <span dir="ltr"><<a href="mailto:jaroslaw.p.75@gmail.com" target="_blank">jaroslaw.p.75@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi,</div><div><br></div><div>Is it possible to make an erlang function be evaluated only once at first call and then return this value each time?</div><div><br></div><div>BR/Jarek</div></div>
<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>erlang-questions mailing list</span><br><span><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a></span><br><span><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a></span><br></div></blockquote></div></body></html>