No condition, immutable
by Arnaud Buchholz
HTML
<script src="https://arnaudbuchholz.github.io/blog/jsfiddle-assert.js"></script>
JavaScript
function allocate (size, values) {
return new Array(size).fill(false)
const stop = [() => []]
const [value, ...others] = values ?? []
const next = stop[size - 1] ?? allocate
return [value ?? false, ...next(size - 1, others)]
}
assert(() => allocate(2).length === 2)
assert(() => allocate(2)[0] === false)
assert(() => allocate(2)[1] === false)
/*
assert(() => allocate(2, [true]).length === 2)
assert(() => allocate(2, [true])[0] === true)
assert(() => allocate(2, [true, true])[1] === true)
*/