Skip to content

Commit f510d3b

Browse files
Callum GibbonsCallum Gibbons
authored andcommitted
[SCALA-271] - Convert a Scala List to a Tuple
1 parent ba365a9 commit f510d3b

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.scala.listtotuple
2+
3+
import scala.quoted.Expr
4+
5+
object ConvertListToTuple {
6+
def twoElementsToTuple(list: List[String]): (String, String) = {
7+
val first :: second :: _ = list
8+
(first, second)
9+
}
10+
11+
def twoElementsToTupleUsingMatch(list: List[String]): (String, String) = {
12+
list match {
13+
case first :: second :: _ => (first, second)
14+
case _ => ("", "")
15+
}
16+
}
17+
18+
def unknownSizeToTuple(list: List[String]): Tuple = {
19+
list match {
20+
case first :: second :: third :: _ => (first, second, third)
21+
case first :: second :: _ => (first, second)
22+
case _ => ("", "")
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.scala.listtotuple
2+
3+
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
import org.scalatest.matchers.should.Matchers.shouldBe
7+
8+
class ConvertListToTupleUnitTest extends AnyFlatSpec with Matchers {
9+
"twoElementsToTuple" should "convert list with 2 elements" in {
10+
val testList = List("Hello", "world")
11+
ConvertListToTuple.twoElementsToTuple(testList) shouldBe ("Hello", "world")
12+
}
13+
14+
"twoElementsToTuple" should "convert list with 3 elements to Tuple3" in {
15+
val testList = List("Hello", "world", "!")
16+
ConvertListToTuple.twoElementsToTuple(testList) shouldBe ("Hello", "world")
17+
}
18+
19+
"twoElementsToTupleUsingMatch" should "convert list with 2 elements" in {
20+
val testList = List("Hello", "world")
21+
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("Hello", "world")
22+
}
23+
24+
"twoElementsToTupleUsingMatch" should "convert list with 3 elements to tuple2 ignoring extra elements" in {
25+
val testList = List("Hello", "world", "!")
26+
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("Hello", "world")
27+
}
28+
29+
"twoElementsToTupleUsingMatch" should "return empty Strings for 1 element" in {
30+
val testList = List("Hello")
31+
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("", "")
32+
}
33+
34+
"twoElementsToTupleUsingMatch" should "return empty Strings for Nil" in {
35+
val testList = Nil
36+
ConvertListToTuple.twoElementsToTupleUsingMatch(testList) shouldBe ("", "")
37+
}
38+
39+
"unknownSizeToTuple" should "return empty Strings for Nil" in {
40+
val testList = Nil
41+
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe ("", "")
42+
}
43+
44+
"unknownSizeToTuple" should "convert list of 2 elements to tuple2" in {
45+
val testList = List("Hello", "world")
46+
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe ("Hello", "world")
47+
}
48+
49+
"unknownSizeToTuple" should "convert list of 3 elements to tuple3" in {
50+
val testList = List("Hello", "world", "!")
51+
ConvertListToTuple.unknownSizeToTuple(testList) shouldBe ("Hello", "world", "!")
52+
}
53+
}

0 commit comments

Comments
 (0)