Open sameerajayasoma opened 5 years ago
concat
, which makes this a bit easier (in conjunction with ...
spread operator). I would do that before this.I think the bar for adding functions to the lang library needs to be quite high. There is precedence for this function:
https://stackoverflow.com/questions/49843262/where-does-the-word-flatmap-originate-from
It’s also easy (and equally declarative) with foreach
expressions proposed by query:
Child[] children =
foreach var team in company.teams
foreach var member in team.members
foreach var child in member.children
select child;
Just to be clear if L is a list of lists, you can flatten it into a list with array:concat(...L)
(same as string:concat
.) But it’s not terribly convenient for this usage in Ballerina because you cannot call it with method syntax; a function called flatten
typically flattens to arbitrary depth, but we cannot write a type for that. What we need is a 1-arg concat, but I’m not sure what to call that.
I would want us to provide the flattening operation before the combination of flattening and mapping. Flattening also makes sense for XML.
This is not possible with the current set of operations in
lang.array
module. Consider the following example. I want to extract all the children into a single list. I can think of a sticky solution with nested loops, but a solution with function iteration would be ideal.We can consider removing nil values (if any) with flatmap.