Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.scalatest.FlatSpec
- class FlattenTest extends FlatSpec {
- "Empty List" should "should flatten to empty list" in {
- val empty = List()
- assert(FlattenUtility.Flatten(empty) == List())
- }
- "Flat list" should "return flat list" in {
- val flat = List(1, 2, 3, 4)
- assert(FlattenUtility.Flatten(flat) == flat)
- }
- "Deep list" should "should return flat list" in {
- val deep = List(List(List(1)))
- assert(FlattenUtility.Flatten(deep) == List(1))
- }
- "Deep and flat list" should "should return flat list" in {
- val both = List(List(1), 2, 3, List(4, List(5)))
- assert(FlattenUtility.Flatten(both) == List(1, 2, 3, 4, 5))
- }
- }
- object FlattenUtility {
- def Flatten(toFlatten : Any) : List[Any] = {
- return toFlatten match {
- case first :: second :: rest => Flatten(first) ++ Flatten(second :: rest)
- case first :: _ => Flatten(first)
- case List() => List()
- case t : Any => List(t)
- }
- }
- }
Add Comment
Please, Sign In to add comment