From b0ed1039317c5c03f7fe9b1bd0dd535748c4856b Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Fri, 22 Mar 2019 02:06:36 +0000 Subject: [PATCH] add `list?` function to `hy.core` `list?` will test if the argument is an instance of list. --- NEWS.rst | 1 + docs/language/core.rst | 18 ++++++++++++++++++ hy/core/language.hy | 5 ++++- tests/native_tests/core.hy | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/NEWS.rst b/NEWS.rst index f1d7447..540b399 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -11,6 +11,7 @@ New Features ------------------------------ * Format strings with embedded Hy code (e.g., `f"The sum is {(+ x y)}"`) are now supported, even on Pythons earlier than 3.6. +* New list? function. Bug Fixes ------------------------------ diff --git a/docs/language/core.rst b/docs/language/core.rst index 4237418..92d41d9 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -899,6 +899,24 @@ Returns the first logically-true value of ``(pred x)`` for any ``x`` in True +.. _list?-fn: + +list? +----- + +Usage: ``(list? x)`` + +Returns ``True`` if *x* is a list. + +.. code-block:: hy + + => (list? '(inc 41)) + True + + => (list? '42) + False + + .. _string?-fn: string? diff --git a/hy/core/language.hy b/hy/core/language.hy index ac046bb..3e1da65 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -203,6 +203,9 @@ Return series of accumulated sums (or other binary function results)." "Check if x is float." (isinstance x float)) +(defn list? [x] + (isinstance x list)) + (defn symbol? [s] "Check if `s` is a symbol." (instance? HySymbol s)) @@ -454,7 +457,7 @@ Even objects with the __name__ magic will work." disassemble drop drop-last drop-while empty? eval even? every? exec first filter flatten float? fraction gensym group-by identity inc input instance? integer integer? integer-char? interleave interpose islice iterable? - iterate iterator? juxt keyword keyword? last macroexpand + iterate iterator? juxt keyword keyword? last list? macroexpand macroexpand-1 mangle map merge-with multicombinations name neg? none? nth numeric? odd? partition permutations pos? product range read read-str remove repeat repeatedly rest reduce second some string string? symbol? diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index ab8eaf4..53efc3f 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -267,6 +267,11 @@ result['y in globals'] = 'y' in globals()") (assert-true (symbol? 'im-symbol)) (assert-false (symbol? (name 'im-symbol)))) +(defn test-list? [] + "NATIVE: testing the list? function" + (assert-false (list? "hello")) + (assert-true (list? [1 2 3]))) + (defn test-gensym [] "NATIVE: testing the gensym function" (import [hy.models [HySymbol]])