Skip to content
This repository was archived by the owner on Nov 20, 2022. It is now read-only.

Commit 13f1fdc

Browse files
committed
Updated kotlin lib, removed kotlin benchmark
1 parent c998866 commit 13f1fdc

File tree

7 files changed

+44
-105
lines changed

7 files changed

+44
-105
lines changed

.github/workflows/todo-to-issue.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://github.com/alstr/todo-to-issue-action
2+
name: "TODO to Issue"
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
build:
11+
runs-on: "ubuntu-latest"
12+
steps:
13+
- uses: "actions/checkout@master"
14+
- name: "TODO to Issue"
15+
uses: "alstr/[email protected]"
16+
id: "todo"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ Icon?
3030
ehthumbs.db
3131
Thumbs.db
3232
*~*
33+
local.properties

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ jdk:
66

77
script:
88
- ./gradlew test
9-
- ./gradlew benchmark
109
- ./gradlew clean build

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# MnemoniK
22
[![](https://jitpack.io/v/aballano/mnemonik.svg)](https://jitpack.io/#aballano/mnemonik)
3-
[ ![Download](https://api.bintray.com/packages/aballano/maven/mnemonik/images/download.svg) ](https://bintray.com/aballano/maven/mnemonik/_latestVersion)
4-
[![Kotlin version badge](https://img.shields.io/badge/kotlin-1.4.10-blue.svg)](http://kotlinlang.org/)
53
[![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)
64

75
Simple [memoization](https://en.wikipedia.org/wiki/Memoization) extension function for Kotlin
@@ -10,17 +8,22 @@ Simple [memoization](https://en.wikipedia.org/wiki/Memoization) extension functi
108
## Rationale
119
> Suppose you have a performance-intensive function that you must call repeatedly.
1210
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
1513
1614
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.
1715

1816

1917
## 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.
2222

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.
2427

2528
## Usage
2629
Having a function like
@@ -35,11 +38,11 @@ You can create a memoized version of it by just calling an extension function ov
3538
val memoized = ::anExpensiveFun.memoize()
3639
```
3740

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:
3942
```kotlin
4043
memoized(5, true)
4144
```
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.
4346

4447
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.
4548

@@ -50,12 +53,12 @@ val memoized = ::anExpensiveFun.memoize(50)
5053
```
5154
By default the cache size is initialized with 256.
5255

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.
5457

5558
```kotlin
5659
val map = ConcurrentHashMap<Int, Long>(50)
5760
val memoizedFib = ::fib.memoize(cache = map)
58-
(...)
61+
5962
// clear the cache at the end
6063
map.clear
6164
```

build.gradle

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,29 @@
11
buildscript {
2-
ext.kotlin_version = '1.4.10'
2+
ext.kotlin_version = '1.6.10'
33

44
repositories {
55
mavenCentral()
6-
maven { url 'https://dl.bintray.com/kotlin/kotlinx' }
76
}
87

98
dependencies {
109
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11-
classpath "org.jetbrains.kotlinx:kotlinx.benchmark.gradle:0.2.0-dev-20"
12-
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
1310
}
1411
}
1512

1613
group = 'com.aballano'
1714

1815
subprojects { project ->
1916
apply plugin: "kotlin"
20-
apply plugin: 'kotlinx.benchmark'
21-
apply plugin: "kotlin-allopen"
2217

2318
repositories {
2419
mavenCentral()
25-
maven { url 'https://dl.bintray.com/kotlin/kotlinx' }
26-
}
27-
28-
sourceSets {
29-
benchmarks
3020
}
3121

3222
dependencies {
3323
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
34-
implementation "org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-20"
3524

3625
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.4.2'
3726
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0"
38-
39-
benchmarksImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0"
40-
benchmarksCompile sourceSets.main.output + sourceSets.main.runtimeClasspath
41-
}
42-
43-
allOpen {
44-
annotation("org.openjdk.jmh.annotations.State")
45-
}
46-
47-
benchmark {
48-
configurations {
49-
main {
50-
warmups = 20
51-
iterations = 10
52-
iterationTime = 3
53-
iterationTimeUnit = "s"
54-
}
55-
smoke {
56-
warmups = 5
57-
iterations = 3
58-
iterationTime = 500
59-
iterationTimeUnit = "ms"
60-
}
61-
}
62-
targets {
63-
register("benchmarks")
64-
}
6527
}
6628

6729
test {

lib/src/benchmarks/kotlin/MemoizeBenchmark.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

lib/src/main/kotlin/com/aballano/mnemonik/Memoize.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import java.util.concurrent.ConcurrentMap
77

88
private const val DEFAULT_CAPACITY = 256
99

10+
/**
11+
* Functions must be pure for the caching technique to work.
12+
*
13+
* A pure function is one that has no side effects: it references no other mutable class fields,
14+
* doesn't set any values other than the return value, and relies only on the parameters for input.
15+
* In other words, you can reuse cached results successfully only if the function reliably returns the
16+
* same values for a given set of parameters.
17+
*
18+
* Also, when passing or returning Objects, make sure to implement both equals and hashcode for the cache to work properly.
19+
*/
20+
21+
1022
fun <A, R> ((A) -> R).memoize(
1123
initialCapacity: Int = DEFAULT_CAPACITY
1224
): (A) -> R = memoize(HashMap(initialCapacity))

0 commit comments

Comments
 (0)