Added name and keyword functions to core

This commit is contained in:
schuster-rainer 2014-03-01 00:23:49 +01:00
parent 8fe3b0edb1
commit 7fe997e9f7
3 changed files with 59 additions and 5 deletions

View File

@ -25,7 +25,8 @@
(import [hy._compat [long-type]]) ; long for python2, int for python3
(import [hy.models.cons [HyCons]])
(import [hy.models.cons [HyCons]]
[hy.models.keyword [HyKeyword KEYWORD_PREFIX]])
(defn _numeric-check [x]
@ -351,10 +352,36 @@
(import functools)
(map (functools.partial (fn [f args] (apply f args)) func) (apply zip lists))))
(defn hyify [text]
"Convert text to match hy identifier"
(.replace (string text) "_" "-"))
(defn keyword [value]
"Create a keyword from the given value. Strings numbers and even objects
with the __name__ magic will work"
(if (and (string? value) (value.startswith KEYWORD_PREFIX))
(hyify value)
(if (string? value)
(HyKeyword (+ ":" (hyify value)))
(try
(hyify (.__name__ value))
(catch [] (HyKeyword (+ ":" (string value))))))))
(defn name [value]
"Convert the given value to a string. Keyword special character will be stripped.
String will be used as is. Even objects with the __name__ magic will work"
(if (and (string? value) (value.startswith KEYWORD_PREFIX))
(hyify (slice value 2))
(if (string? value)
(hyify value)
(try
(hyify (. value __name__))
(catch [] (string value))))))
(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?
list* macroexpand macroexpand-1 neg? nil? none? nth
integer? integer-char? iterable? iterate iterator? keyword
list* macroexpand macroexpand-1 name neg? nil? none? nth
numeric? odd? pos? remove repeat repeatedly rest second
some string string? take take-nth take-while zero? zipwith])

View File

@ -22,6 +22,7 @@ from __future__ import unicode_literals
from hy.models import HyObject
from hy._compat import str_type
KEYWORD_PREFIX = "\uFDD0"
class HyKeyword(HyObject, str_type):
"""Generic Hy Keyword object. It's either a ``str`` or a ``unicode``,
@ -29,8 +30,8 @@ class HyKeyword(HyObject, str_type):
"""
def __new__(cls, value):
if not value.startswith("\uFDD0"):
value = "\uFDD0" + value
if not value.startswith(KEYWORD_PREFIX):
value = KEYWORD_PREFIX + value
obj = str_type.__new__(cls, value)
return obj

View File

@ -988,3 +988,29 @@
"NATIVE: test keyword quoting magic"
(assert (= :foo "\ufdd0:foo"))
(assert (= `:foo "\ufdd0:foo")))
(defn test-keyword-creation []
"NATIVE: Test keyword creation"
(assert (= (keyword "foo") :foo))
(assert (= (keyword "foo_bar") :foo-bar))
(assert (= (keyword `foo) :foo))
(assert (= (keyword `foo-bar) :foo-bar))
(assert (= (keyword 'foo) :foo))
(assert (= (keyword 'foo-bar) :foo-bar))
(assert (= (keyword 1) :1))
(assert (= (keyword 1.0) :1.0))
(assert (= (keyword :foo_bar) :foo-bar)))
(defn test-name-conversion []
"NATIVE: Test name conversion"
(assert (= (name "foo") "foo"))
(assert (= (name "foo_bar") "foo-bar"))
(assert (= (name `foo) "foo"))
(assert (= (name `foo_bar) "foo-bar"))
(assert (= (name 'foo) "foo"))
(assert (= (name 'foo_bar) "foo-bar"))
(assert (= (name 1) "1"))
(assert (= (name 1.0) "1.0"))
(assert (= (name :foo) "foo"))
(assert (= (name :foo_bar) "foo-bar"))
(assert (= (name test-name-conversion) "test-name-conversion")))