<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SUDHIR SHARMA</title>
    <description>The latest articles on DEV Community by SUDHIR SHARMA (@sudhirniv).</description>
    <link>https://dev.to/sudhirniv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F910837%2F6a195a88-cf85-4947-b66e-2b781635d097.jpg</url>
      <title>DEV Community: SUDHIR SHARMA</title>
      <link>https://dev.to/sudhirniv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sudhirniv"/>
    <language>en</language>
    <item>
      <title>Useful Scala Code Snippets</title>
      <dc:creator>SUDHIR SHARMA</dc:creator>
      <pubDate>Thu, 18 Aug 2022 04:00:56 +0000</pubDate>
      <link>https://dev.to/sudhirniv/useful-scala-code-snippets-f9a</link>
      <guid>https://dev.to/sudhirniv/useful-scala-code-snippets-f9a</guid>
      <description>&lt;p&gt;Scala is a strong statically typed general-purpose programming language which supports both object-oriented programming and functional programming.&lt;br&gt;
In this article, I am writing some of the code snippets, which are useful in Scala programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Swap two numbers without using third variable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to get the full month name?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val cal = Calendar.getInstance
val monthName = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault)
println(monthName)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to append varargs parameters to an ArrayBuffer?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val arrBuf = ArrayBuffer[String]()

// Adding one element
arrBuf += "New Delhi"

// Creating a method with a varargs parameter
def appendMultipleVarArgs(strings: ArrayBuffer[String], varArguments: String*): Unit = 
    arrBuf.appendAll(varArguments)

// Adding multiple varargs parameters
appendMultipleVarArgs(arrBuf, "Mumbai", "Bhopal")

// Printing elements
println(arrBuf)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Script to convert strings to uppercase using object&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object ConvertUpper {
 def upperfun(strings: String*) = strings.map(_.toUpperCase())
}
println(ConvertUpper.upperfun("Hello", "Hello, world!", "How are you?", "Bye!"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to transform an array to a string using mkString?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val arr = Array(10,20,30)

var result = arr.mkString
println(result)

result = arr.mkString(",")
println(result)

result = arr.mkString(" ")
println(result)

result = arr.mkString("(", ",", ")")
println(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reference: &lt;a href="https://www.includehelp.com/code-examples/scala.aspx?ref=devto"&gt;100+ Scala Code Examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>scala</category>
      <category>codes</category>
    </item>
  </channel>
</rss>
