diff --git a/AUTHORS b/AUTHORS index 590223c..7e744e0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -91,3 +91,4 @@ * Oskar Kvist * Brandon T. Willard * Andrew R. M. +* Tristan de Cacqueray 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/contrib/hy_repr.hy b/hy/contrib/hy_repr.hy index 9fd59be..c99a45a 100644 --- a/hy/contrib/hy_repr.hy +++ b/hy/contrib/hy_repr.hy @@ -18,7 +18,7 @@ (setv -registry {}) (defn hy-repr-register [types f &optional placeholder] - (for [typ (if (instance? list types) types [types])] + (for [typ (if (list? types) types [types])] (setv (get -registry typ) (, f placeholder)))) (setv -quoting False) diff --git a/hy/contrib/walk.hy b/hy/contrib/walk.hy index a7a46e9..d30f29e 100644 --- a/hy/contrib/walk.hy +++ b/hy/contrib/walk.hy @@ -19,7 +19,7 @@ (outer (HyExpression (map inner form)))] [(instance? HyDict form) (HyDict (outer (HyExpression (map inner form))))] - [(instance? list form) + [(list? form) ((type form) (outer (HyExpression (map inner form))))] [(coll? form) (walk inner outer (list form))] 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/hy/core/macros.hy b/hy/core/macros.hy index 5e06751..1021c76 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -213,7 +213,7 @@ Such 'o!' params are available within `body` as the equivalent 'g!' symbol." (defn extract-o!-sym [arg] (cond [(and (symbol? arg) (.startswith arg "o!")) arg] - [(and (instance? list arg) (.startswith (first arg) "o!")) + [(and (list? arg) (.startswith (first arg) "o!")) (first arg)])) (setv os (list (filter identity (map extract-o!-sym args))) gs (lfor s os (HySymbol (+ "g!" (cut s 2))))) 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]])