MyNixOS website logo
Description

Function Memoization.

A simple way to memoize function results to improve performance by eliminating unnecessary computation or data retrieval activities.

R-CMD-check codecov AppVeyor build status

MemoFunc - A Function Memoization Package for R

A simple way to memoise function results to improve performance by eliminating unnecessary computation or data retrieval activities.

Functions can be memoized with a simple call to memo.


> # a simple example function
> simple.function <- function (value) {
+   print("Executing!")
+   value
+ }

> # call memo function to memoise a function
> simple.function.memo <- memo(simple.function)

> # or like this
> simple.function %<>% memo()

> # or like this
> simple.function2 <- (function (value) value) %>% memo()

Calling a memo is exactly like calling a normal function, in fact it is a normal function! The memo has all the same arguments and defaults as the origional function so it can be used in legacy code without the need for any risky refactoring.


> # the first time we call the memo the function will execute
> simple.function(10)
[1] "Executing!"
[1] 10

> # if we call the function again with the same parameter values then
> # the cached value will be returned
> simple.function(10)
[1] 10

> # calling the memo with a different set of parameter values will
> # cause the function to execute
> simple.function(20)
[1] "Executing!"
[1] 20

Memoing a function can significantly improve the performance of a system by limiting how often expensive call are made. Functions that return a NULL value can be memoed by using the allow.null argument.


> # consider a slow function which is memoised, note that we have used the allow.null argument
> # so that NULL is cached when returned from a function, the default is FALSE
> slow.function <- (function (value) Sys.sleep(value)) %>% memo(allow.null = TRUE)

> # the first time we call the slow function it takes some time
> system.time(slow.function(3))
   user  system elapsed 
   0.00    0.00    3.01 

> # subsequent calls make use of the cache and are much faster
> system.time(slow.function(3))
   user  system elapsed 
   0.01    0.00    0.02 

Installation

devtools::install_github("rwetherall/memofunc")
Metadata

Version

1.0.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows