Skip to content

Improve builtin Lookup#1847

Open
rocky wants to merge 3 commits into
masterfrom
improve-Lookup
Open

Improve builtin Lookup#1847
rocky wants to merge 3 commits into
masterfrom
improve-Lookup

Conversation

@rocky

@rocky rocky commented Jun 28, 2026

Copy link
Copy Markdown
Member

Lookup[] using rules is failing on examples like:

Lookup[<|a -> 1, b -> 2|>, a]
Lookup[<|a -> 1, b -> 2|>, c]
Lookup[<|a -> 1, b -> 2|>, c, 0]

This corrects that. It is not perfect: threading over lists of associations and multiple query keys are not supported. But it is better.

The operator form of Lookup is needed to support Paclet install.

@rocky rocky requested a review from mmatera June 28, 2026 15:57
if element.elements[0] == key:
return element.elements[1]

# Key not found

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a key is not in the association, WMA returns Missing[KeyAbsent, <key>]. For instance,

In[1]:= a=Association[{"F":>1,"G":>2}] ;
In[2]:=Lookup[a,"H"]
Out[2]= Missing[KeyAbsent, H]
In[3]:=Lookup[a,{"P","G"}]
Out[3]= {Missing[KeyAbsent, P], 2}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking and catching. The problem was not in the code, but instead in setting Lookup's attributes.

Add more robustness to test case that assumes an uninitialized variable.
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

Successfully merging this pull request may close these issues.

2 participants