Skip to content

Instantly share code, notes, and snippets.

@cloudle
Created October 29, 2016 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudle/397a51086df486bf4654321a7e33b11d to your computer and use it in GitHub Desktop.
Save cloudle/397a51086df486bf4654321a7e33b11d to your computer and use it in GitHub Desktop.
//@flow *Ecmascript 6 syntax & require flow syntax checking before build;
function flatten (source:Array<any>) : Array<number> {
return source.reduce((previous, current) => {
let next;
if (Array.isArray(current)) {
next = flatten(current);
} else if (typeof current == 'number') {
next = [current];
} else {
throw 'Not a number';
}
return [...previous, ...next];
}, []);
}
console.log(flatten([[1,2,[3], [4]],5]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment