Flatten the Array!

In this challenge, your task is to create a program which takes in a nested array and returns a single-dimensional flattened array. For Example [10,20,[30,[40]],50] should output [10,20,30,40,50].

Input

The input will be a nested array (eg. [10,20,[[[10]]]]). It will contain only Integers (both negative and positive), Strings and Arrays. You can take the input as function argument, STDIN or whatever suits your language. You can assume that the input array won't have an empty array.

Output

The output will be a flatted single-dimensional array with the same elements of same type as in the nested array and in the SAME order.

Test Cases

[10,20,30] -> [10,20,30]
[[10]] -> [10]
[["Hi"],[[10]]] -> ["Hi",10]
[[[20],["Hi"],"Hi",20]] -> [20,"Hi","Hi",20]
[[["[]"],"[]"]] -> ["[]","[]"]


Feel free to ask for any clarification by using comments. This is , so the shortest code in bytes wins!

Note: If your language contains a built-in for this, then you must NOT use it.

Edit

• Some languages treat strings as arrays, is [["Hi"],[[10]]] -> ["H","i",10] ok? – Adám May 18 '16 at 5:30
• @Mego I was surprised too to find out that there was an unflatten question but no flatten question on PPCG. – Arjun May 18 '16 at 6:13
• What if your language only supports subarrays of the same size? (E.g. Java?) What if the type of each element must be the same? (E.g. Java, C++ etc.?) Also, please add e.g. ["[",[["[",],'[',"['['"]] as a test case. – flawr May 18 '16 at 11:30
• @flawr That test case only makes sense for languages that support bot ' and " as delimiters. (But I agree that a test case involving [, ], " and \ inside a string would be useful.) – Martin Ender May 18 '16 at 11:40
• The test cases also exclude languages which do not support these kinds of arrays with multiple types, or with another notation for array literals. – flawr May 18 '16 at 11:42

Brachylog, 6 bytes

ċ↰ᵐc|g


Try it online!

ċ         The input is a list,
|     and the output is
c      the concatenation of
↰ᵐ       the results of this predicate on the list's elements.
|     If the input is not a list, then it
g    wrapped in a list is the output.


Lua, 123 bytes

function f(a)o,t={},function(s)for _,v in next,s do if type(v)=="table"then t(v)else o[#o+1]=v end end end t(a)return o end


I'm sure this is not the best answer, but I thought I'd post it since nobody has given a Lua solution yet. Suggestions welcome!

Try it online!

PowerShell, 35 bytes

filter f{if($_.Rank){$_|f}else{\$_}}


Try it online!

Rank - Returns the number of dimensions in the array. Rank = 0 if an object is not array.

C# (Visual C# Interactive Compiler), 72 bytes

T[]f<T>(T[]a)=>a.SelectMany(x=>x is T[]?f(x as T[]):new[]{x}).ToArray();


Try it online!

Recursive function that takes an object[] as input. Yes, there is a type parameter, so you could pass an array of any type... But it only works correctly if all nested arrays are of the same type. That is a requirement that I have added :)

It would be more flexible to get this to work with an input of type IList or Array.