Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import org.scalatest.FlatSpec
  2.  
  3. class FlattenTest extends FlatSpec {
  4. "Empty List" should "should flatten to empty list" in {
  5. val empty = List()
  6. assert(FlattenUtility.Flatten(empty) == List())
  7. }
  8.  
  9. "Flat list" should "return flat list" in {
  10. val flat = List(1, 2, 3, 4)
  11. assert(FlattenUtility.Flatten(flat) == flat)
  12. }
  13.  
  14. "Deep list" should "should return flat list" in {
  15. val deep = List(List(List(1)))
  16. assert(FlattenUtility.Flatten(deep) == List(1))
  17. }
  18.  
  19. "Deep and flat list" should "should return flat list" in {
  20. val both = List(List(1), 2, 3, List(4, List(5)))
  21. assert(FlattenUtility.Flatten(both) == List(1, 2, 3, 4, 5))
  22. }
  23. }
  24.  
  25. object FlattenUtility {
  26. def Flatten(toFlatten : Any) : List[Any] = {
  27. return toFlatten match {
  28. case first :: second :: rest => Flatten(first) ++ Flatten(second :: rest)
  29. case first :: _ => Flatten(first)
  30. case List() => List()
  31. case t : Any => List(t)
  32. }
  33. }
  34. }
Add Comment
Please, Sign In to add comment