Two sum: Difference between revisions

Content added Content deleted
m (→‎ES6: Minor simplification)
(→‎JS ES6: Added comments on access to indices, and skipping the 'lower triangle' of a cartesian grid (where iy < ix))
Line 244: Line 244:
// summingPairIndices :: -> Int -> [Int] -> [(Int, Int)]
// summingPairIndices :: -> Int -> [Int] -> [(Int, Int)]
let summingPairIndices = (n, xs) =>
let summingPairIndices = (n, xs) =>

// Javascript map functions have access to the array index
// in their second argument.
concatMap((x, ix) => concatMap((y, iy) =>
concatMap((x, ix) => concatMap((y, iy) =>
iy > ix ? [] : ( // Ignoring mirror image tuples
iy < ix ? [] : ( // Ignoring mirror-image tuples by
// skipping the 'lower triangle' (y < x)
// of the cartesian product grid
x + y === n ? [
x + y === n ? [
[iy, ix]
[ix, iy]
] : []
] : []
), xs), xs);
), xs), xs);