WoW:USERAPI Memorizing table: Difference between revisions

m
Move page script moved page USERAPI Memorizing table to WoW:USERAPI Memorizing table without leaving a redirect
(New page: {{userfunc}} __NOTOC__ A '''memorizing table''' is a normal table with a metatable designed to find missing values and save them. The mechanics are transparent to the caller, it thinks i...)
 
m (Move page script moved page USERAPI Memorizing table to WoW:USERAPI Memorizing table without leaving a redirect)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{userfunc}}
{{userfunc}}
__NOTOC__
A '''memoizing table''' is a normal table with a metatable designed to find missing values and save them.  The mechanics are transparent to the caller, it thinks it's just doing a normal lookup.


A '''memorizing table''' is a normal table with a metatable designed to find missing values and save them.  The mechanics are transparent to the caller, it thinks it's just doing a normal lookup.
Memoizing tables are especially handy for caching values which are expensive to lookup or calculate.  The developer generally expects that for a given index the value won't change, so it does not need to be recomputed after the first lookup.
 
Memorizing tables are especially handy for caching values which are expensive to lookup or calculate.  The developer generally expects that for a given index the value won't change, so it does not need to be recomputed after the first lookup.


== Code ==
== Code ==
For this example we will cache the name results from a [[API GetItemInfo|GetItemInfo]] call.
For this example, we will cache the name results from a [[API GetItemInfo|GetItemInfo]] call.


  local names = setmetatable({}, {
  local names = setmetatable({}, {
Line 16: Line 14:
  return name
  return name
  end,
  end,
  })  
  })


== How it works ==
== How it works ==
Anonymous user