<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>easy &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/easy/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Tue, 14 Jun 2022 05:44:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Simple Scala Methods for finding Sum and Maximum</title>
		<link>https://codepills.com/simple-scala-methods-for-finding-sum-and-maximum/</link>
					<comments>https://codepills.com/simple-scala-methods-for-finding-sum-and-maximum/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Thu, 09 May 2019 11:16:00 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[match expressions]]></category>
		<category><![CDATA[maximum]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[switch statement]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=992</guid>

					<description><![CDATA[Simple article about two different ways how to find a sum and maximum of elements in a list <a href="https://codepills.com/simple-scala-methods-for-finding-sum-and-maximum/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>I am learning Scala. I have been confronted with the simple task of creating functions for finding the sum and maximum in the list of numbers. A precondition is that both functions should be solved recursively.</p>
<p><span id="more-992"></span></p>
<p>There are at least two different ways how to solve each function. First, I tried to come up with a recursive approach in the purest possible form.</p>
<p><b>Sum function</b></p>
<pre><code class="language-scala">def sum(xs: List[Int]): Int = {
     if (xs.isEmpty) {
          0
     } else {
          xs.head + sum(xs.tail)
     }
}</code></pre>
<p><b>Max function</b></p>
<pre><code class="language-scala">def max(xs: List[Int]): Int = {
     if (xs.isEmpty) {
          throw new NoSuchElementException
     } else {
          if (xs.tail.isEmpty) {
               return xs.head
          }
          val tailMax = max(xs.tail)
          if (xs.head &gt; tailMax) {
               xs.head
          } else {
               tailMax
          }
     }
}</code></pre>
<p>After that, I searched for the correct answer on the web. However, I remembered that Scala offers pattern matching through something called <code>match expressions</code>. Therefore, it is better to say it is like advanced switch statements, a feature lacking in older versions of Java.</p>
<p><b>Sum function</b></p>
<pre><code class="language-scala">def sum(xs: List[Int]): Int = xs match {
     case List() =&gt; 0
     case _ =&gt; xs.head + sum(xs.tail)
}</code></pre>
<p><b>Max function</b></p>
<pre><code class="language-scala">def max(xs: List[Int]): Int = xs match {
     case List() =&gt; throw new java.util.NoSuchElementException
     case x :: Nil =&gt; x
     case _ =&gt; val tailMax: Int = max(xs.tail);
          if (tailMax &gt; xs.head) tailMax else xs.head
}</code></pre>
<p>If you look better at the improved version with pattern matching, you can recognize that they are just better if statements.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/simple-scala-methods-for-finding-sum-and-maximum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
