Skip to content

Commit

Permalink
Keyword fn should test for membership in sets
Browse files Browse the repository at this point in the history
  • Loading branch information
ikappaki committed Dec 29, 2023
1 parent 2e864d9 commit 95784ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
* Fix issue with `(count nil)` throwing an exception (#759).
* Fix issue with keyword fn not testing for test membership in sets (#762).

## [v0.1.0b0]
### Added
Expand Down
13 changes: 10 additions & 3 deletions src/basilisp/lang/keyword.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import threading
from functools import total_ordering
from typing import Iterable, Optional
from typing import Iterable, Optional, Union

from basilisp.lang import map as lmap
from basilisp.lang.interfaces import IAssociative, ILispObject, IPersistentMap
from basilisp.lang.interfaces import (
IAssociative,
ILispObject,
IPersistentMap,
IPersistentSet,
)

_LOCK = threading.Lock()
_INTERN: IPersistentMap[int, "Keyword"] = lmap.PersistentMap.empty()
Expand Down Expand Up @@ -53,7 +58,9 @@ def __lt__(self, other):
return False
return self._ns < other._ns or self._name < other._name

def __call__(self, m: IAssociative, default=None):
def __call__(self, m: Union[IAssociative, IPersistentSet], default=None):
if isinstance(m, IPersistentSet):
return self if self in m else default
try:
return m.val_at(self, default)
except AttributeError:
Expand Down
5 changes: 5 additions & 0 deletions tests/basilisp/keyword_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from basilisp.lang import map as lmap
from basilisp.lang import set as lset
from basilisp.lang.keyword import Keyword, complete, keyword


Expand Down Expand Up @@ -46,6 +47,10 @@ def test_keyword_as_function():
assert "hi" == kw(lmap.map({kw: "hi"}))
assert None is kw(lmap.map({"hi": kw}))

assert kw == kw(lset.s(kw))
assert None is kw(lset.s(1))
assert "hi" is kw(lset.s(1), default="hi")


@pytest.mark.parametrize(
"o",
Expand Down

0 comments on commit 95784ce

Please sign in to comment.