Two sum: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
Line 194: Line 194:
Nesting concatMap yields the cartesian product of the list with itself, and
Nesting concatMap yields the cartesian product of the list with itself, and
functions passed to Array.map() have access to the array index in their second argument.
functions passed to Array.map() have access to the array index in their second argument.
Returning [] where the y index is lower than then x index ignores the 'lower triangle' of the cartesian grid,
Returning [] where the y index is lower than or equal to the x index ignores the 'lower triangle'
so mirror-image number pairs are not considered.
of the cartesian grid, skipping mirror-image and duplicate number pairs.
Returning [] where a sum condition is not met similarly acts as a filter – all of the empty lists
Returning [] where a sum condition is not met similarly acts as a filter – all of the empty lists
in the map are eliminated by the concat.
in the map are eliminated by the concat.
Line 207: Line 207:
return concatMap(function (x, ix) {
return concatMap(function (x, ix) {
return concatMap(function (y, iy) {
return concatMap(function (y, iy) {
return iy < ix ? [] : x + y === n ? [
return iy <= ix ? [] : x + y === n ? [
[ix, iy]
[ix, iy]
] : []
] : []