Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Scala implementation: flattening arbitrary nested arrays
- */
- def flattenArray[T](input: Array[T]): Array[Int] = {
- input.flatMap {
- case a: Array[_] => flattenArray(a)
- case i: Int => Array(i)
- }
- }
- //some tests
- val a = Array(3, Array(2, 3, 4), 6)
- val b = Array()
- val c = Array(Array(3,4,5), 1)
- val d = Array(Array(3,4,Array(3)), 1)
- flattenArray2(a)
- flattenArray2(b)
- flattenArray2(c)
- flattenArray2(d)
- // output:
- //res0: Array[Int] = Array(3, 2, 3, 4, 6)
- //res1: Array[Int] = Array()
- //res2: Array[Int] = Array(3, 4, 5, 1)
- //res3: Array[Int] = Array(3, 4, 3, 1)
Add Comment
Please, Sign In to add comment