Published
Edited
Jun 24, 2018
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// def concat(x, y):
// return [x] + y if type(y) == list else [x, y]
//
// def product(xs, *args):
//
// if len(args) == 0:
// return xs
//
// ys, *args = args
//
// xys = [
// concat(x, y)
// for x in xs
// for y in ys
// ]
//
// return product(xys, *args)
Insert cell
Insert cell
Insert cell
Insert cell
function solution1(xs, ys, ...args) {
if (typeof ys === 'undefined') return xs
let xys = []
for (const x of xs) {
for (const y of ys) {
xys.push([].concat(x, y))
}
}
return solution1(xys, ...args)
}
Insert cell
solution1([1, 2], [3, 4])
Insert cell
solution1([1, 2], [3, 4], [5, 6])
Insert cell
Insert cell
Insert cell
Insert cell
function solution2(xs, ys, ...args) {
if (typeof ys === 'undefined') return xs
const xys = [].concat(...xs.map(x => ys.map(y => [].concat(x, y))))
return solution2(xys, ...args)
}
Insert cell
solution2([1, 2], [3, 4])
Insert cell
solution2([1, 2], [3, 4], [5, 6])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more