[erlang-questions] one-time evaluated functions

Mikael Pettersson mikpelinux@REDACTED
Mon Oct 30 18:05:59 CET 2017


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>

> 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
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171030/dfbef2ed/attachment.htm>

More information about the erlang-questions mailing list