Created
October 29, 2016 23:18
-
-
Save cloudle/397a51086df486bf4654321a7e33b11d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@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