Runes

Runes are code snippets, each encapsulating a unique coding concept or technique. Drawing their name from alchemical symbols, Runes offer compact, substantive coding knowledge, enriching your skillset and facilitating a deeper comprehension of cutting-edge technologies.

Decomposing data structures into constituent parts using Scala's deconstructive pattern matching.
def deconstructList(xs: List[Int]): List[Int] = xs match
    case Nil => Nil
    // Deconstruct a list into head and tail using the cons :: operator.
    // Then, reverse order by concatenating head to tail.
    case y :: ys => deconstructList(ys) ++ List(y)

deconstructList(List(1, 2, 3, 4))

// res0: List[Int] = List(4, 3, 2, 1)
Scala
Collectible Rune No. 0006
Signaling unimplemented code sections using Scala's ??? keyword.
def myFun(x: Int, y: Int): Int = 
    val myInt1: Int = x
    val myInt2: Int = y
    ???

myFun(1, 2)

// scala.NotImplementedError: an implementation is missing
Scala
Collectible Rune No. 0005
Enhancing code flexibility and enabling generic programming for diverse data types.
def myGenericAdditionFun[T](x: T, y: T): Int = 
    // Casting for x
    val myMatchX = x match
        case x: Int => x
        case x: Double => x.toInt
        case x: String => x.toInt
        case x: Char => x.asDigit
        case x: Boolean => if (x) 1 else 0

    // Casting for x
    val myMatchY = y match
        case y: Int => y
        case y: Double => y.toInt
        case y: String => y.toInt
        case y: Char => y.asDigit
        case y: Boolean => if (y) 1 else 0

    // Return addition
    myMatchX + myMatchY

myGenericAdditionFun(1, 2) 			// res0: Int = 3
myGenericAdditionFun(1.1, 2.2) 		// res1: Int = 3
myGenericAdditionFun('1', '2') 		// res2: Int = 3
myGenericAdditionFun("1", "2") 		// res3: Int = 3
myGenericAdditionFun(true, false) 	// res4: Int = 1
myGenericAdditionFun(true, true) 	// res5: Int = 2
myGenericAdditionFun(false, false) 	// res6: Int = 0
myGenericAdditionFun(1, '2') 		// res7: Int = 3
myGenericAdditionFun("1", '2') 		// res8: Int = 3
myGenericAdditionFun(1.4, '2') 		// res9: Int = 3
Scala
Collectible Rune No. 0004
Extracting elements from a collection of objects using Scala's for-comprehensions.
// Defining a case class
case class Book(title: String, author: List[String])

// Creating Book instances
val book1: Book = Book("War & Peace", List("Leo Tolstoy"))
val book2: Book = Book("Crime & Punishment", List("Fyodor Dostoievski"))
val book3: Book = Book("Rebecca", List("Daphne du Maurier"))
val book4: Book = Book("Les Miserables", List("Victor Hugo"))
val book5: Book = Book("The Count of Monte Cristo", List("Charles Dumas"))
val book6: Book = Book("The Passion According to G.H.", List("Clarice Lispector"))
val book7: Book = Book("Ham on Rye", List("Charles Bukowski"))
val book8: Book = Book("The Brothers Karamazov", List("Fyodor Dostoievski"))

// Defining a list of books
val myBooks: List[Book] = List(book1, book2, book3, book4, book5, book6, book7, book8)

// Getting all books starting with "The"
for
    b <- myBooks
    if b.title.startsWith("The")
yield b.title

// res0: List[String] = List(The Count of Monte Cristo, The Passion According to G.H., The Brothers Karamazov)
Scala
Collectible Rune No. 0003
Determining the maximum value in a collection via a recursive Scala function.
def recursiveMax(xs: List[Int]): Int = 
    def compareMax(x: Int, y: Int): Int = 
        if (x > y) x
        else y
    if (xs.isEmpty) 0
    else compareMax(xs.head, recursiveMax(xs.tail))

recursiveMax(List(2, 6, 1, 6, 8, 10, 11, 0, 11, 20, 20))

// res0: Int = 20
Scala
Collectible Rune No. 0002
Computing the total sum of a collection via a recursive Scala function.
def recursiveSum(xs: List[Int]): Int = 
    if (xs.isEmpty) 0
    else (xs.head + recursiveSum(xs.tail))

recursiveSum(List(1, 2, 3))

// res0: Int = 6
Scala
Collectible Rune No. 0001

Request Full Resume