Skip to content

Instantly share code, notes, and snippets.

@gaoryrt
Last active May 11, 2019 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaoryrt/c07ae01b18ee2d64d56422a07bbe241c to your computer and use it in GitHub Desktop.
Save gaoryrt/c07ae01b18ee2d64d56422a07bbe241c to your computer and use it in GitHub Desktop.
flatten a nested array
const isArr = arg => Object.prototype.toString.call(arg) === '[object Array]'
const flat = inputAny =>
(
isArr(inputAny[0])
? flat(inputAny[0])
: [inputAny[0]]
)
.concat(
inputAny.length > 1
? flat(inputAny.slice(1))
: []
)
console.log(flat([0, [1, [[2, 3]], 4]]))
const Y = f => (x => f(x(x)))(x => f((...y) => x(x)(...y)))
const isArr = arg => Object.prototype.toString.call(arg) === '[object Array]'
const flat = Y(
f => inputAny =>
(
isArr(inputAny[0])
? f(inputAny[0])
: [inputAny[0]]
)
.concat(
inputAny.length > 1
? f(inputAny.slice(1))
: []
)
)
console.log(flat([0, [1, [[2, 3]], 4]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment