Scala

Functional and object-oriented programming with Scala on the JVM. Covers Akka, Cats, and big data processing.

Scala

Scala - ZIO Basics

ZIO’s core abstraction is ZIO[R, E, A], where R represents the environment (dependencies), E the error type, and A the success value. This explicit encoding of effects makes side effects…

Read more →
Scala

Scala - zip and unzip Operations

• Scala’s zip operation combines two collections element-wise into tuples, while unzip separates a collection of tuples back into individual collections—essential for parallel data processing and…

Read more →
Scala

Scala - Type Inference

Scala’s type inference system operates through a constraint-based algorithm that analyzes expressions and statements to determine types without explicit annotations. Unlike dynamically typed…

Read more →
Scala

Scala - Variables (val vs var)

• Scala enforces immutability by default through val, which creates read-only references that cannot be reassigned after initialization, leading to safer concurrent code and easier reasoning about…

Read more →
Scala

Scala - Vector with Examples

Vector provides a balanced performance profile across different operations. Unlike List, which excels at head operations but struggles with indexed access, Vector maintains consistent performance for…

Read more →
Scala

Scala - While and Do-While Loops

While loops execute a code block repeatedly as long as the condition evaluates to true. The condition is checked before each iteration, meaning the loop body may never execute if the condition is…

Read more →
Scala

Scala - XML Processing

• Scala’s native XML literals allow direct embedding of XML in code with compile-time validation, though this feature is deprecated in favor of external libraries for modern applications

Read more →
Scala

Scala - Trait Mixins and Stacking

When you mix multiple traits into a class, Scala doesn’t arbitrarily choose which method to call when conflicts arise. Instead, it uses linearization to create a single, deterministic inheritance…

Read more →
Scala

Scala - Try/Success/Failure

Scala’s Try type represents a computation that may either result in a value (Success) or an exception (Failure). It’s part of scala.util and provides a functional approach to error handling…

Read more →
Scala

Scala - Tuple with Examples

Tuples are lightweight data structures that bundle multiple values of potentially different types into a single object. Unlike collections such as Lists or Arrays, tuples are heterogeneous—each…

Read more →
Scala

Scala - Type Casting/Conversion

Scala handles numeric conversions through a combination of automatic widening and explicit narrowing. Widening conversions (smaller to larger types) happen implicitly, while narrowing requires…

Read more →
Scala

Scala - Sealed Traits and Classes

Sealed traits restrict where subtypes can be defined. All implementations must exist in the same source file as the sealed trait declaration. This constraint enables powerful compile-time guarantees.

Read more →
Scala

Scala - Set with Examples

Sets are unordered collections that contain no duplicate elements. Scala provides both immutable and mutable Set implementations, with immutable being the default. The immutable Set is part of…

Read more →
Scala

Scala - sortBy and sortWith

The sortBy method transforms each element into a comparable value and sorts based on that extracted value. This approach works seamlessly with any type that has an implicit Ordering instance.

Read more →
Scala

Scala - Stream/LazyList

• Scala’s LazyList (formerly Stream in Scala 2.12) provides memory-efficient processing of potentially infinite sequences through lazy evaluation, computing elements only when accessed

Read more →
Scala

Scala - Partial Functions

A partial function in Scala is a function that is not defined for all possible input values of its domain. Unlike total functions that must handle every input, partial functions explicitly declare…

Read more →
Scala

Scala - partition, span, splitAt

Scala provides three distinct methods for dividing collections: partition, span, and splitAt. Each serves different use cases and has different performance characteristics. Choosing the wrong…

Read more →
Scala

Scala - Random Number Generation

• Scala provides multiple approaches to random number generation through scala.util.Random, Java’s java.util.Random, and java.security.SecureRandom for cryptographically secure operations

Read more →
Scala

Scala - Read CSV File

For simple CSV files without complex quoting or escaping, Scala’s standard library provides sufficient functionality. Use scala.io.Source to read files line by line and split on delimiters.

Read more →
Scala

Scala - Recursion and Tail Recursion

Recursion occurs when a function calls itself to solve a problem by breaking it down into smaller subproblems. In Scala, recursion is the preferred approach over imperative loops for many algorithms,…

Read more →
Scala

Scala - reduce and fold Operations

The reduce operation processes a collection by repeatedly applying a binary function to combine elements. It takes the first element as the initial accumulator and applies the function to…

Read more →
Scala

Scala - Lazy Evaluation (lazy val)

Lazy evaluation postpones computation until absolutely necessary. In Scala, lazy val creates a value that’s computed on first access and cached for subsequent uses. This differs from regular val

Read more →
Scala

Scala - Logging Best Practices

• Structured logging with context propagation beats string concatenation—use SLF4J with Logback and MDC for production-grade systems that need traceability across distributed services

Read more →
Scala

Scala - Operators with Examples

• Scala operators are methods with symbolic names that support both infix and prefix notation, enabling expressive mathematical and logical operations while maintaining type safety

Read more →
Scala

Scala - groupBy with Examples

• The groupBy method transforms collections into Maps by partitioning elements based on a discriminator function, enabling efficient data categorization and aggregation patterns

Read more →
Scala

Scala - Higher-Order Functions

• Higher-order functions in Scala accept functions as parameters or return functions as results, enabling powerful abstraction patterns that reduce code duplication and improve composability

Read more →
Scala

Scala - HTTP Client (sttp/akka-http)

The Scala HTTP client landscape centers on two mature libraries. sttp (Scala The Platform) offers backend-agnostic abstractions, letting you swap implementations without changing client code. Akka…

Read more →
Scala

Scala - If/Else Expressions

Unlike Java or C++ where if/else are statements, Scala treats them as expressions that evaluate to a value. This fundamental difference enables assigning the result directly to a variable without…

Read more →
Scala

Scala - Inheritance and Override

• Scala supports single inheritance with the extends keyword, allowing classes to inherit fields and methods from a parent class while providing compile-time type safety through its sophisticated…

Read more →
Scala

Scala - flatMap vs map Difference

The distinction between map and flatMap centers on how they handle the return values of transformation functions. map applies a function to each element and wraps the result, while flatMap

Read more →
Scala

Scala - Date and Time Operations

The java.time package provides separate classes for dates, times, and combined date-times. Use LocalDate for calendar dates without time information and LocalTime for time without date context.

Read more →
Scala

Scala - Enumerations

Scala 2’s scala.Enumeration exists primarily for Java interoperability. It uses runtime reflection and lacks compile-time type safety.

Read more →
Scala

Scala - Environment Variables

• Scala provides multiple approaches to access environment variables through sys.env, System.getenv(), and property files, each with distinct trade-offs for type safety and error handling

Read more →
Scala

Scala - Companion Objects

• Companion objects enable static-like functionality in Scala while maintaining full object-oriented principles, providing a cleaner alternative to Java’s static members through shared namespace with…

Read more →
Scala

Scala - Concurrent Collections

• Scala’s concurrent collections provide thread-safe operations without explicit locking, using lock-free algorithms and compare-and-swap operations for better performance than synchronized…

Read more →
Scala

Scala - Currying with Examples

Currying converts a function that takes multiple arguments into a sequence of functions, each taking a single argument. Instead of f(a, b, c), you get f(a)(b)(c). This transformation enables…

Read more →
Scala

Scala - Build Tools (SBT) Tutorial

SBT follows a conventional directory layout that separates source code, resources, and build definitions. A minimal project requires only source files, but production projects need explicit…

Read more →
Scala

Scala - By-Name Parameters

• By-name parameters in Scala delay evaluation until the parameter is actually used, enabling lazy evaluation patterns and control structure abstractions without macros or special compiler support.

Read more →
Scala

Scala - Case Classes with Examples

Case classes address the verbosity problem in traditional Java-style classes. A standard Scala class representing a user requires explicit implementations of equality, hash codes, and string…

Read more →
Scala

Scala - Cats Effect Basics

Cats Effect’s IO type represents a description of a computation that produces a value of type A. Unlike eager evaluation, IO suspends side effects until explicitly run, maintaining referential…

Read more →
Scala

Scala - Classes and Objects

Scala classes are more concise than Java equivalents while offering greater flexibility. Constructor parameters become fields automatically when declared with val or var.

Read more →
Scala

Scala - Closures with Examples

A closure is a function that references variables from outside its own scope. When a function captures variables from its surrounding context, it ‘closes over’ those variables, creating a closure….

Read more →
Scala

Scala - Abstract Classes

Abstract classes serve as blueprints for other classes, defining common structure and behavior while leaving specific implementations to subclasses. You declare an abstract class using the abstract

Read more →
Scala

Scala - Akka Actors Basics

The actor model treats actors as the fundamental units of computation. Each actor encapsulates state and behavior, communicating exclusively through asynchronous message passing. When an actor…

Read more →
Scala

Scala - Annotations

• Scala annotations provide metadata for classes, methods, and fields that can be processed at compile-time, runtime, or by external tools, enabling cross-cutting concerns like serialization,…

Read more →
Scala

Scala - Anonymous/Lambda Functions

Anonymous functions, also called lambda functions or function literals, are unnamed functions defined inline. In Scala, these are instances of the FunctionN traits (where N is the number of…

Read more →
Scala

Scala - ArrayBuffer (Mutable Array)

ArrayBuffer is Scala’s resizable array implementation, part of the scala.collection.mutable package. It maintains an internal array that grows automatically when capacity is exceeded, typically…

Read more →