Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. /**
  2. Scala implementation: flattening arbitrary nested arrays
  3. */
  4. def flattenArray[T](input: Array[T]): Array[Int] = {
  5. input.flatMap {
  6. case a: Array[_] => flattenArray(a)
  7. case i: Int => Array(i)
  8. }
  9. }
  10.  
  11. //some tests
  12. val a = Array(3, Array(2, 3, 4), 6)
  13. val b = Array()
  14. val c = Array(Array(3,4,5), 1)
  15. val d = Array(Array(3,4,Array(3)), 1)
  16.  
  17. flattenArray2(a)
  18. flattenArray2(b)
  19. flattenArray2(c)
  20. flattenArray2(d)
  21.  
  22. // output:
  23. //res0: Array[Int] = Array(3, 2, 3, 4, 6)
  24. //res1: Array[Int] = Array()
  25. //res2: Array[Int] = Array(3, 4, 5, 1)
  26. //res3: Array[Int] = Array(3, 4, 3, 1)
Add Comment
Please, Sign In to add comment