From dafcc7ec70352a2418587e9b1d686a1c9ee13687 Mon Sep 17 00:00:00 2001 From: Zhao Shenyang Date: Thu, 29 Jan 2015 23:17:52 +0800 Subject: [PATCH 1/3] add `symbol?` function to `hy.core` `symbol?` will test if the input is an instance of HySymbol. It's useful when writing macros. --- hy/core/language.hy | 7 ++++++- tests/native_tests/core.hy | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 4df6ef4..20483ff 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -29,6 +29,7 @@ (import sys) (import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy.models.cons [HyCons]] + [hy.models.symbol [HySymbol]] [hy.models.keyword [HyKeyword *keyword-prefix*]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]]) @@ -163,6 +164,10 @@ "Return True if x is float" (isinstance x float)) +(defn symbol? [s] + "Check whether s is a symbol" + (instance? HySymbol s)) + (import [threading [Lock]]) (setv _gensym_counter 1234) (setv _gensym_lock (Lock)) @@ -414,5 +419,5 @@ interpose iterable? iterate iterator? keyword keyword? list* macroexpand macroexpand-1 map merge-with name neg? nil? none? nth numeric? odd? pos? range read remove repeat repeatedly - rest reduce second some string string? take take-nth + rest reduce second some string string? symbol? take take-nth take-while zero? zip zip_longest zipwith]) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 2e92545..de5d8c7 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -233,6 +233,14 @@ (assert-true (float? -3.2)) (assert-false (float? "foo"))) +(defn test-symbol? [] + "NATIVE: testing the symbol? function" + (assert-false (symbol? "hello")) + (assert-false (symbol? [1 2 3])) + (assert-false (symbol? '[a b c])) + (assert-true (symbol? 'im-symbol)) + (assert-false (symbol? (name 'im-symbol)))) + (defn test-gensym [] "NATIVE: testing the gensym function" (import [hy.models.symbol [HySymbol]]) From 487710c0934207c2fa0d270a7f50023c59674350 Mon Sep 17 00:00:00 2001 From: Zhao Shenyang Date: Fri, 30 Jan 2015 01:21:26 +0800 Subject: [PATCH 2/3] add documents for symbol? --- docs/language/core.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/language/core.rst b/docs/language/core.rst index 916995e..a3dd511 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -736,6 +736,23 @@ Returns ``True`` if *x* is a string. => (string? -2) False +.. _symbol?-fn: + +symbol? +------- + +Usage: ``(symbol? x)`` + +Returns ``True`` if *x* is a symbol. + +.. code-block:: hy + + => (symbol? 'foo) + True + + => (symbol? '[a b c]) + False + .. _zero?-fn: zero? From 95107df685a7bc23b214006974407df390d47563 Mon Sep 17 00:00:00 2001 From: Zhao Shenyang Date: Fri, 30 Jan 2015 01:25:06 +0800 Subject: [PATCH 3/3] Add Shenyang Zhao to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index e40a653..3815519 100644 --- a/AUTHORS +++ b/AUTHORS @@ -58,3 +58,4 @@ * Adam Schwalm * Ilia Choly * Shrayas Rajagopal +* Shenyang Zhao