Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added explicit cache feature & optional parameter#941

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
raklaptudirm merged 1 commit intoTheAlgorithms:masterfromfahimfaisaal:Upgrade-memoize
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
6 changes: 2 additions & 4 deletionsCache/Memoize.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,12 +10,10 @@
* which lets us use it as [Higher-Order Function](https://eloquentjavascript.net/05_higher_order.html)
* and return another function
* @param {Function} func Original function
* @param {Map} cache - it's receive any cache DS which have get, set & has method
* @returns {Function} Memoized function
*/
const memoize = (func) => {
// Initialization of a slot to store the function result by arguments as a key in Hash Map
const cache = new Map()

const memoize = (func, cache = new Map()) => {
const jsonReplacer = (_, value) => {
if (value instanceof Set) { // if the value is Set it's converted to Array cause JSON.stringify can't convert Set
return [...value]
Expand Down
14 changes: 14 additions & 0 deletionsCache/test/Memoize.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ import { memoize } from '../Memoize'
import { union } from './cacheTest'
import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber'
import { factorial } from '../../Recursive/Factorial'
import LFUCache from '../LFUCache'

const multipleFactorials = (arr) => arr.map(factorial)

Expand DownExpand Up@@ -51,4 +52,17 @@ describe('Testing Memoize', () => {
expect(memoUnion(...inputs)).toEqual(new Set([1, 2, 3, 4, 5, 6]))
expect(memoUnion(...inputs)).toEqual(union(...inputs))
})

it('Testing with explicit cache -> LFUCache', () => {
const LFU = new LFUCache(2)

const memoizeFibonacci = memoize(fibonacci, LFU) // added LFU cache explicitly
const fibOfFiveHundred = memoizeFibonacci(500)
const fibOfOneHundred = memoizeFibonacci(100)

expect(memoizeFibonacci(500)).toBe(fibOfFiveHundred)
expect(memoizeFibonacci(100)).toBe(fibOfOneHundred)

expect(LFU.leastFrequency).toBe(2)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp