🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # damiankorz's solution

## to Flatten Array in the CFML Track

Published at Dec 18 2018 · 0 comments
Instructions
Test suite
Solution

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd` into the exercise folder and run the following:

``````box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher
``````

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

## Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### FlattenArrayTest.cfc

``````component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'FlattenArray' );
}

function run(){

describe( "My FlattenArray class", function(){

it( 'no nesting', function(){
expect( SUT.flatten( array=[0, 1, 2] ) ).toBe( [0,1,2] );
});

it( 'flattens array with just integers present', function(){
expect( SUT.flatten( array=[1, [2, 3, 4, 5, 6, 7], 8] ) ).toBe( [1,2,3,4,5,6,7,8] );
});

it( '5 level nesting', function(){
expect( SUT.flatten( array=[0, 2, [[2, 3], 8, 100, 4, [[]]], -2] ) ).toBe( [0,2,2,3,8,100,4,50,-2] );
});

it( '6 level nesting', function(){
expect( SUT.flatten( array=[1, [2, [], [4, []], 6, 7], 8] ) ).toBe( [1,2,3,4,5,6,7,8] );
});

it( '6 level nest list with null values', function(){
expect( SUT.flatten( array=[0, 2, [[2, 3], 8, [], nullValue(), [[nullValue()]]], -2] ) ).toBe( [0,2,2,3,8,100,-2] );
});

it( 'all values in nested list are null', function(){
expect( SUT.flatten( array=[nullValue(), [[[nullValue()]]], nullValue(), nullValue(), [[nullValue(), nullValue()], nullValue()], nullValue()] ) ).toBe( [] );
});

});

}

}``````

### SolutionTest.cfc

``````component extends="FlattenArrayTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````
``````component {
/**
* @returns
*/
public void function flattenNested( required array nested, required array output ){
for( var i = 1; i <= arrayLen( nested ); i++ ){
if( isNull(nested[i]) ){
nested[i] = "";
}
if( !isArray(nested[i]) && len(nested[i]) ){
arrayAppend(output, nested[i]);
}
if( isArray(nested[i]) && len(nested[i]) ){
flattenNested(nested[i], output);
}
}
}
public array function flatten( required array array ) {
var output = [];
for(var i = 1; i <= arrayLen(array); i++){
if( isNull(array[i]) ){
array[i] = "";
}
if( !isArray(array[i]) && len(array[i]) ){
arrayAppend(output, array[i]);
}
if( isArray(array[i]) && !isNull(array[i]) ){
flattenNested(array[i], output);
}
}
return output;
}
}``````

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?