Skip to content

Improve Concept Exercise: Coordinate Transformation (closures) #1373

Closed

Description

@SleeplessByte

The goal of this task is to limit the number of different solutions are valid for the Concept Exercise "Coordinate Transformation".

As a refresher, or first time read, the docs can be found here:

Currently, the final task asks to write a memoize function, that only memoizes the last result.
However, the tests don't enforce that final limitation. Technically memoizing all calls is fine if you look at the grammar pedantically (it's undefined), but the many many ways to build a cache make it harder to write automated analyzers for, and we really want to force people to use let (or var) here.

Solution that should not pass (but passes)

export function memoizeTransform(f) {
  const cache = {}

  return function memoize(x, y) {
    const key = `${x}:${y}`
    
    return cache[key] || cache[key] = f(x, y)
  }
}

Solution that should pass (and passes)

export function memoizeTransform(f) {
  let lastX, lastY, lastResult

  return function memoize(x, y) {
    if (x === lastX && y === lastY) {
      return lastResult
    }
    
    lastX = x
    lastY = y
    
    return lastResult = f(x, y)
  }
}

Activity

  1. added a commit that references this issue on Sep 25, 2021
    2602337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions