<div dir="ltr">Esempio 1<div style>I lambda in python sono verbosi e visto che i vari map, reduce, filter, ... non sono metodi della collezione, si compongono in modo che io percepisco come innaturale.</div><div style>Ad esempio, diciamo che abbiamo un array di interi e vogliamo somarne i cubi.</div>
<div style><br></div><div style>Ruby:</div><div style><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">>> a = [1, 7, 22, 3]</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">=> [1, 7, 22, 3]</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">>> a.map{|x| x*x*x }.reduce{|sum, x| sum + x}</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">=> 11019</span><br></div><div style><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)"><br>
</span></div><div style>Scala:</div><div style><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">scala> val a = Array(1, 7, 22, 3)</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">a: Array[Int] = Array(1, 7, 22, 3)</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)"><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">scala> a.map(x => x*x*x).reduce((sum, x) => sum+x)</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">res1: Int = 11019</span><br></div><div style><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)"><br>
</span></div><div style>Python:</div><div style><span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">>>> a = [1, 7, 22, 3]</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">>>> reduce(lambda sum, x: sum+x, map(lambda x: x*x*x, a))</span><br style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">
<span style="color:rgb(64,64,64);font-family:Roboto,arial,sans-serif;font-size:13px;line-height:18px;background-color:rgb(248,248,248)">11019</span><br></div><div><br></div><div style>Rendo l'idea? Python non solo e' il piu' verboso (perche' devi ripetere "lambda"), ma non ti consente di comporre i metodi naturalmente (in questo python e' piu' FP-oriented degli altri, ma faccio piu' fatica a leggerlo, immaginatevi di comporre 4 o 5 manipolazioni o piu', che e' una cosa che si fa abbastanza naturalmente in Scala o Ruby (o Smalltalk, che poi e' il precursore di questo stile).</div>
<div style><br></div><div style>Ciao</div><div style>Giuliano</div><div style><br></div><div style>PS: vi da' fastidio il top posting per una cosa cosi'?</div><div style>PPS: so che Python ha la funzione sum(), immaginatevi un'altra operazione analoga che abbia senso con una reduce... :)</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/9/17 Marco Beri <span dir="ltr"><<a href="mailto:marcoberi@gmail.com" target="_blank">marcoberi@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">2013/9/17 Piergiuliano Bossi <span dir="ltr"><<a href="mailto:pgbossi@gmail.com" target="_blank">pgbossi@gmail.com</a>></span><div><br><div class="gmail_extra"><div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div>quando devo manipolare una collezione penso a quanto piu' facile e' in Ruby o Scala</div></div></blockquote><div><br></div></div><div>Giuliano,</div><div>magari puoi aggiungere due righe di codice per mostrare le differenze e la maggior facilitą di gestire una collezione in Ruby o Scala?</div>


<div><br></div><div>Se posso capire i conati per il self non riesco davvero a immaginare un modo molto pił facile per gestire le collezioni di quello di Python.</div><div><br></div><div>Ciao.</div><div>Marco.</div><div>P.S. Ovviamente salto tutta la fase dei saluti ecc. visto che ci frequentiamo da anni (in fondo sei tu il protagonista di <a href="http://beri.it/2009/12/22/la-neve/" target="_blank">http://beri.it/2009/12/22/la-neve/</a> che mi ha fatto andare su Radio24 <a href="http://beri.it/2012/03/19/voi-siete-qui/" target="_blank">http://beri.it/2012/03/19/voi-siete-qui/</a>).</div>
<span class="HOEnZb"><font color="#888888">

<div><br></div></font></span></div><span class="HOEnZb"><font color="#888888">-- <br><div><div><div><div><a href="http://beri.it/" target="_blank">http://beri.it/</a> - Un blog</div><div><a href="http://beri.it/i-miei-libri/" target="_blank">http://beri.it/i-miei-libri/</a> - Qualche libro</div>


<div><br></div></div></div></div>
</font></span></div></div></div>
<br>_______________________________________________<br>
Python mailing list<br>
<a href="mailto:Python@lists.python.it">Python@lists.python.it</a><br>
<a href="http://lists.python.it/mailman/listinfo/python" target="_blank">http://lists.python.it/mailman/listinfo/python</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Piergiuliano Bossi<br>Blog: <a href="http://thinkingbox.wordpress.com/" target="_blank">http://thinkingbox.wordpress.com/</a><br>Twitter: <a href="http://twitter.com/thinkingbox" target="_blank">http://twitter.com/thinkingbox</a> (English)<br>
Twitter: <a href="http://twitter.com/scatolapensante" target="_blank">http://twitter.com/scatolapensante</a> (Italiano)<br><div>Google+: <a href="https://plus.google.com/u/0/108187981162465525118" target="_blank">https://plus.google.com/u/0/108187981162465525118</a></div>

</div>