From 07c95d82de891e0c6553c96f062f32dfb787839d Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 01:41:46 +0000 Subject: [PATCH] [Sync Iteration] javascript/coordinate-transformation/1 --- .../1/coordinate-transformation.js | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 solutions/javascript/coordinate-transformation/1/coordinate-transformation.js diff --git a/solutions/javascript/coordinate-transformation/1/coordinate-transformation.js b/solutions/javascript/coordinate-transformation/1/coordinate-transformation.js new file mode 100644 index 0000000..1eb9719 --- /dev/null +++ b/solutions/javascript/coordinate-transformation/1/coordinate-transformation.js @@ -0,0 +1,79 @@ +// @ts-check +// +// The line above enables type checking for this file. Various IDEs interpret +// the @ts-check directive. It will give you helpful autocompletion when +// implementing this exercise. + +/** + * Create a function that returns a function making use of a closure to + * perform a repeatable 2d translation of a coordinate pair. + * + * @param {number} dx the translate x component + * @param {number} dy the translate y component + * + * @returns {function} a function which takes an x, y parameter, returns the + * translated coordinate pair in the form [x, y] + */ +export function translate2d(dx, dy) { + return function coordinates(x, y) { + return [dx + x, dy + y]; + } +} + +/** + * Create a function that returns a function making use of a closure to + * perform a repeatable 2d scale of a coordinate pair. + * + * @param {number} sx the amount to scale the x component + * @param {number} sy the amount to scale the y component + * + * @returns {function} a function which takes an x, y parameter, returns the + * scaled coordinate pair in the form [x, y] + */ +export function scale2d(sx, sy) { + return function coordinates(x, y) { + return [sx * x, sy * y] + }; +} + +/** + * Create a composition function that returns a function that combines two + * functions to perform a repeatable transformation + * + * @param {function} f the first function to apply + * @param {function} g the second function to apply + * + * @returns {function} a function which takes an x, y parameter, returns the + * transformed coordinate pair in the form [x, y] + */ +export function composeTransform(f, g) { + return function (x, y) { + const [x1, y1] = f(x, y) + return g(x1, y1) + } +} + +/** + * Return a function that memoizes the last result. If the arguments are the same as the last call, + * then memoized result returned. + * + * @param {function} f the transformation function to memoize, assumes takes two arguments 'x' and 'y' + * + * @returns {function} a function which takes x and y arguments, and will either return the saved result + * if the arguments are the same on subsequent calls, or compute a new result if they are different. + */ +export function memoizeTransform(f) { + let lastX + let lastY + let lastResult + + return function (x, y) { + if (x === lastX && y === lastY) { + return lastResult + } + lastX = x + lastY = y + lastResult = f(x, y) + return lastResult + } +}