All Projects → aballano → mnemonik

aballano / mnemonik

Licence: Apache-2.0 license
Simple memoization extension function for Kotlin https://en.wikipedia.org/wiki/Memoization

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to mnemonik

Beautiful React Redux
Redux 🚀, Redux 🤘, Redux 🔥 - and the magic optimization
Stars: ✭ 87 (+117.5%)
Mutual labels:  memoization
Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (+345%)
Mutual labels:  memoization
Cacheout
A caching library for Python
Stars: ✭ 238 (+495%)
Mutual labels:  memoization
Python Memoization
A powerful caching library for Python, with TTL support and multiple algorithm options.
Stars: ✭ 109 (+172.5%)
Mutual labels:  memoization
Use Inline Memo
⚛️ React hook for memoizing values inline anywhere in a component
Stars: ✭ 152 (+280%)
Mutual labels:  memoization
Joblib
Computing with Python functions.
Stars: ✭ 2,620 (+6450%)
Mutual labels:  memoization
Decko
💨 The 3 most useful ES7 decorators: bind, debounce and memoize
Stars: ✭ 1,024 (+2460%)
Mutual labels:  memoization
Dynamic-Programming-Questions-by-Aditya-Verma
Aditya Verma (Youtube) DP Playlist Codes/Solutions.
Stars: ✭ 148 (+270%)
Mutual labels:  memoization
Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (+310%)
Mutual labels:  memoization
Datalog
An in-memory datalog implementation for OCaml.
Stars: ✭ 218 (+445%)
Mutual labels:  memoization
Frontend Computer Science
A list of Computer Science topics important for a Front-End Developer to learn 📝
Stars: ✭ 113 (+182.5%)
Mutual labels:  memoization
Data Structures
Common data structures and algorithms implemented in JavaScript
Stars: ✭ 139 (+247.5%)
Mutual labels:  memoization
Memoize One
A memoization library which only remembers the latest invocation
Stars: ✭ 2,649 (+6522.5%)
Mutual labels:  memoization
Memery
A gem for memoization in Ruby
Stars: ✭ 98 (+145%)
Mutual labels:  memoization
memo wise
The wise choice for Ruby memoization
Stars: ✭ 486 (+1115%)
Mutual labels:  memoization
React Selector Hooks
Collection of hook-based memoized selector factories for declarations outside of render.
Stars: ✭ 84 (+110%)
Mutual labels:  memoization
Memo Decorator
Decorator which applies memoization to a method of a class.
Stars: ✭ 213 (+432.5%)
Mutual labels:  memoization
LruClockCache
A low-latency LRU approximation cache in C++ using CLOCK second-chance algorithm. Multi level cache too. Up to 2.5 billion lookups per second.
Stars: ✭ 35 (-12.5%)
Mutual labels:  memoization
Anamnesis.jl
Fancy memoizing for expensive functions in Julia.
Stars: ✭ 18 (-55%)
Mutual labels:  memoization
Fast Memoize.js
🐇 Fastest possible memoization library
Stars: ✭ 2,478 (+6095%)
Mutual labels:  memoization

MnemoniK

Hex.pm

Simple memoization extension function for Kotlin

Rationale

Suppose you have a performance-intensive function that you must call repeatedly. 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.

Functional Thinking - Neal Ford

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.

Important

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.

In other words, you can reuse cached results successfully only if the function reliably returns the same values for a given set of parameters.

Also, when passing or returning Objects, make sure to implement both equals and hashcode for the cache to work properly.

Usage

Having a function like:

fun anExpensiveFun(someArg: Int, someOtherArg: Boolean): String = { /*...*/ }

You can create a memoized version of it by just calling an extension function over its reference like this:

val memoized = ::anExpensiveFun.memoize()

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:

memoized(5, true)

Will just execute the function and return the value. But a second call with the same arguments will retrieve the previous value from cache.

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.

If you want to specify how big the cache has to be you can do it like the following:

val memoized = ::anExpensiveFun.memoize(50)

By default the cache size is initialized with 256.

By default HashMap and ConcurrentHashMap are used as caches but you can also pass any MutableMap and ConcurrentMap instances which allows custom control of the cache.

val map = ConcurrentHashMap<Int, Boolean>(50)
val memoized = ::anExpensiveFun.memoize(cache = map)

// clear the cache at the end
map.clear

Note: The same approach also works for suspend functions.

Limitations

Currently this library only supports up to 5 function parameters.

Note that the memoization might not be thread safe for the first call, subsequent calls are safe as they will simply retrieve from cache.

Distribution

Add as a dependency to your build.gradle with Jitpack

License

MIT License

Copyright (c) 2022 aballano

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].