Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}