Skip to content

Commit 3d82670

Browse files
authored
More core library functions and macros (#373)
* More core library functions and macros * Fix when-let and when-some * Tests * Testssss * Additional tests * More functions and tests * Oops * Bitwise functions * Woops * Fix seq iterators to yield to child iterators
1 parent f12900a commit 3d82670

File tree

8 files changed

+1472
-531
lines changed

8 files changed

+1472
-531
lines changed

src/basilisp/core/__init__.lpy

Lines changed: 433 additions & 50 deletions
Large diffs are not rendered by default.

src/basilisp/lang/interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def __eq__(self, other):
204204

205205
def __iter__(self):
206206
o = self
207-
while o:
207+
if o:
208208
yield o.first
209-
o = o.rest
209+
yield from o.rest
210210

211211

212212
class IType(ABC):

src/basilisp/lang/runtime.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ISeqable,
4444
)
4545
from basilisp.lang.typing import LispNumber
46+
from basilisp.lang.util import munge
4647
from basilisp.logconfig import TRACE
4748
from basilisp.util import Maybe
4849

@@ -827,7 +828,8 @@ def apply_kw(f, args):
827828
except TypeError as e:
828829
logger.debug("Ignored %s: %s", type(e).__name__, e)
829830

830-
return f(*final, **last)
831+
kwargs = to_py(last, lambda kw: munge(kw.name, allow_builtins=True))
832+
return f(*final, **kwargs)
831833

832834

833835
__nth_sentinel = object()
@@ -983,6 +985,11 @@ def get(m, k, default=None):
983985
return default
984986

985987

988+
def is_special_form(s: sym.Symbol) -> bool:
989+
"""Return True if s names a special form."""
990+
return s in _SPECIAL_FORMS
991+
992+
986993
@functools.singledispatch
987994
def to_lisp(o, keywordize_keys: bool = True):
988995
"""Recursively convert Python collections into Lisp collections."""
@@ -1240,6 +1247,7 @@ def _basilisp_fn(f):
12401247
"""Create a Basilisp function, setting meta and supplying a with_meta
12411248
method implementation."""
12421249
assert not hasattr(f, "meta")
1250+
f._basilisp_fn = True
12431251
f.meta = None
12441252
f.with_meta = partial(_fn_with_meta, f)
12451253
return f

src/basilisp/lang/seq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ def is_realized(self):
158158

159159
def __iter__(self):
160160
o = self
161-
while o:
161+
if o:
162162
first = o.first
163163
if isinstance(o, LazySeq):
164164
if o.is_empty:
165165
return
166166
yield first
167-
o = o.rest
167+
yield from o.rest
168168

169169

170170
def sequence(s: Iterable) -> ISeq[Any]:

tests/basilisp/core_macros_test.lpy

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,54 @@
218218
(is (= nil (comment 1)))
219219
(is (= nil (comment [1 2 3]))))
220220

221+
(deftest if-let-test
222+
(is (= :a (if-let [a :a] a :b)))
223+
(is (= 2 (if-let [a 1] (inc a) :b)))
224+
(is (= :b (if-let [a nil] a :b)))
225+
(is (= :b (if-let [a false] a :b)))
226+
(is (thrown? basilisp.lang.compiler/CompilerException
227+
(eval '(if-let [a nil] a a))))
228+
229+
;; ExceptionInfo is thrown and wrapped in a CompilerException
230+
;; during macro expansion
231+
(is (thrown? basilisp.lang.compiler/CompilerException
232+
(eval '(if-let [a nil b :something] a :b)))))
233+
234+
(deftest if-some-test
235+
(is (= :a (if-some [a :a] a :b)))
236+
(is (= 2 (if-some [a 1] (inc a) :b)))
237+
(is (= :b (if-some [a nil] a :b)))
238+
(is (= false (if-some [a false] a :b)))
239+
(is (thrown? basilisp.lang.compiler/CompilerException
240+
(eval '(if-some [a nil] a a))))
241+
242+
;; ExceptionInfo is thrown and wrapped in a CompilerException
243+
;; during macroexpansion
244+
(is (thrown? basilisp.lang.compiler/CompilerException
245+
(eval '(if-some [a nil b :something] a :b)))))
246+
247+
(deftest when-let-test
248+
(is (= :a (when-let [a :a] a)))
249+
(is (= 2 (when-let [a 1] (inc a))))
250+
(is (nil? (when-let [a nil] a)))
251+
(is (nil? (when-let [a false] a)))
252+
253+
;; ExceptionInfo is thrown and wrapped in a CompilerException
254+
;; during macroexpansion
255+
(is (thrown? basilisp.lang.compiler/CompilerException
256+
(eval '(when-let [a nil b :something] a)))))
257+
258+
(deftest when-some-test
259+
(is (= :a (when-some [a :a] a)))
260+
(is (= 2 (when-some [a 1] (inc a))))
261+
(is (nil? (when-some [a nil] a)))
262+
(is (= false (when-some [a false] a)))
263+
264+
;; ExceptionInfo is thrown and wrapped in a CompilerException
265+
;; during macroexpansion
266+
(is (thrown? basilisp.lang.compiler/CompilerException
267+
(eval '(when-some [a nil b :something] a)))))
268+
221269
(deftest ->-test
222270
(is (= :a (-> :a)))
223271
(is (= 2 (-> 1 inc)))

0 commit comments

Comments
 (0)