Comments on Programming Scalaurn:uuid:e1564837-63f8-360f-96b6-22237dbd750e2009-08-11T20:58:20-07:00 <div>Enumerations <p id="para_days_enumeration_scala_output">Running this script with <strong class="userinput"><code>scala</code></strong> yields the following output.</p> <blockquote><p>thought I mention this, others might run into the same question, on <pre> val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value </pre> </p> <p>in Java Object i, j = new Object() </p> <p>is only an initialization of j, i remain uninitialized. It makes sense that all the enumerations are initialized in scala given the preference for val. </p> &#8212; maddalab (2009-07-29 05:18:01) </blockquote> </div>urn:uuid:cebbda5a-9744-3226-8550-c2c4871cf247maddalab2009-07-29T05:18:01-07:00Comment on 'Enumerations' <div>Pattern Matching <p id="para_instead_consider_using_patter">Instead, consider using pattern matching for those &#8220;rare&#8221; times when you need to extract information in a controlled way. An <code class="literal">unapply</code> method can hide the implementation details of the objects it is decomposing. In fact, the information returned by <code class="literal">unapply</code> might be a transformation of the actual information in the type.</p> <blockquote><p>ehh unapply?? first reference to the method. I had to go look up the scala documentation on extractor objects </p> <p>http://www.scala-lang.org/node/112 </p> <p>to understand that line. </p> &#8212; maddalab (2009-07-29 04:56:47) </blockquote> </div>urn:uuid:434e0803-7553-3cac-a9dc-3a2212b49e5dmaddalab2009-07-29T04:56:47-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_the_case_objects_are_just_si">The <code class="literal">case objects</code> are just singleton objects like we&#8217;ve seen before, but with the special <code class="literal">case</code> behavior. We&#8217;re most interested in the embedded <code class="literal">p @ Person(&#8230;)</code> inside the case clause. We&#8217;re matching on particular kinds of <code class="literal">Person</code> objects inside the enclosing tuple. We also want to assign the <code class="literal">Person</code> to a variable p, so we can use it for printing.</p> <blockquote><p>format from Predef from Console.format is deprecated, also using 2.8 REPL does not result in any output on the REPL </p> <p>Since this is the last example with pattern matching, thought I would mention that I do not recall an example the demonstrates using the result of a match expression. </p> <p>Match expressions return the result of the evaluated case, building on your example I came up with this </p> <p><pre> val allemps = Map(1-&gt;alice, 2-&gt;bob, 3-&gt;charlie) val pooremps = allemps.filter(_ match {case (id, p @ Person(_, <em>, Manager)) =&gt; false case (id, p @ Person(</em>, <em>, </em>)) =&gt; true}) </p> <pre><code> for(e &lt;- pooremps) println(e) </code></pre></pre> &#8212; maddalab (2009-07-29 04:45:15) </blockquote> </div>urn:uuid:1020006e-351e-3ae0-9ec2-0c644a95a0a0maddalab2009-07-29T04:45:15-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_this_type_pattern_match_become">This type of pattern match becomes extremely useful when working with Actors, as we&#8217;ll see later on. Case classes are frequently sent to Actors as messages, and deep pattern matching on an object&#8217;s contents is a convenient way to "parse" those messages.</p> <blockquote><p>hopefully formats better this time <pre> case class Address(location:String, city:String, state:String) case class Person(address:Address, name:String, age:Int) </p> <p>val bhaskar = new Person(new Address("Hookey lane", "Wonderland", "Chocolate Factory"), "Bhaskar", 8726) val dave = new Person(new Address("Mills Road", "Wilmington", "Delaware"), "David", 53553) </p> <p>for(p &lt;- List(bhaskar, dave)){ p match { case Person(Address(_, city, "Chocolate Factory"), name, age) =&gt; println("Whoo hoo " + name + ", " + age + " still lives in Chocolate Factory, never grew up") case Person(Address(_, city, state), name, age) if (age &gt; 10000) =&gt; println(name + " from city " + city + " and state " + state + " is " + age + " ollllldddd ") <br /> case Person(_,_,_) =&gt; println("You are just boring") } } </pre> </p> &#8212; maddalab (2009-07-29 03:21:47) </blockquote> </div>urn:uuid:a16685d8-aa0e-3530-87ff-c39372f15a75maddalab2009-07-29T03:21:47-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_this_type_pattern_match_become">This type of pattern match becomes extremely useful when working with Actors, as we&#8217;ll see later on. Case classes are frequently sent to Actors as messages, and deep pattern matching on an object&#8217;s contents is a convenient way to "parse" those messages.</p> <blockquote><p>The section on case classes repeatedly mentions "deep pattern matching" but the selected example is not different from the ones in previous section and does not demonstrate any deep matching </p> <p>case class Address(location:String, city:String, state:String) case class Person(address:Address, name:String, age:Int) </p> <p>val bhaskar = new Person(new Address("Hookey lane", "Wonderland", "Chocolate Factory"), "Bhaskar", 8726) val dave = new Person(new Address("Mills Road", "Wilmington", "Delaware"), "David", 53553) </p> <p>for(p &lt;- List(bhaskar, dave)){ p match { case Person(Address(_, city, "Chocolate Factory"), name, age) =&gt; println("Whoo hoo " + name + ", " + age + " still lives in Chocolate Factory, never grew up") case Person(Address(_, city, state), name, age) if (age &gt; 10000) =&gt; println(name + " from city " + city + " and state " + state + " is " + age + " ollllldddd ") <br /> case Person(_,_,_) =&gt; println("You are just boring") } } </p> &#8212; maddalab (2009-07-29 03:21:10) </blockquote> </div>urn:uuid:58c99f57-a416-30b0-aece-3370d832a901maddalab2009-07-29T03:21:10-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_we_first_define_a__case_class_">We first define a <span class="emphasis"><em>case class</em></span>, a special type of class that we&#8217;ll learn more about in <a class="xref" href="ch06.html#CaseClasses" title="Case Classes">the section called &#8220;Case Classes&#8221;</a> in <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a>. For now, it will suffice to say that a case class allows for very terse construction of simple objects with some pre-defined methods. Our pattern match then looks for Alice and Bob by inspecting the values passed to the constructor of the <code class="literal">Person</code> case class. Charlie falls through to the catch-all case; even though he has the same <code class="literal">age</code> value as Bob, we&#8217;re matching on the <code class="literal">name</code> property as well.</p> <blockquote><p>I noticed that the use of "default" and "catch-all" is inconsistent, had to go back and read the section, the two words are used to mean the case thing. </p> <p>However I think one should be use for </p> <p>case _ (catch-all) </p> <p>and the other for </p> <p>case Person(name, age) -&gt; default </p> <p>Keeps the ducks lined up, right now they are all over the place </p> &#8212; maddalab (2009-07-29 03:08:05) </blockquote> </div>urn:uuid:b49307d9-9d1b-3b24-83df-a70e251a130emaddalab2009-07-29T03:08:05-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_the_second_case_matches_the_em">The second case matches the empty list, <code class="literal">Nil</code>. It prints an end of line and terminates the recursion.</p> <blockquote><p>Any reason the second case specifically uses Nil ove </p> <p>case List() </p> &#8212; maddalab (2009-07-29 02:51:04) </blockquote> </div>urn:uuid:5a6b6ce8-2c6d-3673-b5e5-ad1b87111071maddalab2009-07-29T02:51:04-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_it_looks_just_like_a_c_style_">It looks just like a C-style <code class="literal">case</code> statement, right? The only difference is the last <code class="literal">case</code> with the underscore &#8216;_&#8217; wild card. It matches anything not defined in the cases above it, so it serves the same purpose as the <code class="literal">default</code> keyword in Java and C# <code class="literal">switch</code> statements.</p> <blockquote><p>I thinks </p> <p>val bools = List(true, false) </p> <p>should be </p> <p>val bools = List(true, false, 1) </p> <p>to demonstrate the catch-all case. </p> <p>In the first case bools is inferred to by List[Boolean] and in the second List[AnyVal] </p> &#8212; maddalab (2009-07-28 04:33:42) </blockquote> </div>urn:uuid:4d7a81eb-643e-3ad0-a33b-2b6ec8103296maddalab2009-07-28T04:33:42-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_yup_that_s_all_that_s_necessa">Yup, that&#8217;s all that&#8217;s necessary. This clean one-liner is possible because of Scala&#8217;s <code class="literal">RichInt</code> class. An <span class="emphasis"><em>implicit conversion</em></span> is invoked by the compiler to convert the <code class="literal">1</code>, an <code class="literal">Int</code>, into a <code class="literal">RichInt</code>. (We&#8217;ll discuss these conversions in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a> and in <a class="xref" href="ch08.html#ImplicitConversions" title="Implicit Conversions">the section called &#8220;Implicit Conversions&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>.) <code class="literal">RichInt</code> defines a <code class="literal">to</code> method that takes another integer and returns an instance of <code class="literal">Range.Inclusive</code>. That is, <code class="literal">Inclusive</code> is a nested class in the <code class="literal">Range</code> <span class="emphasis"><em>companion object</em></span> (a concept we introduced briefly in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a>; see <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a> for details). This subclass of the <span class="emphasis"><em>class</em></span> <code class="literal">Range</code> inherits a number of methods for working with sequences and iterable data structures, including those necessary to use it in a <code class="literal">for</code> loop.</p> <blockquote><p><i>RichInt defines a to method that takes another integer and returns an instance of Range.Inclusive. That is, Inclusive is a nested class in the Range companion object</i> </p> <p>I do not understand how you draw the implication (using That is) that we are using the Range companion object and not the class </p> &#8212; maddalab (2009-07-28 04:22:26) </blockquote> </div>urn:uuid:b4635e9f-8bb4-3874-9e6f-27e437dbebeemaddalab2009-07-28T04:22:26-07:00Comment on 'Other Looping Constructs' <div>Scala if Statements <p id="para_note_that_configfilepath_is_">Note that <code class="literal">configFilePath</code> is now <code class="literal">Unit</code>. (It was <code class="literal">String</code> before.) The type inference picks a type that works for all outcomes of the if expression. <code class="literal">Unit</code> is the only possibility, since no value is one possible outcome.</p> <blockquote><p>This is incorrect (at least in 2.8) </p> <p>the type of the if expression is inferred to be Any and the result could be either the String or Unit. This seem more correct as Unit is value and not a type </p> <p>scala&gt; ().isInstanceOf[Any] <br /> res37: Boolean = true </p> <p>scala&gt; val configFile = new java.io.File("~/.myapprc") configFile: java.io.File = ~/.myapprc </p> <p>scala&gt; val configFilePath = if (configFile.exists()) { | configFile.getAbsolutePath() <br /> | } <br /> configFilePath: Any = () </p> &#8212; maddalab (2009-07-28 02:51:12) </blockquote> </div>urn:uuid:9251f0de-5826-3e47-9b51-6a294a5e2badmaddalab2009-07-28T02:51:12-07:00Comment on 'Scala if Statements' <div>Domain-Specific Languages <p id="para_notice_how_much_this_code_read">Notice how much this code reads like English: &#8220;this should test that in the following scenario&#8221;, &#8220;this value must equal that value&#8221;, and so forth. This example uses the superb Specs library, which effectively provides a DSL for the behavior-driven development testing and engineering methodology. By making maximum use of Scala&#8217;s liberal syntax and rich methods, Specs test suites are readable even by non-developers.</p> <blockquote><p>Currently specs builds with scala 2.7 version and the jar obtained from the specs website cannot be used with scala 2.8 </p> <p>So, tho this example makes sense, there is no way to try it out at this time </p> &#8212; maddalab (2009-07-28 02:24:40) </blockquote> </div>urn:uuid:488a4b08-67b4-3c4a-86cc-630e5249f689maddalab2009-07-28T02:24:40-07:00Comment on 'Domain-Specific Languages' <div>Methods Without Parentheses and Dots <p id="para_to_facilitate_a_variety_of_rea">To facilitate a variety of readable programming styles, Scala is flexible about the use of parentheses in methods. If a method takes no parameters, you can define it without parentheses. Callers must invoke the method without parentheses. If you add empty parentheses, then callers may optionally add parentheses. For example, the <code class="literal">size</code> method for <code class="literal">List</code> has no parentheses, so you write <code class="literal">List(1, 2, 3).size</code>. If you try <code class="literal">List(1, 2, 3).size()</code>, you&#8217;ll get an error. However, the <code class="literal">length</code> method for <code class="literal">String</code> does have parentheses in its definition, so both <code class="literal">"hello".length()</code> and <code class="literal">"hello".length</code> will compile.</p> <blockquote><p>I think it might be useful to note that this allow for flexibility of implementation </p> <p>List(1,2,3).size </p> <p>allows for size to be implemented as either a method or a member field </p> <p>def size = 10 </p> <p>val size = 10 </p> <p>the client would not have to change when the underlying implementation is changed. I think this has been stated at a few places as "Scala's uniform access principle" </p> &#8212; maddalab (2009-07-28 01:49:45) </blockquote> </div>urn:uuid:98af1c29-bc42-3a0e-8cb3-84f8d84eafc0maddalab2009-07-28T01:49:45-07:00Comment on 'Methods Without Parentheses and Dots' <div>Conditional Operators <div class="table" id="conditional-operators"><p class="title"><b>Table&#160;3.1.&#160;Conditional Operators</b></p><div class="table-contents"><table summary="Conditional Operators" border="0"><colgroup><col align="left" /><col align="left" /><col align="left" /></colgroup><thead valign="top"><tr><th align="left" valign="top"> Operator </th><th align="left" valign="top"> Operation </th><th align="left" valign="top"> Description</th></tr></thead><tbody valign="top"><tr><td align="left" valign="top"><p><code class="literal">&amp;&amp;</code></p></td><td align="left" valign="top"><p>and</p></td><td align="left" valign="top"><p>The values on the left and right of the operator are true. The right-hand side is <span class="emphasis"><em>only</em></span> evaluated if the left-hand side is <span class="emphasis"><em>true</em></span>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">||</code></p></td><td align="left" valign="top"><p>or</p></td><td align="left" valign="top"><p>At least one of the values on the left or right is true. The right-hand side is <span class="emphasis"><em>only</em></span> evaluated if the left-hand side is <span class="emphasis"><em>false</em></span>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">&gt;</code></p></td><td align="left" valign="top"><p>greater than</p></td><td align="left" valign="top"><p>The value on the left is greater than the value on the right.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">&#8658;</code></p></td><td align="left" valign="top"><p>greater than or equals</p></td><td align="left" valign="top"><p>The value on the left is greater than or equal to the value on the right.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">&lt;</code></p></td><td align="left" valign="top"><p>less than</p></td><td align="left" valign="top"><p>The value on the left is less than the value on the right.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">&#8656;</code></p></td><td align="left" valign="top"><p>less than or equals</p></td><td align="left" valign="top"><p>The value on the left is less than or equal to the value on the right.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">==</code></p></td><td align="left" valign="top"><p>equals</p></td><td align="left" valign="top"><p>The value on the left is the same as the value on the right.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">!=</code></p></td><td align="left" valign="top"><p>not equal</p></td><td align="left" valign="top"><p>The value on the left is not the same as the value on the right.</p></td></tr></tbody></table></div></div> <blockquote><p>The greater than or equals operator should be &gt;=, not =&gt;, I guess. </p> &#8212; jannic (2009-07-26 04:38:19) </blockquote> </div>urn:uuid:e92b6f71-f197-3dbb-aaf3-52bac62bf89fjannic2009-07-26T04:38:19-07:00Comment on 'Conditional Operators' <div>Pattern Matching <p id="para_it_first_matches_on_head_05">It first matches on <code class="literal">head :: tail</code>, where <code class="literal">head</code> will be assigned the first element in the list and <code class="literal">tail</code> will be assigned the rest of the list. That is, we&#8217;re extracting the head and tail from the list using ::. When this case matches, it prints the <code class="literal">head</code> and calls <code class="literal">processList</code> recursively to process the tail.</p> <blockquote><p>There's nothing after "using" in the sentence: "...extracting the head and tail from the list using." </p> &#8212; jrduncans (2009-07-15 14:22:12) </blockquote> </div>urn:uuid:b7f9395a-c224-324b-83ca-efa03faf51a4jrduncans2009-07-15T14:22:12-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_as_it_turns_out_there_s_a_mor">As it turns out, there&#8217;s a more elegant way to loop through collections in Scala, as we&#8217;ll see in the next section.</p> <blockquote><p>This example doesn't loop through a collection, it loops over a range. And we saw looping over a collection in an earlier section. </p> &#8212; jrduncans (2009-07-15 13:55:18) </blockquote> </div>urn:uuid:ddc7d2df-9eee-3f14-a486-e54d8d15f024jrduncans2009-07-15T13:55:18-07:00Comment on 'Other Looping Constructs' <div>Pattern Matching <p id="para_the_case_objects_are_just_si">The <code class="literal">case objects</code> are just singleton objects like we&#8217;ve seen before, but with the special <code class="literal">case</code> behavior. We&#8217;re most interested in the embedded <code class="literal">p @ Person(&#8230;)</code> inside the case clause. We&#8217;re matching on particular kinds of <code class="literal">Person</code> objects inside the enclosing tuple. We also want to assign the <code class="literal">Person</code> to a variable p, so we can use it for printing.</p> <blockquote><p>You could use "p: Person" instead of "p @ Person(_, _, <em>)" for the second case statement. You might also want to either use id or replace it with "</em>". </p> &#8212; GatesDA (2009-07-15 13:19:37) </blockquote> </div>urn:uuid:4d35b8f5-1db3-3e11-92f0-7200e89d1dd2GatesDA2009-07-15T13:19:37-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_however_because_of_demand_for">However, because of demand for it, Scala version 2.8 includes support for <code class="literal">break</code>, implemented as a library method, rather than a built-in break keyword.</p> <blockquote><p>The main text should read well on its own if the notes are moved to a sidebar, skipped by the reader, or otherwise taken out of flow. I would either move the comment on 2.8's break method in with the note, or change the note to be part of the main text. </p> &#8212; GatesDA (2009-07-15 06:47:04) </blockquote> </div>urn:uuid:660c953b-8c38-3a69-abd7-8bd5a49e8bc0GatesDA2009-07-15T06:47:04-07:00Comment on 'Other Looping Constructs' <div>Pattern Matching <p id="para_this_type_pattern_match_become">This type of pattern match becomes extremely useful when working with Actors, as we&#8217;ll see later on. Case classes are frequently sent to Actors as messages, and deep pattern matching on an object&#8217;s contents is a convenient way to "parse" those messages.</p> <blockquote><p>Thanks. will fix. </p> &#8212; deanwampler (2009-07-12 21:14:12) </blockquote> </div>urn:uuid:92fa382a-b770-3809-8c3c-8bcf204705cedeanwampler2009-07-12T21:14:12-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_in_the_second_case_in_this_e">In the second <code class="literal">case</code> in this example, we&#8217;ve extracted the values inside the tuple to scoped variables, then reused these variables in the resulting expression.</p> <blockquote><p>Thanks. will clarify. </p> &#8212; deanwampler (2009-07-12 21:04:22) </blockquote> </div>urn:uuid:df3685c2-82b3-3eb7-b3af-7af9b8c43bbcdeanwampler2009-07-12T21:04:22-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_yup_that_s_all_that_s_necessa">Yup, that&#8217;s all that&#8217;s necessary. This clean one-liner is possible because of Scala&#8217;s <code class="literal">RichInt</code> class. An <span class="emphasis"><em>implicit conversion</em></span> is invoked by the compiler to convert the <code class="literal">1</code>, an <code class="literal">Int</code>, into a <code class="literal">RichInt</code>. (We&#8217;ll discuss these conversions in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a> and in <a class="xref" href="ch08.html#ImplicitConversions" title="Implicit Conversions">the section called &#8220;Implicit Conversions&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>.) <code class="literal">RichInt</code> defines a <code class="literal">to</code> method that takes another integer and returns an instance of <code class="literal">Range.Inclusive</code>. That is, <code class="literal">Inclusive</code> is a nested class in the <code class="literal">Range</code> <span class="emphasis"><em>companion object</em></span> (a concept we introduced briefly in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a>; see <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a> for details). This subclass of the <span class="emphasis"><em>class</em></span> <code class="literal">Range</code> inherits a number of methods for working with sequences and iterable data structures, including those necessary to use it in a <code class="literal">for</code> loop.</p> <blockquote><p>@Mario. True. Will add something about the conversion. @Javier. You're right, although technically foreach and for loops are different constructs. </p> &#8212; deanwampler (2009-07-12 20:56:41) </blockquote> </div>urn:uuid:6cefe52a-d273-3cfa-b183-e15c146c7714deanwampler2009-07-12T20:56:41-07:00Comment on 'Other Looping Constructs' <div>Scala for Comprehensions <p id="para_as_you_might_guess_this_code_">As you might guess, this code says &#8220;for every element in the List <code class="literal">dogBreeds</code>, create a temporary variable called <code class="literal">breed</code> with the value of that element, then print it.&#8221; Think of the <code class="literal">&lt;-</code> operator as an arrow directing elements of a collection, one-by-one, to the scoped variable by which we&#8217;ll refer to them inside the <code class="literal">for</code> expression. The left-arrow operator is called a <span class="emphasis"><em>generator</em></span>, so named because it&#8217;s <span class="emphasis"><em>generating</em></span> individual values from a collection for use in an expression.</p> <blockquote><p>We have examples later on. </p> &#8212; deanwampler (2009-07-12 20:50:48) </blockquote> </div>urn:uuid:58f30bae-cd9b-3433-b36d-89472d435acbdeanwampler2009-07-12T20:50:48-07:00Comment on 'Scala for Comprehensions' <div>Methods Without Parentheses and Dots <p id="para_to_facilitate_a_variety_of_rea">To facilitate a variety of readable programming styles, Scala is flexible about the use of parentheses in methods. If a method takes no parameters, you can define it without parentheses. Callers must invoke the method without parentheses. If you add empty parentheses, then callers may optionally add parentheses. For example, the <code class="literal">size</code> method for <code class="literal">List</code> has no parentheses, so you write <code class="literal">List(1, 2, 3).size</code>. If you try <code class="literal">List(1, 2, 3).size()</code>, you&#8217;ll get an error. However, the <code class="literal">length</code> method for <code class="literal">String</code> does have parentheses in its definition, so both <code class="literal">"hello".length()</code> and <code class="literal">"hello".length</code> will compile.</p> <blockquote><p>Your confirming what the text says. List(...).size is fine, but not List(...).size(). The methods on java.lang.String with no arguments are defined with (), as required by Java, but Scala lets you drop those parentheses, as in your example. </p> &#8212; deanwampler (2009-07-12 20:49:26) </blockquote> </div>urn:uuid:8cae4505-f5c3-30d2-95e2-a19e39226693deanwampler2009-07-12T20:49:26-07:00Comment on 'Methods Without Parentheses and Dots' <div>Operator? Operator? <dt id="varlistentry-pattern-matching-identifiers"><span class="term"> Pattern Matching Identifiers </span></dt> <blockquote><p>Thanks. will fix. </p> &#8212; deanwampler (2009-07-12 20:43:28) </blockquote> </div>urn:uuid:6a31e3ba-d4ab-30f8-9e79-8186665937d6deanwampler2009-07-12T20:43:28-07:00Comment on 'Operator? Operator?' <div>Operator? Operator? <p id="para_here_is_a_summary_of_the_rules">So, what characters can you use in identifiers? Here is a summary of the rules for identifiers, used for method and type names, variables, <span class="emphasis"><em>etc.</em></span> For the precise details, see <a class="xref" href="apa.html#ScalaSpec2009">[ScalaSpec2009]</a>. Scala allows all the printable ASCII characters, such as letters, digits, the underscore &#8216;_&#8217;, and the dollar sign &#8216;$&#8217;, with the exceptions of the &#8220;parenthetical&#8221; characters, &#8216;(&#8217;, &#8216;)&#8217;, &#8216;[&#8217;, &#8216;]&#8217;, &#8216;{&#8217;, &#8216;}&#8217;, and the &#8220;delimiter&#8221; characters &#8216;`&#8217;, &#8216;&#8217;&#8217;, &#8216;'&#8217;, &#8216;"&#8217;, &#8216;.&#8217;, &#8216;;&#8217;, and &#8216;,&#8217;. Scala allows the other characters between \u0020-\u007F that are not in the sets above, such as mathematical symbols and &#8220;other&#8221; symbols. These remaining characters are called <span class="emphasis"><em>operator characters</em></span> and they include characters such as &#8216;/&#8217;, &#8216;&lt;&#8217;, <span class="emphasis"><em>etc.</em></span></p> <blockquote><p>We "informally" list them in the glossary under "operator characters". </p> &#8212; deanwampler (2009-07-12 20:43:08) </blockquote> </div>urn:uuid:22bbefa6-14c4-301e-bce6-85a37cf397acdeanwampler2009-07-12T20:43:08-07:00Comment on 'Operator? Operator?' <div>Operator? Operator? <p id="para_that_plus_sign_between_the_num">That plus sign between the numbers? It&#8217;s a method. First, Scala allows non-alphanumeric method names. You can call methods <code class="literal">+</code>, <code class="literal">-</code>, <code class="literal">_$_</code>, or whatever you desire. Second, this expression is identical to <code class="literal">1.+(2)</code>. When a method takes one argument, Scala lets you drop both the period and the parentheses, so the method invocation looks like an operator invocation. This is called &#8220;infix&#8221; notation, where the operator is between the instance and the argument. We&#8217;ll find out more about this shortly.</p> <blockquote><p>Well, we certainly imply this by saying "between the instance and the argument". </p> &#8212; deanwampler (2009-07-12 20:41:31) </blockquote> </div>urn:uuid:3823137d-6f3f-35b1-9a12-ca8178e6b720deanwampler2009-07-12T20:41:31-07:00Comment on 'Operator? Operator?' <div>Pattern Matching <p id="para_here_we_pull_each_element_out_">Here we pull each element out of a <code class="literal">List</code> of <code class="literal">Any</code> type of element, in this case containing a <code class="literal">String</code>, a <code class="literal">Double</code>, an <code class="literal">Int</code>, and a <code class="literal">Char</code>. For the first three of those types, we let the user know specifically which type we got and what the value was. When we get something else (the <code class="literal">Char</code>), we just let the user know the value. We could add further elements to the list of other types and they&#8217;d be caught by the <code class="literal">other</code> wild card case.</p> <blockquote><p>You might want to add at least one operation unique to one of the types to make it crystal clear that using a variable gives you some benefit. Like change the 's' case to this: </p> <p>case s: String =&gt; println("got a String of length " + s.length) </p> <p>Then call attention to the fact that sundry.length doesn't compile but s.length does. </p> &#8212; wrburdick (2009-07-09 03:00:51) </blockquote> </div>urn:uuid:1f48c1ec-f679-300d-82ac-cab6941efa2ewrburdick2009-07-09T03:00:51-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_in_this_example_we_assign_the">In this example, we assign the wild card case to a variable called <code class="literal">otherNumber</code>, then print it in the subsequent expression. If we generate a seven, we&#8217;ll extoll that number&#8217;s virtues. Otherwise, we&#8217;ll curse fate for making us suffer an unlucky number.</p> <blockquote><p>I think the use of variables in "Matching on Type" example is clearer than the above, because you gain something by using a variable there (like being able to perform string operations). It was confusing to me what the difference between 'otherNumber' and 'randomInt' was. </p> &#8212; wrburdick (2009-07-09 02:56:33) </blockquote> </div>urn:uuid:3dddd218-4bef-39d6-8900-82660f69cdeawrburdick2009-07-09T02:56:33-07:00Comment on 'Pattern Matching' <div>Methods Without Parentheses and Dots <p id="para_to_facilitate_a_variety_of_rea">To facilitate a variety of readable programming styles, Scala is flexible about the use of parentheses in methods. If a method takes no parameters, you can define it without parentheses. Callers must invoke the method without parentheses. If you add empty parentheses, then callers may optionally add parentheses. For example, the <code class="literal">size</code> method for <code class="literal">List</code> has no parentheses, so you write <code class="literal">List(1, 2, 3).size</code>. If you try <code class="literal">List(1, 2, 3).size()</code>, you&#8217;ll get an error. However, the <code class="literal">length</code> method for <code class="literal">String</code> does have parentheses in its definition, so both <code class="literal">"hello".length()</code> and <code class="literal">"hello".length</code> will compile.</p> <blockquote><p>List(1,2,3).size does not result in an error. Scala allows you omit () parenthesis. For example, scala&gt; "hello World".toLowerCase.toUpperCase res3: java.lang.String = HELLO WORLD </p> &#8212; ajaykumarns (2009-07-07 00:10:19) </blockquote> </div>urn:uuid:efcc9df5-067f-3ae8-9e17-20a2e85dffbfajaykumarns2009-07-07T00:10:19-07:00Comment on 'Methods Without Parentheses and Dots' <div>Other Looping Constructs <p id="para_you_can_find_a_table_of_the_co">You can find a table of the conditional operators that work in <code class="literal">while</code> loops below.</p> <blockquote><p>I'm puzzled; why don't all conditional operators work in while loops? Or do you cover that later as well? </p> &#8212; diathesis (2009-06-30 08:48:22) </blockquote> </div>urn:uuid:ca5c355f-56e8-3bec-8bdc-44ccd433c50adiathesis2009-06-30T08:48:22-07:00Comment on 'Other Looping Constructs' <div>Pattern Matching <p id="para_this_type_pattern_match_become">This type of pattern match becomes extremely useful when working with Actors, as we&#8217;ll see later on. Case classes are frequently sent to Actors as messages, and deep pattern matching on an object&#8217;s contents is a convenient way to "parse" those messages.</p> <blockquote><p>is <em>a</em> convenient way </p> &#8212; robcd (2009-06-20 11:12:04) </blockquote> </div>urn:uuid:99dcf3d1-b612-35ab-8b4d-b33d1cd19844robcd2009-06-20T11:12:04-07:00Comment on 'Pattern Matching' <div>Operator? Operator? <dt id="varlistentry-pattern-matching-identifiers"><span class="term"> Pattern Matching Identifiers </span></dt> <blockquote><p>reckon you meant 'that <em>begin</em> with' instead of 'being with' - both times </p> &#8212; robcd (2009-06-20 08:56:12) </blockquote> </div>urn:uuid:fbd68002-9530-3ca9-9875-98eae7f45676robcd2009-06-20T08:56:12-07:00Comment on 'Operator? Operator?' <div>Other Looping Constructs <p id="para_yup_that_s_all_that_s_necessa">Yup, that&#8217;s all that&#8217;s necessary. This clean one-liner is possible because of Scala&#8217;s <code class="literal">RichInt</code> class. An <span class="emphasis"><em>implicit conversion</em></span> is invoked by the compiler to convert the <code class="literal">1</code>, an <code class="literal">Int</code>, into a <code class="literal">RichInt</code>. (We&#8217;ll discuss these conversions in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a> and in <a class="xref" href="ch08.html#ImplicitConversions" title="Implicit Conversions">the section called &#8220;Implicit Conversions&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>.) <code class="literal">RichInt</code> defines a <code class="literal">to</code> method that takes another integer and returns an instance of <code class="literal">Range.Inclusive</code>. That is, <code class="literal">Inclusive</code> is a nested class in the <code class="literal">Range</code> <span class="emphasis"><em>companion object</em></span> (a concept we introduced briefly in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a>; see <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a> for details). This subclass of the <span class="emphasis"><em>class</em></span> <code class="literal">Range</code> inherits a number of methods for working with sequences and iterable data structures, including those necessary to use it in a <code class="literal">for</code> loop.</p> <blockquote><p>"That's all that's necessary" is misleading, the i variable is not necessary, you can do just </p> <p>(1 to 10) foreach println </p> &#8212; javiervegeas (2009-06-14 16:03:02) </blockquote> </div>urn:uuid:34e9809d-2c48-3d2d-9f7a-0e3fb02acf0ejaviervegeas2009-06-14T16:03:02-07:00Comment on 'Other Looping Constructs' <div>Pattern Matching <p id="para_in_the_second_case_in_this_e">In the second <code class="literal">case</code> in this example, we&#8217;ve extracted the values inside the tuple to scoped variables, then reused these variables in the resulting expression.</p> <blockquote><p>Consider clarifying the below Tip portion "or a meaningfully-named variable" to explain that it must not have a type declaration, the lack of type declaration is what allows it to be bound to anything. </p> &#8212; stevej (2009-06-10 22:20:10) </blockquote> </div>urn:uuid:294f4c25-bcc2-3858-95cc-52910cedbde3stevej2009-06-10T22:20:10-07:00Comment on 'Pattern Matching' <div>Operator? Operator? <p id="para_here_is_a_summary_of_the_rules">So, what characters can you use in identifiers? Here is a summary of the rules for identifiers, used for method and type names, variables, <span class="emphasis"><em>etc.</em></span> For the precise details, see <a class="xref" href="apa.html#ScalaSpec2009">[ScalaSpec2009]</a>. Scala allows all the printable ASCII characters, such as letters, digits, the underscore &#8216;_&#8217;, and the dollar sign &#8216;$&#8217;, with the exceptions of the &#8220;parenthetical&#8221; characters, &#8216;(&#8217;, &#8216;)&#8217;, &#8216;[&#8217;, &#8216;]&#8217;, &#8216;{&#8217;, &#8216;}&#8217;, and the &#8220;delimiter&#8221; characters &#8216;`&#8217;, &#8216;&#8217;&#8217;, &#8216;'&#8217;, &#8216;"&#8217;, &#8216;.&#8217;, &#8216;;&#8217;, and &#8216;,&#8217;. Scala allows the other characters between \u0020-\u007F that are not in the sets above, such as mathematical symbols and &#8220;other&#8221; symbols. These remaining characters are called <span class="emphasis"><em>operator characters</em></span> and they include characters such as &#8216;/&#8217;, &#8216;&lt;&#8217;, <span class="emphasis"><em>etc.</em></span></p> <blockquote><p>By 'this', I meant all the operator characters. </p> &#8212; stevej (2009-06-10 21:43:31) </blockquote> </div>urn:uuid:dd6fa864-796d-3c0f-b301-d6b251980598stevej2009-06-10T21:43:31-07:00Comment on 'Operator? Operator?' <div>Operator? Operator? <p id="para_here_is_a_summary_of_the_rules">So, what characters can you use in identifiers? Here is a summary of the rules for identifiers, used for method and type names, variables, <span class="emphasis"><em>etc.</em></span> For the precise details, see <a class="xref" href="apa.html#ScalaSpec2009">[ScalaSpec2009]</a>. Scala allows all the printable ASCII characters, such as letters, digits, the underscore &#8216;_&#8217;, and the dollar sign &#8216;$&#8217;, with the exceptions of the &#8220;parenthetical&#8221; characters, &#8216;(&#8217;, &#8216;)&#8217;, &#8216;[&#8217;, &#8216;]&#8217;, &#8216;{&#8217;, &#8216;}&#8217;, and the &#8220;delimiter&#8221; characters &#8216;`&#8217;, &#8216;&#8217;&#8217;, &#8216;'&#8217;, &#8216;"&#8217;, &#8216;.&#8217;, &#8216;;&#8217;, and &#8216;,&#8217;. Scala allows the other characters between \u0020-\u007F that are not in the sets above, such as mathematical symbols and &#8220;other&#8221; symbols. These remaining characters are called <span class="emphasis"><em>operator characters</em></span> and they include characters such as &#8216;/&#8217;, &#8216;&lt;&#8217;, <span class="emphasis"><em>etc.</em></span></p> <blockquote><p>This would be great to list in an appendix. </p> &#8212; stevej (2009-06-10 21:41:47) </blockquote> </div>urn:uuid:89b9f910-1cc7-3071-b47b-f66944e71b24stevej2009-06-10T21:41:47-07:00Comment on 'Operator? Operator?' <div>Other Looping Constructs <p id="para_yup_that_s_all_that_s_necessa">Yup, that&#8217;s all that&#8217;s necessary. This clean one-liner is possible because of Scala&#8217;s <code class="literal">RichInt</code> class. An <span class="emphasis"><em>implicit conversion</em></span> is invoked by the compiler to convert the <code class="literal">1</code>, an <code class="literal">Int</code>, into a <code class="literal">RichInt</code>. (We&#8217;ll discuss these conversions in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a> and in <a class="xref" href="ch08.html#ImplicitConversions" title="Implicit Conversions">the section called &#8220;Implicit Conversions&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>.) <code class="literal">RichInt</code> defines a <code class="literal">to</code> method that takes another integer and returns an instance of <code class="literal">Range.Inclusive</code>. That is, <code class="literal">Inclusive</code> is a nested class in the <code class="literal">Range</code> <span class="emphasis"><em>companion object</em></span> (a concept we introduced briefly in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a>; see <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a> for details). This subclass of the <span class="emphasis"><em>class</em></span> <code class="literal">Range</code> inherits a number of methods for working with sequences and iterable data structures, including those necessary to use it in a <code class="literal">for</code> loop.</p> <blockquote><p>When mentioning RichInt, should'nt there be also a short explanation on how that Int gets converted into RichInt (that is using Predefs given implicits / implicit type conversion) ? </p> &#8212; magle (2009-06-10 08:06:10) </blockquote> </div>urn:uuid:9f945a87-e527-3ffe-8d95-7b678b6bd8b3magle2009-06-10T08:06:10-07:00Comment on 'Other Looping Constructs' <div>Scala for Comprehensions <p id="para_as_you_might_guess_this_code_">As you might guess, this code says &#8220;for every element in the List <code class="literal">dogBreeds</code>, create a temporary variable called <code class="literal">breed</code> with the value of that element, then print it.&#8221; Think of the <code class="literal">&lt;-</code> operator as an arrow directing elements of a collection, one-by-one, to the scoped variable by which we&#8217;ll refer to them inside the <code class="literal">for</code> expression. The left-arrow operator is called a <span class="emphasis"><em>generator</em></span>, so named because it&#8217;s <span class="emphasis"><em>generating</em></span> individual values from a collection for use in an expression.</p> <blockquote><p>Maybe you could note, that it's possible to apply more than one Generator within a single for comprehension, like for( friend &lt;- petFriends; breed &lt;- dogBreeds; ... ){ ... } </p> &#8212; magle (2009-06-10 07:56:39) </blockquote> </div>urn:uuid:cbb5258c-b80d-3b7f-acce-dece34a205aamagle2009-06-10T07:56:39-07:00Comment on 'Scala for Comprehensions' <div>Operator? Operator? <p id="para_that_plus_sign_between_the_num">That plus sign between the numbers? It&#8217;s a method. First, Scala allows non-alphanumeric method names. You can call methods <code class="literal">+</code>, <code class="literal">-</code>, <code class="literal">_$_</code>, or whatever you desire. Second, this expression is identical to <code class="literal">1.+(2)</code>. When a method takes one argument, Scala lets you drop both the period and the parentheses, so the method invocation looks like an operator invocation. This is called &#8220;infix&#8221; notation, where the operator is between the instance and the argument. We&#8217;ll find out more about this shortly.</p> <blockquote><p>There's another constraint for droping the period and the parentheses: the receiver of the method have to be mentioned explicitly. </p> <p>E.g. it's not possible to call a method inside a class in the above way ( like '+ 2' within class Int), without explicitly using 'this' as the receiver of the method (thus only valid if used like 'this + 2'). This is also true for imported methods of objects - you can't write 'println "ho"', but also have to mention the receiver explicitly. </p> &#8212; magle (2009-06-10 04:37:51) </blockquote> </div>urn:uuid:69993d72-b184-320d-a4b3-378877c40544magle2009-06-10T04:37:51-07:00Comment on 'Operator? Operator?' <div>Enumerations <p id="para_we_ll_revisit_enumerations_in_">Case classes (see <a class="xref" href="ch06.html#CaseClasses" title="Case Classes">the section called &#8220;Case Classes&#8221;</a> in <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a>) are often used instead of enumerations in Scala, because the &#8220;use case&#8221; for them often involves pattern matching. We&#8217;ll revisit this topic in <a class="xref" href="ch13.html#EnumerationsVsPatternMatching" title="Enumerations vs. Pattern Matching">the section called &#8220;Enumerations vs. Pattern Matching&#8221;</a> in <a class="xref" href="ch13.html" title="Chapter&#160;13.&#160;Application Design">Chapter&#160;13, <i>Application Design</i></a>.</p> <blockquote><p>Fixed the cross reference. Will add note about enums vs. case classes. </p> &#8212; deanwampler (2009-06-07 18:59:54) </blockquote> </div>urn:uuid:8a7bb9de-2644-39cc-bd97-a8fedb755fbcdeanwampler2009-06-07T18:59:54-07:00Comment on 'Enumerations' <div>Pattern Matching <p id="para_pattern_matching_aside_scala_">Pattern matching aside, Scala&#8217;s treatment of exception handling should be familiar to those fluent in Java, Ruby, Python, and most other mainstream languages. And yes, you throw an exception by writing <code class="literal">throw new MyBadException(&#8230;)</code>. That&#8217;s all there is to it.</p> <blockquote><p>Good point. Will add a statement to that effect. </p> &#8212; deanwampler (2009-06-07 18:55:05) </blockquote> </div>urn:uuid:c16c5cd5-d10e-3a4b-8be5-c9fea2a19d8ddeanwampler2009-06-07T18:55:05-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_finally_let_s_try_a__deep_mat">Let&#8217;s try a <span class="emphasis"><em>deep match</em></span>, examining the contents of objects in our pattern match.</p> <blockquote><p>Thought more about it; will add a "note" to encourage thinking more careful about why you might have "none of the above" and make sure it's really the right thing to do. </p> &#8212; deanwampler (2009-06-11 06:39:36) </blockquote> </div>urn:uuid:5d4b4fad-383a-3ca8-8197-44d2a6fe2beedeanwampler2009-06-11T06:39:36-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_finally_let_s_try_a__deep_mat">Let&#8217;s try a <span class="emphasis"><em>deep match</em></span>, examining the contents of objects in our pattern match.</p> <blockquote><p>Will clarify the wording. </p> &#8212; deanwampler (2009-06-11 06:39:36) </blockquote> </div>urn:uuid:c3089ee6-93ca-36b8-98af-0d6bc5ca2cc6deanwampler2009-06-11T06:39:36-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_in_the_second_case_of_we_ve_">In the second <code class="literal">case</code> of we&#8217;ve used a special wild card pattern to match a <code class="literal">List</code> of any size, even zero elements, and any element values. You can use this pattern at the end of any sequence match to remove length as a condition.</p> <blockquote><p>Will fix. Thanks. </p> &#8212; deanwampler (2009-06-07 18:47:06) </blockquote> </div>urn:uuid:a4df853c-3c80-3993-a32c-ad129c348e21deanwampler2009-06-07T18:47:06-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_an_idea_borrowed_from_function">An idea borrowed from functional languages, <span class="emphasis"><em>pattern matching</em></span> is a powerful yet concise way to make a programmatic choice between multiple conditions. Pattern matching is the familiar <code class="literal">case</code> statement from your favorite C-like language, but on steroids. In the typical <code class="literal">case</code> statement you&#8217;re limited to matching against values of ordinal types, yielding trivial expressions like this: "in the case that <code class="literal">i</code> is 5, print a message; in the case that <code class="literal">i</code> is 6, exit the program". With Scala&#8217;s pattern matching, your cases can include types, wild-cards, sequences, and even deep inspections of an object&#8217;s variables.</p> <blockquote><p>We get into this later. </p> &#8212; deanwampler (2009-06-07 18:46:36) </blockquote> </div>urn:uuid:f5419477-c92c-36c3-bc41-d313131db0dbdeanwampler2009-06-07T18:46:36-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_you_can_find_a_table_of_the_co">You can find a table of the conditional operators that work in <code class="literal">while</code> loops below.</p> <blockquote><p>Good point! We like the example, so we'll add a warning comment! </p> &#8212; deanwampler (2009-06-07 18:45:31) </blockquote> </div>urn:uuid:ce1a25dc-d6d8-3c57-a368-42ce99d798f5deanwampler2009-06-07T18:45:31-07:00Comment on 'Other Looping Constructs' <div>Scala for Comprehensions <p id="para_every_time_through_the_for_e">Every time through the <code class="literal">for</code> expression, the filtered result is yielded as a value named <code class="literal">breed</code>. These results accumulate with every run, and the resulting collection is assigned to the value <code class="literal">filteredBreeds</code> (as we did with <code class="literal">if</code> statements above). The type of the collection resulting from a <code class="literal">for-yield</code> expression is inferred from the type of the collection being iterated over. In this case, <code class="literal">filteredBreeds</code> is of type <code class="literal">List[String]</code>, since it is a subset of the <code class="literal">dogBreeds</code> list, which is also of type <code class="literal">List[String]</code>.</p> <blockquote><p>Will expand the tip briefly. Short answer, people almost always use {}. </p> &#8212; deanwampler (2009-06-07 18:44:46) </blockquote> </div>urn:uuid:29b559a4-412a-3b05-82c0-74bf286d5c13deanwampler2009-06-07T18:44:46-07:00Comment on 'Scala for Comprehensions' <div>Scala for Comprehensions <p id="para_another_familiar_control_struc">Another familiar control structure that&#8217;s particularly feature-rich in Scala is the <code class="literal">for</code> loop, referred to in the Scala community as a <code class="literal">for</code> <span class="emphasis"><em>comprehension</em></span> or <code class="literal">for</code> <span class="emphasis"><em>expression</em></span>. This corner of the language deserves at least one fancy name, because it can do some great party tricks.</p> <blockquote><p>Will add a brief description </p> &#8212; deanwampler (2009-06-07 18:38:39) </blockquote> </div>urn:uuid:bccb867c-93f9-3c88-ad94-03c69ce0ddc6deanwampler2009-06-07T18:38:39-07:00Comment on 'Scala for Comprehensions' <div>Scala if Statements <p id="para_because_if_statements_are_ex">Because <code class="literal">if</code> statements are expressions in Scala, there is no need for the special-case ternary conditional expressions that exists in C-derived languages. You won&#8217;t see <code class="literal">x ? doThis() : doThat()</code> in Scala. Scala provides a mechanism that&#8217;s just as powerful and more readable.</p> <blockquote><p>Reworded a bit. </p> &#8212; deanwampler (2009-06-07 18:38:12) </blockquote> </div>urn:uuid:dbf2e02e-9a62-3cd6-96b4-1509e6592e00deanwampler2009-06-07T18:38:12-07:00Comment on 'Scala if Statements' <div>Scala if Statements <p id="para_note_that_if_statements_are_">Note that <code class="literal">if</code> statements are expressions, meaning they have values. In this example, the value <code class="literal">configFilePath</code> is the result of an <code class="literal">if</code> expression that handles the case of a configuration file not existing internally, then returns the absolute path to that file. This value can now be reused throughout an application, and the <code class="literal">if</code> expression won&#8217;t be re-evaluated when the value is used.</p> <blockquote><p>Thanks, both of you, for these comments. Will clarify. </p> &#8212; deanwampler (2009-06-07 18:10:55) </blockquote> </div>urn:uuid:40997890-6e53-3bf3-8183-1593c05ff9b6deanwampler2009-06-07T18:10:55-07:00Comment on 'Scala if Statements' <div>Methods Without Parentheses and Dots <p id="para_so_if_an_expression_like_2_0">So, if an expression like <code class="literal">2.0 * 4.0 / 3.0 * 5.0</code> is actually a series of method calls on <code class="literal">Doubles</code>, what are the <span class="emphasis"><em>operator precedence</em></span> rules? Here they are in order from lowest to highest precedence <a class="xref" href="apa.html#ScalaSpec2009">[ScalaSpec2009]</a>.</p> <blockquote><p>Yea, it did. Will fix. </p> &#8212; deanwampler (2009-06-07 18:09:56) </blockquote> </div>urn:uuid:99995199-042b-3130-a53b-950f65887afedeanwampler2009-06-07T18:09:56-07:00Comment on 'Methods Without Parentheses and Dots' <div>Pattern Matching <p id="para_an_idea_borrowed_from_function">An idea borrowed from functional languages, <span class="emphasis"><em>pattern matching</em></span> is a powerful yet concise way to make a programmatic choice between multiple conditions. Pattern matching is the familiar <code class="literal">case</code> statement from your favorite C-like language, but on steroids. In the typical <code class="literal">case</code> statement you&#8217;re limited to matching against values of ordinal types, yielding trivial expressions like this: "in the case that <code class="literal">i</code> is 5, print a message; in the case that <code class="literal">i</code> is 6, exit the program". With Scala&#8217;s pattern matching, your cases can include types, wild-cards, sequences, and even deep inspections of an object&#8217;s variables.</p> <blockquote><p>It may be good to mention that pattern matching is different from regular expressions. Readers who haven't been exposed to functional programming may not be aware of pattern matching and may think of this as something to do with regex. </p> &#8212; parthm (2009-06-02 06:23:34) </blockquote> </div>urn:uuid:17a441aa-8f03-35ba-a8a1-55008ca23964parthm2009-06-02T06:23:34-07:00Comment on 'Pattern Matching' <div>Other Looping Constructs <p id="para_you_can_find_a_table_of_the_co">You can find a table of the conditional operators that work in <code class="literal">while</code> loops below.</p> <blockquote><p>You could consider tweaking the above example to be runnable in the REPL. </p> <p>I pasted this one into the REPL before realizing that I can't really run it as it sleeps for a day as indicated in the comment. I had to kill the REPL. </p> &#8212; parthm (2009-06-02 06:15:59) </blockquote> </div>urn:uuid:7c682513-31e0-3c03-b50f-1259fb3025ffparthm2009-06-02T06:15:59-07:00Comment on 'Other Looping Constructs' <div>Scala for Comprehensions <p id="para_every_time_through_the_for_e">Every time through the <code class="literal">for</code> expression, the filtered result is yielded as a value named <code class="literal">breed</code>. These results accumulate with every run, and the resulting collection is assigned to the value <code class="literal">filteredBreeds</code> (as we did with <code class="literal">if</code> statements above). The type of the collection resulting from a <code class="literal">for-yield</code> expression is inferred from the type of the collection being iterated over. In this case, <code class="literal">filteredBreeds</code> is of type <code class="literal">List[String]</code>, since it is a subset of the <code class="literal">dogBreeds</code> list, which is also of type <code class="literal">List[String]</code>.</p> <blockquote><p>Regarding the "Tip" above (parens or braces). Is there a recommendation on what is preferred or common in Scala community? </p> &#8212; parthm (2009-06-02 06:08:21) </blockquote> </div>urn:uuid:37e55903-3f39-369c-9014-2e38b5f24034parthm2009-06-02T06:08:21-07:00Comment on 'Scala for Comprehensions' <div>Scala for Comprehensions <p id="para_another_familiar_control_struc">Another familiar control structure that&#8217;s particularly feature-rich in Scala is the <code class="literal">for</code> loop, referred to in the Scala community as a <code class="literal">for</code> <span class="emphasis"><em>comprehension</em></span> or <code class="literal">for</code> <span class="emphasis"><em>expression</em></span>. This corner of the language deserves at least one fancy name, because it can do some great party tricks.</p> <blockquote><p>Maybe it can be briefly described why <code>for</code> is referred to as "comprehension" and not "loop". </p> &#8212; parthm (2009-06-02 06:01:35) </blockquote> </div>urn:uuid:6176effa-88b5-3235-9213-41a8bd635cfdparthm2009-06-02T06:01:35-07:00Comment on 'Scala for Comprehensions' <div>Methods Without Parentheses and Dots <p id="para_so_if_an_expression_like_2_0">So, if an expression like <code class="literal">2.0 * 4.0 / 3.0 * 5.0</code> is actually a series of method calls on <code class="literal">Doubles</code>, what are the <span class="emphasis"><em>operator precedence</em></span> rules? Here they are in order from lowest to highest precedence <a class="xref" href="apa.html#ScalaSpec2009">[ScalaSpec2009]</a>.</p> <blockquote><blockquote><p><code>operator precedence'' rules? The following</code>scala The markup for the text above seems to have gone bad. </p> </blockquote> &#8212; parthm (2009-06-02 05:52:39) </blockquote> </div>urn:uuid:c975a4f3-e22f-3dfd-bd96-104498af15a3parthm2009-06-02T05:52:39-07:00Comment on 'Methods Without Parentheses and Dots' <div>Pattern Matching <p id="para_in_the_second_case_of_we_ve_">In the second <code class="literal">case</code> of we&#8217;ve used a special wild card pattern to match a <code class="literal">List</code> of any size, even zero elements, and any element values. You can use this pattern at the end of any sequence match to remove length as a condition.</p> <blockquote><p>This example is a bit confusing. The message in the second case is not exact -- a list of [1, 3] would also print this. </p> &#8212; retronym (2009-05-21 04:18:28) </blockquote> </div>urn:uuid:c140602b-211d-30cb-8f95-4f188c1e9a8bretronym2009-05-21T04:18:28-07:00Comment on 'Pattern Matching' <div>Enumerations <p id="para_we_ll_revisit_enumerations_in_">Case classes (see <a class="xref" href="ch06.html#CaseClasses" title="Case Classes">the section called &#8220;Case Classes&#8221;</a> in <a class="xref" href="ch06.html" title="Chapter&#160;6.&#160;Advanced Object-Oriented Programming In Scala">Chapter&#160;6, <i>Advanced Object-Oriented Programming In Scala</i></a>) are often used instead of enumerations in Scala, because the &#8220;use case&#8221; for them often involves pattern matching. We&#8217;ll revisit this topic in <a class="xref" href="ch13.html#EnumerationsVsPatternMatching" title="Enumerations vs. Pattern Matching">the section called &#8220;Enumerations vs. Pattern Matching&#8221;</a> in <a class="xref" href="ch13.html" title="Chapter&#160;13.&#160;Application Design">Chapter&#160;13, <i>Application Design</i></a>.</p> <blockquote><p>Revisit in where? (the chapter number is missing) </p> <p>Btw. you probably should mention that instead of Enumeration, often case classes are used. </p> &#8212; clojure (2009-05-20 13:05:53) </blockquote> </div>urn:uuid:7df853eb-f688-349e-a7eb-204ad89b0d85clojure2009-05-20T13:05:53-07:00Comment on 'Enumerations' <div>Pattern Matching <p id="para_pattern_matching_aside_scala_">Pattern matching aside, Scala&#8217;s treatment of exception handling should be familiar to those fluent in Java, Ruby, Python, and most other mainstream languages. And yes, you throw an exception by writing <code class="literal">throw new MyBadException(&#8230;)</code>. That&#8217;s all there is to it.</p> <blockquote><p>I don't see a clear, explicit mention that exceptions are not checked, which I think is important to know for people coming from Java land. </p> &#8212; clojure (2009-05-20 13:02:01) </blockquote> </div>urn:uuid:273a546f-194b-3ee6-879c-6b8422e9ea28clojure2009-05-20T13:02:01-07:00Comment on 'Pattern Matching' <div>Pattern Matching <p id="para_finally_let_s_try_a__deep_mat">Let&#8217;s try a <span class="emphasis"><em>deep match</em></span>, examining the contents of objects in our pattern match.</p> <blockquote><p>"It is often helpful to include a catch-all case" </p> <p>I don't think _ should be encouraged. If one later adds another case class, there's a good change some method won't be updated. Even if not that, it's better to be explicit... it's way more readable (of course, there are exceptions) </p> &#8212; clojure (2009-06-11 06:39:36) </blockquote> </div>urn:uuid:25941bbf-1955-366e-b7e7-5600ea0a8834clojure2009-06-11T06:39:36-07:00Comment on 'Pattern Matching' <div>Scala if Statements <p id="para_because_if_statements_are_ex">Because <code class="literal">if</code> statements are expressions in Scala, there is no need for the special-case ternary conditional expressions that exists in C-derived languages. You won&#8217;t see <code class="literal">x ? doThis() : doThat()</code> in Scala. Scala provides a mechanism that&#8217;s just as powerful and more readable.</p> <blockquote><p>Huh? How does it clutter up it anymore than if-then-else? Though, I admit the latter being a bit more readable. </p> &#8212; clojure (2009-05-20 12:46:59) </blockquote> </div>urn:uuid:ef0e9629-8154-3bc2-acbd-b73c23ed1aa0clojure2009-05-20T12:46:59-07:00Comment on 'Scala if Statements' <div>Scala if Statements <p id="para_note_that_if_statements_are_">Note that <code class="literal">if</code> statements are expressions, meaning they have values. In this example, the value <code class="literal">configFilePath</code> is the result of an <code class="literal">if</code> expression that handles the case of a configuration file not existing internally, then returns the absolute path to that file. This value can now be reused throughout an application, and the <code class="literal">if</code> expression won&#8217;t be re-evaluated when the value is used.</p> <blockquote><p>"return" is not very good choice of word, I think. "Note that if statement is an expression, which evaluates to a value" sounds better to me. </p> &#8212; clojure (2009-05-20 12:43:57) </blockquote> </div>urn:uuid:eeabf363-6f43-32f1-af88-470d363c4b9fclojure2009-05-20T12:43:57-07:00Comment on 'Scala if Statements' <div>Scala if Statements <p id="para_note_that_if_statements_are_">Note that <code class="literal">if</code> statements are expressions, meaning they have values. In this example, the value <code class="literal">configFilePath</code> is the result of an <code class="literal">if</code> expression that handles the case of a configuration file not existing internally, then returns the absolute path to that file. This value can now be reused throughout an application, and the <code class="literal">if</code> expression won&#8217;t be re-evaluated when the value is used.</p> <blockquote><p>Perhaps point out that this only makes sense in an if / else. Scala allows val x = if true 1; but it returns (). </p> &#8212; retronym (2009-05-20 04:04:33) </blockquote> </div>urn:uuid:eacc2920-72fd-3699-ae10-bed39a550f33retronym2009-05-20T04:04:33-07:00Comment on 'Scala if Statements'