-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1604 from cawpat/master
[SCALA-271] - Convert a Scala List to a Tuple
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...core-collections-4/src/main/scala/com/baeldung/scala/listtotuple/ConvertListToTuple.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _ => ("", "") | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...lections-4/src/test/scala/com/baeldung/scala/listtotuple/ConvertListToTupleUnitTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "!") | ||
} | ||
} |