Remove def and standardize on setv

This commit is contained in:
Simon Gomizelj 2018-01-05 15:51:00 -05:00
parent 8394461658
commit 0cd4df3898
15 changed files with 63 additions and 82 deletions

1
NEWS
View File

@ -26,6 +26,7 @@ Changes from 0.13.0
* `#%` works on any expression and has a new `&kwargs` parameter `%**` * `#%` works on any expression and has a new `&kwargs` parameter `%**`
* new `doc` macro and `#doc` tag macro * new `doc` macro and `#doc` tag macro
* support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a` * support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a`
* remove `def`
[ Bug Fixes ] [ Bug Fixes ]
* Numeric literals are no longer parsed as symbols when followed by a dot * Numeric literals are no longer parsed as symbols when followed by a dot

View File

@ -48,7 +48,7 @@ each sub-form, uses ``f`` 's return value in place of the original.
.. code-block:: hy .. code-block:: hy
=> (import [hy.contrib.walk [postwalk]]) => (import [hy.contrib.walk [postwalk]])
=> (def trail '([1 2 3] [4 [5 6 [7]]])) => (setv trail '([1 2 3] [4 [5 6 [7]]]))
=> (defn walking [x] => (defn walking [x]
... (print "Walking:" x :sep "\n") ... (print "Walking:" x :sep "\n")
... x) ... x)
@ -128,7 +128,7 @@ each sub-form, uses ``f`` 's return value in place of the original.
.. code-block:: hy .. code-block:: hy
=> (import [hy.contrib.walk [prewalk]]) => (import [hy.contrib.walk [prewalk]])
=> (def trail '([1 2 3] [4 [5 6 [7]]])) => (setv trail '([1 2 3] [4 [5 6 [7]]]))
=> (defn walking [x] => (defn walking [x]
... (print "Walking:" x :sep "\n") ... (print "Walking:" x :sep "\n")
... x) ... x)

View File

@ -225,7 +225,7 @@ Returns a function which applies several forms in series from left to right. The
.. code-block:: hy .. code-block:: hy
=> (def op (ap-compose (+ it 1) (* it 3))) => (setv op (ap-compose (+ it 1) (* it 3)))
=> (op 2) => (op 2)
9 9

View File

@ -536,15 +536,15 @@ Gets help for macros or tag macros, respectively.
Gets help for a tag macro function available in this module. Gets help for a tag macro function available in this module.
def / setv setv
---------- ----
``def`` and ``setv`` are used to bind a value, object, or function to a symbol. ``setv`` is used to bind a value, object, or function to a symbol.
For example: For example:
.. code-block:: clj .. code-block:: clj
=> (def names ["Alice" "Bob" "Charlie"]) => (setv names ["Alice" "Bob" "Charlie"])
=> (print names) => (print names)
[u'Alice', u'Bob', u'Charlie'] [u'Alice', u'Bob', u'Charlie']
@ -590,7 +590,7 @@ below:
... ...
... (defn speak [self] (print "Meow"))) ... (defn speak [self] (print "Meow")))
=> (def spot (Cat)) => (setv spot (Cat))
=> (setv spot.colour "Black") => (setv spot.colour "Black")
'Black' 'Black'
=> (.speak spot) => (.speak spot)
@ -1077,8 +1077,8 @@ immediately.
.. code-block:: hy .. code-block:: hy
=> (def collection (range 10)) => (setv collection (range 10))
=> (def filtered (genexpr x [x collection] (even? x))) => (setv filtered (genexpr x [x collection] (even? x)))
=> (list filtered) => (list filtered)
[0, 2, 4, 6, 8] [0, 2, 4, 6, 8]
@ -1295,7 +1295,7 @@ passed to another function for filtering output.
.. code-block:: clj .. code-block:: clj
=> (def people [{:name "Alice" :age 20} => (setv people [{:name "Alice" :age 20}
... {:name "Bob" :age 25} ... {:name "Bob" :age 25}
... {:name "Charlie" :age 50} ... {:name "Charlie" :age 50}
... {:name "Dave" :age 5}]) ... {:name "Dave" :age 5}])
@ -1359,7 +1359,7 @@ conditional expression. Some examples:
.. code-block:: clj .. code-block:: clj
=> (def collection (range 10)) => (setv collection (range 10))
=> (list-comp x [x collection]) => (list-comp x [x collection])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@ -1653,7 +1653,7 @@ counted starting from the end of the list. Some example usage:
.. code-block:: clj .. code-block:: clj
=> (def collection (range 10)) => (setv collection (range 10))
=> (cut collection) => (cut collection)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

View File

@ -66,11 +66,11 @@ chain the given functions together, so ``((comp g f) x)`` is equivalent to
.. code-block:: hy .. code-block:: hy
=> (def example (comp str +)) => (setv example (comp str +))
=> (example 1 2 3) => (example 1 2 3)
"6" "6"
=> (def simple (comp)) => (setv simple (comp))
=> (simple "hello") => (simple "hello")
"hello" "hello"
@ -89,7 +89,7 @@ inverted. So, ``((complement f) x)`` is equivalent to ``(not (f x))``.
.. code-block:: hy .. code-block:: hy
=> (def inverse (complement identity)) => (setv inverse (complement identity))
=> (inverse True) => (inverse True)
False False
=> (inverse 1) => (inverse 1)
@ -155,7 +155,7 @@ the arguments given to it.
.. code-block:: hy .. code-block:: hy
=> (def answer (constantly 42)) => (setv answer (constantly 42))
=> (answer) => (answer)
42 42
=> (answer 1 2 3) => (answer 1 2 3)

View File

@ -120,26 +120,6 @@ Layout & Indentation
Coding Style Coding Style
============ ============
+ As a convention, try not to use ``def`` for anything other than global
variables; use ``setv`` inside functions, loops, etc.
.. code-block:: clj
;; Good (and preferred)
(def *limit* 400000)
(defn fibs [a b]
(while True
(yield a)
(setv (, a b) (, b (+ a b)))))
;; Bad (and not preferred)
(defn fibs [a b]
(while True
(yield a)
(def (, a b) (, b (+ a b)))))
+ Do not use s-expression syntax where vector syntax is intended. + Do not use s-expression syntax where vector syntax is intended.
For instance, the fact that the former of these two examples works For instance, the fact that the former of these two examples works
is just because the compiler isn't overly strict. In reality, the is just because the compiler isn't overly strict. In reality, the
@ -164,17 +144,17 @@ Coding Style
.. code-block:: clj .. code-block:: clj
;; Preferred ;; Preferred
(def *names* (setv *names*
(with [f (open "names.txt")] (with [f (open "names.txt")]
(-> (.read f) (.strip) (.replace "\"" "") (.split ",") (sorted)))) (-> (.read f) (.strip) (.replace "\"" "") (.split ",") (sorted))))
;; Not so good ;; Not so good
(def *names* (setv *names*
(with [f (open "names.txt")] (with [f (open "names.txt")]
(sorted (.split "," (.replace "\"" "" (.strip (.read f))))))) (sorted (.split "," (.replace "\"" "" (.strip (.read f)))))))
;; Probably not a good idea ;; Probably not a good idea
(defn square? [x] (setv square? [x]
(->> 2 (pow (int (sqrt x))) (= x))) (->> 2 (pow (int (sqrt x))) (= x)))

View File

@ -1784,7 +1784,7 @@ class HyASTCompiler(object):
expression, func=func.expr, args=args, keywords=keywords, expression, func=func.expr, args=args, keywords=keywords,
starargs=oldpy_star, kwargs=oldpy_kw) starargs=oldpy_star, kwargs=oldpy_kw)
@builds("def", "setv") @builds("setv")
def compile_def_expression(self, expression): def compile_def_expression(self, expression):
root = expression.pop(0) root = expression.pop(0)
if not expression: if not expression:

View File

@ -74,20 +74,20 @@
(, (get l 0) (cut l 1))) (, (get l 0) (cut l 1)))
(defmacro defn [name &rest bodies] (defmacro defn [name &rest bodies]
(def arity-overloaded? (fn [bodies] (setv arity-overloaded? (fn [bodies]
(if (isinstance (first bodies) HyString) (if (isinstance (first bodies) HyString)
(arity-overloaded? (rest bodies)) (arity-overloaded? (rest bodies))
(isinstance (first bodies) HyExpression)))) (isinstance (first bodies) HyExpression))))
(if (arity-overloaded? bodies) (if (arity-overloaded? bodies)
(do (do
(def comment (HyString)) (setv comment (HyString))
(if (= (type (first bodies)) HyString) (if (= (type (first bodies)) HyString)
(def [comment bodies] (head-tail bodies))) (setv [comment bodies] (head-tail bodies)))
(def ret `(do)) (setv ret `(do))
(.append ret '(import [hy.contrib.multi [MultiDispatch]])) (.append ret '(import [hy.contrib.multi [MultiDispatch]]))
(for [body bodies] (for [body bodies]
(def [let-binds body] (head-tail body)) (setv [let-binds body] (head-tail body))
(.append ret (.append ret
`(with-decorator MultiDispatch (defn ~name ~let-binds ~comment ~@body)))) `(with-decorator MultiDispatch (defn ~name ~let-binds ~comment ~@body))))
ret) ret)

View File

@ -55,7 +55,7 @@
`(Sequence (fn ~param (do ~@seq-code)))) `(Sequence (fn ~param (do ~@seq-code))))
(defmacro defseq [seq-name param &rest seq-code] (defmacro defseq [seq-name param &rest seq-code]
`(def ~seq-name (Sequence (fn ~param (do ~@seq-code))))) `(setv ~seq-name (Sequence (fn ~param (do ~@seq-code)))))
(defn end-sequence [] (defn end-sequence []
"raise IndexError exception to signal end of sequence" "raise IndexError exception to signal end of sequence"

View File

@ -93,7 +93,7 @@ If the second argument `codegen` is true, generate python code instead."
(.add seen val))))) (.add seen val)))))
(if-python2 (if-python2
(def (setv
remove itertools.ifilterfalse remove itertools.ifilterfalse
zip-longest itertools.izip_longest zip-longest itertools.izip_longest
;; not builtin in Python3 ;; not builtin in Python3
@ -104,7 +104,7 @@ If the second argument `codegen` is true, generate python code instead."
map itertools.imap map itertools.imap
range xrange range xrange
zip itertools.izip) zip itertools.izip)
(def (setv
remove itertools.filterfalse remove itertools.filterfalse
zip-longest itertools.zip_longest zip-longest itertools.zip_longest
;; was builtin in Python2 ;; was builtin in Python2
@ -132,16 +132,16 @@ function with keyword arguments, which isn't supported by Python 3's `exec`."
(none? $locals) (none? $locals)
(setv $locals $globals)) (setv $locals $globals))
(exec* $code $globals $locals)) (exec* $code $globals $locals))
(def exec exec)) (setv exec exec))
;; infinite iterators ;; infinite iterators
(def (setv
count itertools.count count itertools.count
cycle itertools.cycle cycle itertools.cycle
repeat itertools.repeat) repeat itertools.repeat)
;; shortest-terminating iterators ;; shortest-terminating iterators
(def (setv
*map itertools.starmap *map itertools.starmap
chain itertools.chain chain itertools.chain
compress itertools.compress compress itertools.compress
@ -152,7 +152,7 @@ function with keyword arguments, which isn't supported by Python 3's `exec`."
tee itertools.tee) tee itertools.tee)
;; combinatoric iterators ;; combinatoric iterators
(def (setv
combinations itertools.combinations combinations itertools.combinations
multicombinations itertools.combinations_with_replacement multicombinations itertools.combinations_with_replacement
permutations itertools.permutations permutations itertools.permutations
@ -359,7 +359,7 @@ If a key occurs in more than one map, the mapping(s) from the latter
"Check if `n` is an odd number." "Check if `n` is an odd number."
(= (% n 2) 1)) (= (% n 2) 1))
(def -sentinel (object)) (setv -sentinel (object))
(defn partition [coll &optional [n 2] step [fillvalue -sentinel]] (defn partition [coll &optional [n 2] step [fillvalue -sentinel]]
"Chunk `coll` into `n`-tuples (pairs by default). "Chunk `coll` into `n`-tuples (pairs by default).
@ -485,7 +485,7 @@ Even objects with the __name__ magic will work."
False False
(or a b))) (or a b)))
(def *exports* (setv *exports*
'[*map accumulate butlast calling-module-name chain coll? combinations '[*map accumulate butlast calling-module-name chain coll? combinations
comp complement compress cons cons? constantly count cycle dec distinct comp complement compress cons cons? constantly count cycle dec distinct
disassemble drop drop-last drop-while empty? eval even? every? exec first disassemble drop drop-last drop-while empty? eval even? every? exec first

View File

@ -7,11 +7,11 @@
(import pytest) (import pytest)
(def walk-form '(print {"foo" "bar" (setv walk-form '(print {"foo" "bar"
"array" [1 2 3 [4]] "array" [1 2 3 [4]]
"something" (+ 1 2 3 4) "something" (+ 1 2 3 4)
"cons!" (cons 1 2) "cons!" (cons 1 2)
"quoted?" '(foo)})) "quoted?" '(foo)}))
(defn collector [acc x] (defn collector [acc x]
(.append acc x) (.append acc x)

View File

@ -651,14 +651,14 @@ result['y in globals'] = 'y' in globals()")
(defn test-complement [] (defn test-complement []
"NATIVE: test complement" "NATIVE: test complement"
(def helper (complement identity)) (setv helper (complement identity))
(assert-true (helper False)) (assert-true (helper False))
(assert-false (helper True))) (assert-false (helper True)))
(defn test-constantly [] (defn test-constantly []
"NATIVE: test constantly" "NATIVE: test constantly"
(def helper (constantly 42)) (setv helper (constantly 42))
(assert-true (= (helper) 42)) (assert-true (= (helper) 42))
(assert-true (= (helper 1 2 3) 42)) (assert-true (= (helper 1 2 3) 42))

View File

@ -189,10 +189,10 @@
(defn test-alias-names-in-errors [] (defn test-alias-names-in-errors []
"NATIVE: tests that native aliases show the correct names in errors" "NATIVE: tests that native aliases show the correct names in errors"
(try (eval '(setv 1 2 3)) (try (eval '(list-comp 1 2 3 4))
(except [e [Exception]] (assert (in "setv" (str e))))) (except [e [Exception]] (assert (in "list_comp" (str e)))))
(try (eval '(def 1 2 3)) (try (eval '(set-comp 1 2 3 4))
(except [e [Exception]] (assert (in "def" (str e)))))) (except [e [Exception]] (assert (in "set_comp" (str e))))))
(defn test-for-loop [] (defn test-for-loop []
@ -1631,17 +1631,17 @@ macros()
(import [io [StringIO]])) (import [io [StringIO]]))
(import [hy.models [HyExpression]]) (import [hy.models [HyExpression]])
(def stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)")) (setv stdin-buffer (StringIO "(+ 2 2)\n(- 2 2)"))
(assert (= (eval (read stdin-buffer)) 4)) (assert (= (eval (read stdin-buffer)) 4))
(assert (isinstance (read stdin-buffer) HyExpression)) (assert (isinstance (read stdin-buffer) HyExpression))
"Multiline test" "Multiline test"
(def stdin-buffer (StringIO "(\n+\n41\n1\n)\n(-\n2\n1\n)")) (setv stdin-buffer (StringIO "(\n+\n41\n1\n)\n(-\n2\n1\n)"))
(assert (= (eval (read stdin-buffer)) 42)) (assert (= (eval (read stdin-buffer)) 42))
(assert (= (eval (read stdin-buffer)) 1)) (assert (= (eval (read stdin-buffer)) 1))
"EOF test" "EOF test"
(def stdin-buffer (StringIO "(+ 2 2)")) (setv stdin-buffer (StringIO "(+ 2 2)"))
(read stdin-buffer) (read stdin-buffer)
(try (try
(read stdin-buffer) (read stdin-buffer)

View File

@ -177,17 +177,17 @@
(.append result result-row)) (.append result result-row))
result)]) result)])
(def first-test-matrix (HyTestMatrix [[1 2 3] (setv first-test-matrix (HyTestMatrix [[1 2 3]
[4 5 6] [4 5 6]
[7 8 9]])) [7 8 9]]))
(def second-test-matrix (HyTestMatrix [[2 0 0] (setv second-test-matrix (HyTestMatrix [[2 0 0]
[0 2 0] [0 2 0]
[0 0 2]])) [0 0 2]]))
(def product-of-test-matrices (HyTestMatrix [[ 2 4 6] (setv product-of-test-matrices (HyTestMatrix [[ 2 4 6]
[ 8 10 12] [ 8 10 12]
[14 16 18]])) [14 16 18]]))
(defn test-matmul [] (defn test-matmul []
"NATIVE: test matrix multiplication" "NATIVE: test matrix multiplication"

View File

@ -89,7 +89,7 @@
(deftag t [expr] (deftag t [expr]
`(, ~@expr)) `(, ~@expr))
(def a #t[1 2 3]) (setv a #t[1 2 3])
(assert (= (type a) tuple)) (assert (= (type a) tuple))
(assert (= (, 1 2 3) a))) (assert (= (, 1 2 3) a)))