Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Last active December 1, 2017 23:01
Show Gist options
  • Save mvaldesdeleon/48a86d2bd36c7247c121a5d8ab9126a4 to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/48a86d2bd36c7247c121a5d8ab9126a4 to your computer and use it in GitHub Desktop.
Sugerencia meetupjs
// newtype Point = {x :: Number, y :: Number}
// newtype EnhancedPoint = {x :: Number, y :: Number, visible :: Boolean, distance :: Number}
/* Library functions */
// getCenter :: Point => Point => Point
const getCenter =
({x: x1, y: y1}) =>
({x: x2, y: y2}) =>
({x: 0.5*(x1+x2), y: 0.5*(y1+y2)})
// XXX box corners need to be in order
// FIX by ordering the corners before the conditional check
// inBox :: Point => Point => Point => Boolean
const inBox =
({x: x1, y: y1}) =>
({x: x2, y: y2}) =>
({x: xt, y: yt}) =>
x1 <= xt && xt <= x2 &&
y1 <= yt && yt <= y2
// getDistance :: Point => Point => Number
const getDistance =
({x: x1, y: y1}) =>
({x: x2, y: y2}) =>
Math.sqrt (Math.pow (x1 - x2, 2) + Math.pow (y1 - y2, 2))
/* Input data */
// topLeft :: Point
const topLeft =
{x: ..., y: ...}
// bottomRight :: Point
const bottomRight =
{x: ..., y: ...}
// results :: [Point]
const results =
[
{x: ..., y: ...},
...
]
/* Auxiliary data */
// center :: Point
const center =
getCenter (topLeft) (bottomRight)
// inScreen :: Point => Boolean
const inScreen =
inBox (topLeft) (bottomRight)
/* Specific helper functions */
// addDistanceAndVisibility :: Point => EnhancedPoint
const addDistanceAndVisibility =
p =>
({x: p.x, y: p.y, visible: inScreen (p), distance: getDistance (p) (center)})
// byVisibleThenClosest :: (EnhancedPoint, EnhancedPoint) => Number
const byVisibleThenClosest =
({inScreen1, distance1}, {inScreen2, distance2}) =>
const visible = visible1 - visible2 // true before false
const distance = distance2 - distance1 // low before high
return visible !== 0 ? visible : distance
}
/* The result */
// result :: EnhancedPoint | undefined
const result = results
.map (addDistanceAndVisibility)
.sort (byVisibleThenClosest)
[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment