开发者

Flattening a collection of collections

开发者_运维知识库

I am not sure to understand what means the term "flattening" in programming languages. More precisely, what does it mean to "flatten a collection of collections"?

Does it means something like: Collection< Collection< Object >> --> Collection< Object >?


This is some doc.

Also, this might be helpful:

[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] => [1, 2, 3, 4, 5, 6, 7, 8]

I think an informal definition would be "recursively get all the contents of the current collection and put all the contents into one single collection". Of course, the recursively could be ignored, in which case just one layer would be faltten.


Flattening is the process of converting several collections (themselves stored in a single collection) into one single collection that contains all of the items in the collections you had before.

Say you have some lists of random strings:

["apple", "ball"], ["cat", "dog"], ["elephant", "frog"]

Then you store those three lists in a list:

[["apple", "ball"], ["cat", "dog"], ["elephant", "frog"]]

When you flatten that list, you'll end up with one list that contains all of the elements:

["apple", "ball", "cat", "dog", "elephant", "frog"]


It means to create a single collection from all the elements in another collection, regardless of wether those elements are individual items, or collections themselves. So, given something like this:

{{0, 1, 2}, 3, 4, {5, 6}, 7}

Where {0, 1, 2} and {5, 6} are collections, then you would have a resulting array like this:

{0, 1, 2, 3, 4, 5, 6, 7}


To flatten a collection means to place them into a single object.

So if I have an array with two objects that have three elements, String name, String age and Collection Children, where children has a name element and an age element like so

Array

Obj 1: Name: Kevin Age: 27 Children: [{Name: Matt Age: 6}]
Obj 2: Name: Jim Age: 22 Children: [{Name: Jake Age: 3},{Name: Jerry Age: 7}]

Flattened out it would look like:

Obj1: Name: Kevin Age: 27 Child1Name: Matt Child1Age: 6
Obj1: Name: Jim Age: 22 Child1Name: Jake Child1Age: 3 Child2Name: Jerry Child2Age: 7

The difference is that in the first group Obj1 contains an array of objects, while in the second group obj1 is one object with the objects in the children array added as elements.


I would say yes. It can be to remove just one level of collections or all levels of collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜