Skip to content

Feat: Added Memoize Func #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions Cache/Memoize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Memoize
* @param {Function} fn
* @returns
*/
export const memoize = (func) => {
// eslint-disable-next-line no-console
console.log(`Creating cache for function '${func.name}'`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those console.log() are probably not crucial for the functionality, and don't look good when running tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While on the test subject. Where did you get the idea to name the directory __test__, when the rest is just test ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! You're right. I apply my own convention rather than yours... My apologize 🙏
I'm gonna fix it!


const cache = {}

return (...args) => {
const [arg] = args

if (arg in cache) {
// eslint-disable-next-line no-console
console.log(`Reading cache with argument ${arg}`)

return cache[arg]
}

// eslint-disable-next-line no-console
console.log(`Updating cache with argument ${arg}`)

const result = func(arg)
cache[arg] = result
return result
}
}
37 changes: 37 additions & 0 deletions Cache/__tests__/Memoize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { memoize } from '../Memoize'

const fibonacci = (n) => {
if (n < 2) {
return n
}

return fibonacci(n - 2) + fibonacci(n - 1)
}

const factorial = (n) => {
if (n === 0) {
return 1
}

return n * factorial(n - 1)
}

describe('memoize', () => {
it('expects the fibonacci function to use the cache on the second call', () => {
const memoFibonacci = memoize(fibonacci)

expect(memoFibonacci(5)).toEqual(fibonacci(5))
expect(memoFibonacci(5)).toEqual(5)
expect(memoFibonacci(10)).toEqual(fibonacci(10))
expect(memoFibonacci(10)).toEqual(55)
})

it('expects the factorial function to use the cache on the second call', () => {
const memoFactorial = memoize(factorial)

expect(memoFactorial(5)).toEqual(factorial(5))
expect(memoFactorial(5)).toEqual(120)
expect(memoFactorial(10)).toEqual(factorial(10))
expect(memoFactorial(10)).toEqual(3_628_800)
})
})