0

I have following list structure -

List(List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List({"esx":"192.168.20.52","vm":" naa.60a9800042704577762b45634476337a ","datastore":"","vNic":"","portGroupVLan":"","vSwitch":"","physicalNic":"","lunName":"lun_30102014_101347)","writeIops":44998,"readIops":1635,"latency":47008,"serialNumber":"BpEwv+EcDv3z","usedSize":0,"totalSize":4,"availableSize":4,"throughput":null}, (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List(()), List((), (), ()))

I want to remove all empty lists from above . I want output as -

List({"esx":"192.168.20.52","vm":" naa.60a9800042704577762b45634476337a ","datastore":"","vNic":"","portGroupVLan":"","vSwitch":"","physicalNic":"","lunName":"lun_30102014_101347)","writeIops":44998,"readIops":1635,"latency":47008,"serialNumber":"BpEwv+EcDv3z","usedSize":0,"totalSize":4,"availableSize":4,"throughput":null})

How do I get above output by using scala??

1
  • 4
    Flatten it! That will remove the empty lists whilst collapsing the structure by one dimension. Commented Nov 6, 2014 at 11:42

1 Answer 1

2

If your input is like that, i.e. you only have list(list(list())) depth, a double call to flatten will solve it.

val x = yourList
x.flatten.flatten

if your list has empty lists at various depths, you need to constantly flatten until you reach a fixed point:

val x = yourList
var y = x.flatten
var z = y.flatten
while (y != z) {
   y = z
   z = z.flatten
y

Hope that helps :)

Sign up to request clarification or add additional context in comments.

4 Comments

But I want only non empty list only... Can you see my expected output and input given please
Flattening removes empty lists: List(List(), List(1)).flatten == List(1) I assume that your actual code is not List(()) but List(List()), otherwise you don't have empty lists, you have lists filled with a single instance of the Unit type (VERY different thing).
your while loop will never typecheck.
You are right. My bad. Can you edit my response / add a response that does the same thing but with type check passing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.