add symbol? function to hy.core

`symbol?` will test if the input is an instance of HySymbol. It's useful when writing macros.
This commit is contained in:
Zhao Shenyang 2015-01-29 23:17:52 +08:00
parent 8d6f9c3d84
commit dafcc7ec70
2 changed files with 14 additions and 1 deletions

View File

@ -29,6 +29,7 @@
(import sys) (import sys)
(import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy._compat [long-type]]) ; long for python2, int for python3
(import [hy.models.cons [HyCons]] (import [hy.models.cons [HyCons]]
[hy.models.symbol [HySymbol]]
[hy.models.keyword [HyKeyword *keyword-prefix*]]) [hy.models.keyword [HyKeyword *keyword-prefix*]])
(import [hy.lex [LexException PrematureEndOfInput tokenize]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]])
@ -163,6 +164,10 @@
"Return True if x is float" "Return True if x is float"
(isinstance x float)) (isinstance x float))
(defn symbol? [s]
"Check whether s is a symbol"
(instance? HySymbol s))
(import [threading [Lock]]) (import [threading [Lock]])
(setv _gensym_counter 1234) (setv _gensym_counter 1234)
(setv _gensym_lock (Lock)) (setv _gensym_lock (Lock))
@ -414,5 +419,5 @@
interpose iterable? iterate iterator? keyword keyword? list* interpose iterable? iterate iterator? keyword keyword? list*
macroexpand macroexpand-1 map merge-with name neg? nil? none? macroexpand macroexpand-1 map merge-with name neg? nil? none?
nth numeric? odd? pos? range read remove repeat repeatedly 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]) take-while zero? zip zip_longest zipwith])

View File

@ -233,6 +233,14 @@
(assert-true (float? -3.2)) (assert-true (float? -3.2))
(assert-false (float? "foo"))) (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 [] (defn test-gensym []
"NATIVE: testing the gensym function" "NATIVE: testing the gensym function"
(import [hy.models.symbol [HySymbol]]) (import [hy.models.symbol [HySymbol]])