From 3a9cf486db3673ab7f5bbdecee5048592e66871c Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Tue, 1 Apr 2014 23:23:53 -0400 Subject: [PATCH] Add a keyword? function to detect keywords. Thanks to algernon for the function body; huge improvement. --- hy/core/language.hy | 7 ++++++- tests/native_tests/core.hy | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index cdef29e..0df4b44 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -44,6 +44,11 @@ "Check whether c can be used as a cons object" (instance? HyCons c)) +(defn keyword? [k] + "Check whether k is a keyword" + (and (instance? (type :foo) k) + (.startswith k (get :foo 0)))) + (defn cycle [coll] "Yield an infinite repetition of the items in coll" (setv seen []) @@ -354,7 +359,7 @@ (def *exports* '[calling-module-name coll? cons cons? cycle dec distinct disassemble drop drop-while empty? even? every? first filter flatten float? gensym identity inc instance? integer - integer? integer-char? iterable? iterate iterator? + integer? integer-char? iterable? iterate iterator? keyword? list* macroexpand macroexpand-1 neg? nil? none? nth numeric? odd? pos? remove repeat repeatedly rest second some string string? take take-nth take-while zero? zipwith]) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 15debf4..98e403d 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -478,3 +478,14 @@ (assert-equal (list res) [4 4 4]) (setv res (zipwith operator.sub [3 7 9] [1 2 4])) (assert-equal (list res) [2 5 5])) + +(defn test-is-keyword [] + "NATIVE: testing the keyword? function" + (assert (keyword? ':bar)) + (assert (keyword? ':baz)) + (assert (keyword? :bar)) + (assert (keyword? :baz)) + (assert (not (keyword? "foo"))) + (assert (not (keyword? ":foo"))) + (assert (not (keyword? 1))) + (assert (not (keyword? nil))))