diff options
Diffstat (limited to 'public/make-memoize.md')
-rw-r--r-- | public/make-memoize.md | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/public/make-memoize.md b/public/make-memoize.md index 03bb2dc..64f4f5f 100644 --- a/public/make-memoize.md +++ b/public/make-memoize.md @@ -3,6 +3,7 @@ A memoization routine for GNU Make functions --- date: "2014-11-20" license: WTFPL-2 +markdown_options: "-markdown_in_html_blocks" --- I'm a big fan of [GNU Make][make]. I'm pretty knowledgeable about it, @@ -29,21 +30,21 @@ unsuitable for my needs. So, I implemented my own, more flexible memoization routine for Make. - # This definition of `rest` is equivalent to that in GMSL - rest = $(wordlist 2,$(words $1),$1) - - # How to use: Define 2 variables (the type you would pass to $(call): - # `_<var>NAME</var>_main` and `_<var>NAME</var>_hash`. Now, `_<var>NAME</var>_main` is the function getting - # memoized, and _<var>NAME</var>_hash is a function that hashes the function arguments - # into a string suitable for a variable name. - # - # Then, define the final function like: - # - # <var>NAME</var> = $(foreach func,<var>NAME</var>,$(memoized)) - - _main = $(_$(func)_main) - _hash = __memoized_$(_$(func)_hash) - memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash))) +<pre><code># This definition of `rest` is equivalent to that in GMSL +rest = $(wordlist 2,$(words $1),$1) + +# How to use: Define 2 variables (the type you would pass to $(call): +# `_<var>NAME</var>_main` and `_<var>NAME</var>_hash`. Now, `_<var>NAME</var>_main` is the function getting +# memoized, and _<var>NAME</var>_hash is a function that hashes the function arguments +# into a string suitable for a variable name. +# +# Then, define the final function like: +# +# <var>NAME</var> = $(foreach func,<var>NAME</var>,$(memoized)) + +_main = $(_$(func)_main) +_hash = __memoized_$(_$(func)_hash) +memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash)))</code></pre> However, I later removed it from the Makefile, as I [re-implemented][reimplement] the bits that it memoized in a more efficient way, such that memoization @@ -53,21 +54,21 @@ Later, I realized that my memoized routine could have been improved by replacing `func` with `$0`, which would simplify how the final function is declared: - # This definition of `rest` is equivalent to that in GMSL - rest = $(wordlist 2,$(words $1),$1) - - # How to use: - # - # _<var>NAME</var>_main = <var>your main function to be memoized</var> - # _<var>NAME</var>_hash = <var>your hash function for parameters</var> - # <var>NAME</var> = $(memoized) - # - # The output of your hash function should be a string following - # the same rules that variable names follow. - - _main = $(_$0_main) - _hash = __memoized_$(_$0_hash) - memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash))) +<pre><code># This definition of `rest` is equivalent to that in GMSL +rest = $(wordlist 2,$(words $1),$1) + +# How to use: +# +# _<var>NAME</var>_main = <var>your main function to be memoized</var> +# _<var>NAME</var>_hash = <var>your hash function for parameters</var> +# <var>NAME</var> = $(memoized) +# +# The output of your hash function should be a string following +# the same rules that variable names follow. + +_main = $(_$0_main) +_hash = __memoized_$(_$0_hash) +memoized = $(if $($(_hash)),,$(eval $(_hash) := _ $(_main)))$(call rest,$($(_hash)))</pre></code> Now, I'm pretty sure that should work, but I have only actually tested the first version. |