Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memo spec proposal #229

Open
kurtbrose opened this issue Nov 12, 2021 · 1 comment
Open

Memo spec proposal #229

kurtbrose opened this issue Nov 12, 2021 · 1 comment

Comments

@kurtbrose
Copy link
Collaborator

Here's the proposal: Memo(), a glom memoization primitive.

On each top-level glom execution, Memo() would reset.

Here's a rough implementation:

class Memo:
   def __init__(self, spec):
      self.spec = spec

   def glom(self, target, scope):
      glom_globals = scope[ROOT]
      if self not in glom_globals:
         glom_globals[self] = ({}, {})
      val_memo, id_memo = glom_globals[self]
      try:
         hash(target)
      except:
         memo, memo_key = id_memo, id(target)
      else:
         memo, memo_key = val_memo, target
      if memo_key not in memo:
         memo[memo_key] = scope[glom](target, self.spec, scope)
      return memo[memo_key]

Motivation:

  • allow for safe traversal of self-recursive data structures -- Memo(Ref(Match(Switch(...)))) would be able to safely unzip a into a self-recursive data structure -- memoization would result in a self-recursive output of a similar shape to the input
@kurtbrose
Copy link
Collaborator Author

also, kind of a dupe of #186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant