Comments on Programming Scalaurn:uuid:cde03bbf-bdac-30bb-89bf-eba545c8035f2009-08-11T20:58:50-07:00 <div>Traversing, Mapping, Filtering, Folding, and Reducing <p id="para_it_returns_list_1_2_3_">It returns <code class="literal">List(&lt;1&gt;, &lt;2&gt;, &lt;3&gt;, &lt;4&gt;, &lt;5&gt;, &lt;6&gt;)</code>. Note that we had to call <code class="literal">reverse</code> on the result to get back a list in the same order as the input list.</p> <blockquote><p>I think it would help to add a comment stating why you do (list, x) =&gt; ("&lt;" + x + "&gt;") :: list instead of (list, x) =&gt; list :: ("&lt;" + x + "&gt;") </p> <p>since the second option would remove the need for "reverse" - is this to "add to head" and get O(2 * n) - also reverse - instead of add to tail and get O(sqr(n))/2 ? </p> <p>by the way - i am enjoying your book a lot </p> &#8212; shalomDeitch (2009-08-11 08:20:02) </blockquote> </div>urn:uuid:e4ce2302-9241-3c62-805f-ef2b32296badshalomDeitch2009-08-11T08:20:02-07:00Comment on 'Traversing, Mapping, Filtering, Folding, and Reducing' <div>Currying <p id="para_we_ll_revisit_the_curry_meth">We&#8217;ll revisit the <code class="literal">curry</code> method in <a class="xref" href="ch12.html#FunctionTypes" title="Function Types">the section called &#8220;Function Types&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>.</p> <blockquote><p>While we did see the Function.curried method, we didn't see the curry method above. </p> &#8212; jrduncans (2009-07-25 21:20:18) </blockquote> </div>urn:uuid:5408dcba-af10-3bd2-b860-069888b84658jrduncans2009-07-25T21:20:18-07:00Comment on 'Currying' <div>Recap: Functional Component Abstractions <p id="para_http_is_another_good_example_">HTTP is another good example. With a handful of message types and a very simple standard for message content, it set the stage for the Internet revolution. RESTful web services built on top of HTTP are also proving successful as components, but they are just complex enough that care is required to ensure that they work successfully.</p> <blockquote><p>"... required to ensure that they work <strong>successfully</strong>." </p> &#8212; chrislewis (2009-07-18 19:45:02) </blockquote> </div>urn:uuid:0f87f568-725d-36af-9efe-5c1ce4e5d77cchrislewis2009-07-18T19:45:02-07:00Comment on 'Recap: Functional Component Abstractions' <div>Tail Calls and Tail-Call Optimization <p id="fact-script-output">This script produces the same output as before. Now, <code class="literal">factorial</code> does all the work with a nested method, <code class="literal">fact</code>, that is tail recursive because it passes an <code class="literal">accumulator</code> argument to hold the computation in progress. This argument is computed with a multiplication <span class="emphasis"><em>before</em></span> the recursive call to <code class="literal">fact</code>, which is now the very last thing that is done. In our previous implementation, this multiplication was done <span class="emphasis"><em>after</em></span> the call to <code class="literal">fact</code>. When we call <code class="literal">fact(1)</code>, we simply return the accumulated value.</p> <blockquote><p>Same as previous code sample: why not <code>if</code> or <code>case</code> with literal? </p> &#8212; hotdog (2009-07-18 11:14:06) </blockquote> </div>urn:uuid:647c75b1-e854-3ecd-be4b-f1e628a7a897hotdog2009-07-18T11:14:06-07:00Comment on 'Tail Calls and Tail-Call Optimization' <div>Recursion <p id="para_the_output_is_the_same_but_no">The output is the same, but now there are no mutable variables. Recursion not only helps us avoid mutable variables, it is also the most natural way to express some functions, particularly mathematical functions. The recursive definition in our second <code class="literal">factorial</code> is structurally similar to a definition for factorials that you might see in a mathematics book.</p> <blockquote><p>What's the point of pattern matching here? I think it's supposed to make branches easier to read... <code>if (i==1) 1 else i*factorial(i-1)</code> looks much easier to comprehend, isn't it? </p> <p>If you insist on <code>match</code>, what's wrong with <code>case 1 =&gt; 1</code>? Current version looks like a test question :) </p> &#8212; hotdog (2009-07-18 11:12:58) </blockquote> </div>urn:uuid:70553c54-2d68-3907-9600-5850c1ebc32chotdog2009-07-18T11:12:58-07:00Comment on 'Recursion' <div>Traversing, Mapping, Filtering, Folding, and Reducing <p id="para_we_convert_the_pair_string_st">This script produces the following output:</p> <blockquote><p>Thanks. Obsolete "output". will fix. </p> &#8212; deanwampler (2009-07-12 21:52:10) </blockquote> </div>urn:uuid:735c5010-b25e-3d8c-87ce-8bef109571aadeanwampler2009-07-12T21:52:10-07:00Comment on 'Traversing, Mapping, Filtering, Folding, and Reducing' <div>Traversing, Mapping, Filtering, Folding, and Reducing <p id="para_we_convert_the_pair_string_st">This script produces the following output:</p> <blockquote><p>"This script produces the output ArrayBufferAlabama." </p> <p>No, I don't think it does :-) </p> <p>(It produces something like ArrayBuffer((Alabama,10), (Alaska,6) ... ) </p> &#8212; NikolajLindberg (2009-07-09 12:29:38) </blockquote> </div>urn:uuid:f41f7ef6-6294-3c24-a327-1bfb1d5da54eNikolajLindberg2009-07-09T12:29:38-07:00Comment on 'Traversing, Mapping, Filtering, Folding, and Reducing' <div>Implicits <p id="para_we_can_t_modify_richstring_o">We can&#8217;t modify <code class="literal">RichString</code> or <code class="literal">Predef</code> to add an implicit conversion method for our custom <code class="literal">FancyString</code> class. Instead, we define an <code class="literal">object</code> named <code class="literal">FancyString2RichString</code> and define the conversion method in it. We then import the contents of this object and the converter gets invoked implicitly in the last line. The output of this script is the following.</p> <blockquote><p>Since the original example above is for adding methods, I think it makes sense to show that as the first example of implicit conversions. For example, give FancyString a method that doesn't exist in RichString like returning a crc32 of it. </p> &#8212; stevej (2009-06-24 21:08:45) </blockquote> </div>urn:uuid:9e5cba5b-4d11-349c-82de-822b50bc052estevej2009-06-24T21:08:45-07:00Comment on 'Implicits'