You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 20, 2022. It is now read-only.
> Suppose you have a performance-intensive function that you must call repeatedly.
12
10
A common solution is to build an internal cache (...) Memoization is a feature built into a programming language that enables automatic caching of recurring function-return values.
13
-
14
-
Functional Thinking - Neal Ford
11
+
>
12
+
> Functional Thinking - Neal Ford
15
13
16
14
Kotlin doesn't have yet any similar feature in it's tools. Although it might have it at some point I wanted to experiment a bit with this technique so that's why I created the lib.
17
15
18
16
19
17
## Important
20
-
**Functions must be pure** for the caching technique to work. A pure function is one that has no side effects: it references no other mutable class fields, doesn't set any values other than the return value, and relies only on the parameters for input.
21
-
Obviously, you can reuse cached results successfully only if the function reliably returns the same values for a given set of parameters.
18
+
Functions must be pure for the caching technique to work.
19
+
20
+
A pure function is one that has no side effects: it references no other mutable class fields,
21
+
doesn't set any values other than the return value, and relies only on the parameters for input.
22
22
23
-
Also, when passing or returning Objects, make sure to **implement both `equals` and `hashcode`** for the cache to work properly!
23
+
In other words, you can reuse cached results successfully only if the function reliably returns the
24
+
same values for a given set of parameters.
25
+
26
+
Also, when passing or returning Objects, make sure to implement both equals and hashcode for the cache to work properly.
24
27
25
28
## Usage
26
29
Having a function like
@@ -35,11 +38,11 @@ You can create a memoized version of it by just calling an extension function ov
35
38
val memoized = ::anExpensiveFun.memoize()
36
39
```
37
40
38
-
Now `memoized` is the same function as `anExpensiveFun` but is wrapped in a Closure that contains an internal cache, meaning that the first call to:
41
+
Now `memoized` is the same function as `anExpensiveFun` but is wrapped in a lambda that contains an internal cache, meaning that the first call to:
39
42
```kotlin
40
43
memoized(5, true)
41
44
```
42
-
Will just execute the function. But the second call **with the same arguments** will retrieve the already calculated value from cache.
45
+
Will just execute the function and return the value. But a second call **with the same arguments** will retrieve the previous value from cache.
43
46
44
47
Note that we're storing values in a **memory cache**, so try to have that in consideration when doing a relatively big amount of calls to your memoized function or if you use big objects as parameters or return type.
45
48
@@ -50,12 +53,12 @@ val memoized = ::anExpensiveFun.memoize(50)
50
53
```
51
54
By default the cache size is initialized with 256.
52
55
53
-
Now you can also pass a specific `HashMap` instance (like ConcurrentHashMap) which also allows freeing the cache after you're done.
56
+
Now you can also pass any `MutableMap` instance (like `HashMap`) which allows custom control of it.
0 commit comments