Skip to content

Commit f063c90

Browse files
authored
support symbols as fns for maps and sets (#776)
Hi, can you please review patch to add support for symbols as fns for maps and sets. Fixes #775. Tests are copied from the corresponding call fn for keywords. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent 1f1acdd commit f063c90

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Changed
1212
* Optimize calls to Python's `operator` module into their corresponding native operators (#754)
1313
* Allow vars to be callable to adhere to Clojure conventions (#767)
14+
* Support symbols as fns for sets and maps (#775)
1415

1516
### Fixed
1617
* Fix issue with `(count nil)` throwing an exception (#759)

src/basilisp/lang/symbol.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from functools import total_ordering
2-
from typing import Optional
3-
4-
from basilisp.lang.interfaces import ILispObject, IPersistentMap, IWithMeta
2+
from typing import Optional, Union
3+
4+
from basilisp.lang.interfaces import (
5+
IAssociative,
6+
ILispObject,
7+
IPersistentMap,
8+
IPersistentSet,
9+
IWithMeta,
10+
)
511
from basilisp.lang.obj import lrepr
612
from basilisp.lang.util import munge
713

@@ -71,6 +77,14 @@ def __lt__(self, other):
7177
return False
7278
return self._ns < other._ns or self._name < other._name
7379

80+
def __call__(self, m: Union[IAssociative, IPersistentSet], default=None):
81+
if isinstance(m, IPersistentSet):
82+
return self if self in m else default
83+
try:
84+
return m.val_at(self, default)
85+
except (AttributeError, TypeError):
86+
return None
87+
7488

7589
def symbol(name: str, ns: Optional[str] = None, meta=None) -> Symbol:
7690
"""Create a new symbol."""

tests/basilisp/symbol_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44

55
from basilisp.lang import map as lmap
6+
from basilisp.lang import set as lset
7+
from basilisp.lang import vector as lvector
68
from basilisp.lang.keyword import keyword
79
from basilisp.lang.symbol import Symbol, symbol
810

@@ -44,6 +46,21 @@ def test_symbol_with_meta():
4446
assert sym3.meta == lmap.m(tag=keyword("macro"))
4547

4648

49+
def test_symbol_as_function():
50+
sym = symbol("kw")
51+
assert None is sym(None)
52+
53+
assert 1 == sym(lmap.map({sym: 1}))
54+
assert "hi" == sym(lmap.map({sym: "hi"}))
55+
assert None is sym(lmap.map({"hi": sym}))
56+
57+
assert sym == sym(lset.s(sym))
58+
assert None is sym(lset.s(1))
59+
assert "hi" is sym(lset.s(1), default="hi")
60+
61+
assert None is sym(lvector.v(1))
62+
63+
4764
@pytest.mark.parametrize(
4865
"o",
4966
[

0 commit comments

Comments
 (0)