<div class="gmail_quote">2011/10/20 Florian Klämpfl <span dir="ltr"><<a href="mailto:florian@freepascal.org">florian@freepascal.org</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
If anybody does not see why increasing complexity without a good reason<br>
should be avoided, I recommend:<br>
<br>
<a href="http://www.youtube.com/watch?v=kYUrqdUyEpI" target="_blank">http://www.youtube.com/watch?v=kYUrqdUyEpI</a><br>
<div><div></div><div class="h5"></div></div></blockquote></div><div><br></div><div><br></div><div>:-)</div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">There is lots of confusion about anonymous functions. The name is misleading, they are called closures in other languages.</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">There was a similar discussion earlier, see :</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><a href="http://www.hu.freepascal.org/lists/fpc-pascal/2010-January/023755.html">http://www.hu.freepascal.org/lists/fpc-pascal/2010-January/023755.html</a></span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">The origin is from functional languages but it is fine to borrow it to OP, the same way classes and object features were borrowed earlier from other languages. Besides I think the closure </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">addition </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">is almost as important.</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">They have some very important uses, like making multithreading easier.</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">I copy text from Delphi help page again here because it looks so cool:</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">---------------------------------------------------------</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Using Code for a Parameter</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Anonymous methods make it easier to write functions and structures </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">parameterized by code, not just values.</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Multithreading is a good application for anonymous methods. if you want to </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">execute some code in parallel,</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">you might have a parallel-for function that </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">looks like this:</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">type</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  TProcOfInteger = reference to procedure(x: Integer);</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">procedure ParallelFor(start, finish: Integer; proc: TProcOfInteger);</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">The ParallelFor procedure iterates a procedure over different threads.</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Assuming this procedure is implemented correctly and efficiently using threads</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">or a thread pool, it could then be easily used to take advantage of multiple</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">processors:</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">procedure CalculateExpensiveThings;</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">var</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  results: array of Integer;</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">begin</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  SetLength(results, 100);</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  ParallelFor(Low(results), High(results), </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">    procedure(i: Integer)                           // \</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">    begin                                           //  \ code block </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">      results[i] := ExpensiveCalculation(i);        //  /  used as parameter</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">    end                                             // /</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">    );</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  // use results</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">  end;</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Contrast this to how it would need to be done without anonymous methods: </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">probably a "task" class with a virtual abstract method, with a concrete </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">descendant for ExpensiveCalculation, and then adding all the tasks to a </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">queue--not nearly as natural or integrated.</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Here, the "parallel-for" algorithm is the abstraction that is being </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">parameterized by code. In the past, a common way to implement this pattern is </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">with a virtual base class with one or more abstract methods; consider the </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">TThread class and its abstract Execute method. However, anonymous methods make </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">this pattern--parameterizing of algorithms and data structures using code--far </span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">easier.</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Regards,</span></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; ">Juha Manninen</span></div>
<div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; background-color: rgb(255, 255, 255); font-size: medium; "><br></span></div>