Skip to content

Commit

Permalink
Merge pull request #1604 from cawpat/master
Browse files Browse the repository at this point in the history
[SCALA-271] - Convert a Scala List to a Tuple
  • Loading branch information
dominiqueplante authored Nov 12, 2024
2 parents 07d23e1 + 852a788 commit 558c080
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.scala.listtotuple

object ConvertListToTuple {
def twoElementsToTuple(list: List[String]): (String, String) = {
val first :: second :: _ = list
(first, second)
}

def twoElementsToTupleUsingMatch(list: List[String]): (String, String) = {
list match {
case first :: second :: _ => (first, second)
case _ => ("", "")
}
}

def unknownSizeToTuple(list: List[String]): Tuple = {
list match {
case first :: second :: third :: _ => (first, second, third)
case first :: second :: _ => (first, second)
case first :: _ => Tuple1(first)
case _ => ("", "")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung.scala.listtotuple

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class ConvertListToTupleUnitTest extends AnyFlatSpec with Matchers {
"twoElementsToTuple" should "convert list with 2 elements" in {
val testList = List("Hello", "world")
ConvertListToTuple.twoElementsToTuple(testList) shouldBe ("Hello", "world")
}

"twoElementsToTuple" should "convert list with 3 elements to Tuple3" in {
val testList = List("Hello", "world", "!")
ConvertListToTuple.twoElementsToTuple(testList) shouldBe ("Hello", "world")
}

"twoElementsToTupleUsingMatch" should "convert list with 2 elements" in {
val testList = List("Hello", "world")
ConvertListToTuple.twoElementsToTupleUsingMatch(
testList
) shouldBe ("Hello", "world")
}

"twoElementsToTupleUsingMatch" should "convert list with 3 elements to tuple2 ignoring extra elements" in {
val testList = List("Hello", "world", "!")
ConvertListToTuple.twoElementsToTupleUsingMatch(
testList
) shouldBe ("Hello", "world")
}

"twoElementsToTupleUsingMatch" should "return empty Strings for 1 element" in {
val testList = List("Hello")
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("", "")
}

"twoElementsToTupleUsingMatch" should "return empty Strings for Nil" in {
val testList = Nil
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("", "")
}

"unknownSizeToTuple" should "return empty Strings for Nil" in {
val testList = Nil
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe ("", "")
}

"unknownSizeToTuple" should "convert list of 1 element to tuple1" in {
val testList = List("Hello")
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe Tuple1("Hello")
}

"unknownSizeToTuple" should "convert list of 2 elements to tuple2" in {
val testList = List("Hello", "world")
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe ("Hello", "world")
}

"unknownSizeToTuple" should "convert list of 3 elements to tuple3" in {
val testList = List("Hello", "world", "!")
ConvertListToTuple.unknownSizeToTuple(
testList
) shouldBe ("Hello", "world", "!")
}
}

0 comments on commit 558c080

Please sign in to comment.