DEV Community

Cover image for Useful Scala Code Snippets
SUDHIR SHARMA
SUDHIR SHARMA

Posted on

2

Useful Scala Code Snippets

Scala is a strong statically typed general-purpose programming language which supports both object-oriented programming and functional programming.
In this article, I am writing some of the code snippets, which are useful in Scala programming language.

Swap two numbers without using third variable

num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
Enter fullscreen mode Exit fullscreen mode

How to get the full month name?

val cal = Calendar.getInstance
val monthName = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault)
println(monthName)
Enter fullscreen mode Exit fullscreen mode

How to append varargs parameters to an ArrayBuffer?

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)
Enter fullscreen mode Exit fullscreen mode

Script to convert strings to uppercase using object

object ConvertUpper {
 def upperfun(strings: String*) = strings.map(_.toUpperCase())
}
println(ConvertUpper.upperfun("Hello", "Hello, world!", "How are you?", "Bye!"))
Enter fullscreen mode Exit fullscreen mode

How to transform an array to a string using mkString?

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)
Enter fullscreen mode Exit fullscreen mode

Reference: 100+ Scala Code Examples

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay