<div>Well, first, let&#39;s address some misconceptions in Tierry&#39;s email:<br></div><div><br></div><div><span style="background-color:rgb(253,253,253)">&quot;E</span><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)">nums in swift are purely functional and therefore functions without parameters always return the same value in an enum&quot;...is not true. A function without parameters can have any amount of side effects and can return a random value or a copy of a global variable.</span><br></div><div><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)"><br></span></div><div><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)">There are some nice designs out there for memoization in Swift. I recall one in the excellent Advanced Swift, but I could be remembering another source. I&#39;d suggest a discussion on swift-users.</span></div><div><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)"><br></span></div><div><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)">But yes, pure functions are some ways off.</span></div><div><span style="color:rgb(49,49,49);word-spacing:1px;background-color:rgb(255,255,255)"><br></span></div><div><br></div><div><div class="gmail_quote"><div>On Tue, Apr 25, 2017 at 12:19 David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br><br>Sent from my iPhone</div></div><div><div><br>On Apr 25, 2017, at 06:44, Tierry Hörmann via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div style="word-wrap:break-word">Hi all<div><br></div><div>I discovered that memoization is currently a pain to implement when using functional programming.</div><div>E.g. when implementing a lazy linked list, one obviously uses an enum to represent the data. But there is also a wrapper struct needed with lazy variables (especially a lazy property tail), as enums can not have stored properties. Here some example code of such a lazy linked list:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">struct</span><span style="font-variant-ligatures:no-common-ligatures"> LazyList&lt;T&gt; {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> root: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt;</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">init</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> root: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt; = </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt;.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">End</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">root</span><span style="font-variant-ligatures:no-common-ligatures"> = root</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> hd: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">? {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">get</span><span style="font-variant-ligatures:no-common-ligatures"> {</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">root</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">val</span><span style="font-variant-ligatures:no-common-ligatures">()}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">lazy</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">private</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> tail: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt; = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">root</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">tail</span><span style="font-variant-ligatures:no-common-ligatures">()()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> tl: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LazyList</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt; {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">mutating</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">get</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LazyList</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">tail</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">enum</span><span style="font-variant-ligatures:no-common-ligatures"> LLE&lt;T&gt; {</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> End</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">indirect</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> Node(</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">, () -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt;, </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> val() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">? {</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">switch</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">Node</span><span style="font-variant-ligatures:no-common-ligatures">(v, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">):</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> v</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">default</span><span style="font-variant-ligatures:no-common-ligatures">:</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">nil</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_1189542397825839138m_-5679191037068719472webkit-block-placeholder"></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> tail() -&gt; () -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">LLE</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">&gt; {</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">switch</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">case</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">Node</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">, tl, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures">):</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> tl</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">default</span><span style="font-variant-ligatures:no-common-ligatures">:</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">assert</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">false</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;Can&#39;t call tail on empty list&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> {</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">In my opinion this isn’t optimal (and can also create bigger problems, e.g. when implementing count). But enums in swift are purely functional and therefore functions without parameters always return the same value in an enum, so storing the result of a function call with no parameters could in some situations give a great performance boost.</div><div style="margin:0px;line-height:normal">Has this issue already been discussed? If yes, what was the outcome? And if no, I would suggest an additional keyword “memoizing” for functions without parameters in enums. Or maybe someone has a better idea, which would fit better inside the swift universe?</div></span></div></div></div></blockquote><br></div><div><div>We&#39;ve talked about &quot;pure&quot; functions (a prerequisite for memoization) a few times. Usually around the time we start talking about which definition of &quot;pure&quot; we want to use, someone will come by and say it&#39;s out of scope. IIRC, the last time it happened was after we started discussing Swift 4, so I&#39;d guess it&#39;s still out of scope.</div><div><br></div><div>- Dave Sweeris </div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div>