<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    And you should do ets:lookup_element *first*<br>
    as that is the normal case<br>
    and it only requires a read lock.<br>
    <br>
    /Sverker<br>
    <br>
    <div class="moz-cite-prefix">On 10/30/2017 06:29 PM, Chandrashekhar
      Mullaparthi wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:E4C7E660-71B3-4AF8-AFEE-C7F46A1E7F6F@gmail.com">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      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!)<br>
      <div>
        <div><span style="background-color: rgba(255, 255, 255, 0);"></span></div>
      </div>
      <div><br>
        On 30 Oct 2017, at 17:20, Chandrashekhar Mullaparthi <<a
          href="mailto:chandrashekhar.mullaparthi@gmail.com"
          moz-do-not-send="true">chandrashekhar.mullaparthi@gmail.com</a>>
        wrote:<br>
        <br>
      </div>
      <blockquote type="cite">
        <div>
          <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>
              On 30 Oct 2017, at 17:05, Mikael Pettersson <<a
                href="mailto:mikpelinux@gmail.com"
                moz-do-not-send="true">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" moz-do-not-send="true">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"
                        moz-do-not-send="true">erlang-questions@erlang.org</a><br>
                      <a
                        href="http://erlang.org/mailman/listinfo/erlang-questions"
                        rel="noreferrer" target="_blank"
                        moz-do-not-send="true">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"
                    moz-do-not-send="true">erlang-questions@erlang.org</a></span><br>
                <span><a
                    href="http://erlang.org/mailman/listinfo/erlang-questions"
                    moz-do-not-send="true">http://erlang.org/mailman/listinfo/erlang-questions</a></span><br>
              </div>
            </blockquote>
          </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"
              moz-do-not-send="true">erlang-questions@erlang.org</a></span><br>
          <span><a
              href="http://erlang.org/mailman/listinfo/erlang-questions"
              moz-do-not-send="true">http://erlang.org/mailman/listinfo/erlang-questions</a></span><br>
        </div>
      </blockquote>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>