Skip to content

Instantly share code, notes, and snippets.

@line-o
Last active August 11, 2022 21:14
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 line-o/9ac314dde4b77f2d079d43539de035ee to your computer and use it in GitHub Desktop.
Save line-o/9ac314dde4b77f2d079d43539de035ee to your computer and use it in GitHub Desktop.
array:flatten is recursive
declare function local:flatten-one($nested as array(*)) as array(*) {
array:fold-left($nested, [], function ($flattened as array(*), $next as item()*) as array(*) {
typeswitch($next)
case array(*) return array:join(($flattened, $next))
default return array:append($flattened, $next)
})
};
local:flatten-one([[1], 3, map{}, [1, [3,4]], ()])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment