From 0cd4df3898126bc121a0a32f00ac27749783ca02 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Fri, 5 Jan 2018 15:51:00 -0500 Subject: [PATCH] Remove def and standardize on setv --- NEWS | 1 + docs/contrib/walk.rst | 4 ++-- docs/extra/anaphoric.rst | 2 +- docs/language/api.rst | 20 ++++++++++---------- docs/language/core.rst | 8 ++++---- docs/style-guide.rst | 26 +++----------------------- hy/compiler.py | 2 +- hy/contrib/multi.hy | 16 ++++++++-------- hy/contrib/sequences.hy | 2 +- hy/core/language.hy | 16 ++++++++-------- tests/native_tests/contrib/walk.hy | 10 +++++----- tests/native_tests/core.hy | 4 ++-- tests/native_tests/language.hy | 14 +++++++------- tests/native_tests/mathematics.hy | 18 +++++++++--------- tests/native_tests/tag_macros.hy | 2 +- 15 files changed, 63 insertions(+), 82 deletions(-) diff --git a/NEWS b/NEWS index 83a9120..a3a7b3a 100644 --- a/NEWS +++ b/NEWS @@ -26,6 +26,7 @@ Changes from 0.13.0 * `#%` works on any expression and has a new `&kwargs` parameter `%**` * new `doc` macro and `#doc` tag macro * support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a` + * remove `def` [ Bug Fixes ] * Numeric literals are no longer parsed as symbols when followed by a dot diff --git a/docs/contrib/walk.rst b/docs/contrib/walk.rst index fe0eeea..1bd0fcc 100644 --- a/docs/contrib/walk.rst +++ b/docs/contrib/walk.rst @@ -48,7 +48,7 @@ each sub-form, uses ``f`` 's return value in place of the original. .. code-block:: hy => (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] ... (print "Walking:" x :sep "\n") ... x) @@ -128,7 +128,7 @@ each sub-form, uses ``f`` 's return value in place of the original. .. code-block:: hy => (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] ... (print "Walking:" x :sep "\n") ... x) diff --git a/docs/extra/anaphoric.rst b/docs/extra/anaphoric.rst index a630eae..bbd0e67 100644 --- a/docs/extra/anaphoric.rst +++ b/docs/extra/anaphoric.rst @@ -225,7 +225,7 @@ Returns a function which applies several forms in series from left to right. The .. code-block:: hy - => (def op (ap-compose (+ it 1) (* it 3))) + => (setv op (ap-compose (+ it 1) (* it 3))) => (op 2) 9 diff --git a/docs/language/api.rst b/docs/language/api.rst index a98f949..398563d 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -536,15 +536,15 @@ Gets help for macros or tag macros, respectively. 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: .. code-block:: clj - => (def names ["Alice" "Bob" "Charlie"]) + => (setv names ["Alice" "Bob" "Charlie"]) => (print names) [u'Alice', u'Bob', u'Charlie'] @@ -590,7 +590,7 @@ below: ... ... (defn speak [self] (print "Meow"))) - => (def spot (Cat)) + => (setv spot (Cat)) => (setv spot.colour "Black") 'Black' => (.speak spot) @@ -1077,8 +1077,8 @@ immediately. .. code-block:: hy - => (def collection (range 10)) - => (def filtered (genexpr x [x collection] (even? x))) + => (setv collection (range 10)) + => (setv filtered (genexpr x [x collection] (even? x))) => (list filtered) [0, 2, 4, 6, 8] @@ -1295,7 +1295,7 @@ passed to another function for filtering output. .. code-block:: clj - => (def people [{:name "Alice" :age 20} + => (setv people [{:name "Alice" :age 20} ... {:name "Bob" :age 25} ... {:name "Charlie" :age 50} ... {:name "Dave" :age 5}]) @@ -1359,7 +1359,7 @@ conditional expression. Some examples: .. code-block:: clj - => (def collection (range 10)) + => (setv collection (range 10)) => (list-comp x [x collection]) [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 - => (def collection (range 10)) + => (setv collection (range 10)) => (cut collection) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] diff --git a/docs/language/core.rst b/docs/language/core.rst index 3e9611b..8838153 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -66,11 +66,11 @@ chain the given functions together, so ``((comp g f) x)`` is equivalent to .. code-block:: hy - => (def example (comp str +)) + => (setv example (comp str +)) => (example 1 2 3) "6" - => (def simple (comp)) + => (setv simple (comp)) => (simple "hello") "hello" @@ -89,7 +89,7 @@ inverted. So, ``((complement f) x)`` is equivalent to ``(not (f x))``. .. code-block:: hy - => (def inverse (complement identity)) + => (setv inverse (complement identity)) => (inverse True) False => (inverse 1) @@ -155,7 +155,7 @@ the arguments given to it. .. code-block:: hy - => (def answer (constantly 42)) + => (setv answer (constantly 42)) => (answer) 42 => (answer 1 2 3) diff --git a/docs/style-guide.rst b/docs/style-guide.rst index 74c64d0..2d40326 100644 --- a/docs/style-guide.rst +++ b/docs/style-guide.rst @@ -120,26 +120,6 @@ Layout & Indentation 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. For instance, the fact that the former of these two examples works is just because the compiler isn't overly strict. In reality, the @@ -164,17 +144,17 @@ Coding Style .. code-block:: clj ;; Preferred - (def *names* + (setv *names* (with [f (open "names.txt")] (-> (.read f) (.strip) (.replace "\"" "") (.split ",") (sorted)))) ;; Not so good - (def *names* + (setv *names* (with [f (open "names.txt")] (sorted (.split "," (.replace "\"" "" (.strip (.read f))))))) ;; Probably not a good idea - (defn square? [x] + (setv square? [x] (->> 2 (pow (int (sqrt x))) (= x))) diff --git a/hy/compiler.py b/hy/compiler.py index 1a7c216..f1dee44 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1784,7 +1784,7 @@ class HyASTCompiler(object): expression, func=func.expr, args=args, keywords=keywords, starargs=oldpy_star, kwargs=oldpy_kw) - @builds("def", "setv") + @builds("setv") def compile_def_expression(self, expression): root = expression.pop(0) if not expression: diff --git a/hy/contrib/multi.hy b/hy/contrib/multi.hy index c0cee3f..44d8d80 100644 --- a/hy/contrib/multi.hy +++ b/hy/contrib/multi.hy @@ -74,20 +74,20 @@ (, (get l 0) (cut l 1))) (defmacro defn [name &rest bodies] - (def arity-overloaded? (fn [bodies] - (if (isinstance (first bodies) HyString) - (arity-overloaded? (rest bodies)) - (isinstance (first bodies) HyExpression)))) + (setv arity-overloaded? (fn [bodies] + (if (isinstance (first bodies) HyString) + (arity-overloaded? (rest bodies)) + (isinstance (first bodies) HyExpression)))) (if (arity-overloaded? bodies) (do - (def comment (HyString)) + (setv comment (HyString)) (if (= (type (first bodies)) HyString) - (def [comment bodies] (head-tail bodies))) - (def ret `(do)) + (setv [comment bodies] (head-tail bodies))) + (setv ret `(do)) (.append ret '(import [hy.contrib.multi [MultiDispatch]])) (for [body bodies] - (def [let-binds body] (head-tail body)) + (setv [let-binds body] (head-tail body)) (.append ret `(with-decorator MultiDispatch (defn ~name ~let-binds ~comment ~@body)))) ret) diff --git a/hy/contrib/sequences.hy b/hy/contrib/sequences.hy index 7157b67..b18002f 100644 --- a/hy/contrib/sequences.hy +++ b/hy/contrib/sequences.hy @@ -55,7 +55,7 @@ `(Sequence (fn ~param (do ~@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 [] "raise IndexError exception to signal end of sequence" diff --git a/hy/core/language.hy b/hy/core/language.hy index b202bc2..c9a3fde 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -93,7 +93,7 @@ If the second argument `codegen` is true, generate python code instead." (.add seen val))))) (if-python2 - (def + (setv remove itertools.ifilterfalse zip-longest itertools.izip_longest ;; not builtin in Python3 @@ -104,7 +104,7 @@ If the second argument `codegen` is true, generate python code instead." map itertools.imap range xrange zip itertools.izip) - (def + (setv remove itertools.filterfalse zip-longest itertools.zip_longest ;; was builtin in Python2 @@ -132,16 +132,16 @@ function with keyword arguments, which isn't supported by Python 3's `exec`." (none? $locals) (setv $locals $globals)) (exec* $code $globals $locals)) - (def exec exec)) + (setv exec exec)) ;; infinite iterators -(def +(setv count itertools.count cycle itertools.cycle repeat itertools.repeat) ;; shortest-terminating iterators -(def +(setv *map itertools.starmap chain itertools.chain compress itertools.compress @@ -152,7 +152,7 @@ function with keyword arguments, which isn't supported by Python 3's `exec`." tee itertools.tee) ;; combinatoric iterators -(def +(setv combinations itertools.combinations multicombinations itertools.combinations_with_replacement 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." (= (% n 2) 1)) -(def -sentinel (object)) +(setv -sentinel (object)) (defn partition [coll &optional [n 2] step [fillvalue -sentinel]] "Chunk `coll` into `n`-tuples (pairs by default). @@ -485,7 +485,7 @@ Even objects with the __name__ magic will work." False (or a b))) -(def *exports* +(setv *exports* '[*map accumulate butlast calling-module-name chain coll? combinations comp complement compress cons cons? constantly count cycle dec distinct disassemble drop drop-last drop-while empty? eval even? every? exec first diff --git a/tests/native_tests/contrib/walk.hy b/tests/native_tests/contrib/walk.hy index 689cd2a..14bf5e9 100644 --- a/tests/native_tests/contrib/walk.hy +++ b/tests/native_tests/contrib/walk.hy @@ -7,11 +7,11 @@ (import pytest) -(def walk-form '(print {"foo" "bar" - "array" [1 2 3 [4]] - "something" (+ 1 2 3 4) - "cons!" (cons 1 2) - "quoted?" '(foo)})) +(setv walk-form '(print {"foo" "bar" + "array" [1 2 3 [4]] + "something" (+ 1 2 3 4) + "cons!" (cons 1 2) + "quoted?" '(foo)})) (defn collector [acc x] (.append acc x) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index e5af611..f4d599c 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -651,14 +651,14 @@ result['y in globals'] = 'y' in globals()") (defn test-complement [] "NATIVE: test complement" - (def helper (complement identity)) + (setv helper (complement identity)) (assert-true (helper False)) (assert-false (helper True))) (defn test-constantly [] "NATIVE: test constantly" - (def helper (constantly 42)) + (setv helper (constantly 42)) (assert-true (= (helper) 42)) (assert-true (= (helper 1 2 3) 42)) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 703e083..5ac9458 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -189,10 +189,10 @@ (defn test-alias-names-in-errors [] "NATIVE: tests that native aliases show the correct names in errors" - (try (eval '(setv 1 2 3)) - (except [e [Exception]] (assert (in "setv" (str e))))) - (try (eval '(def 1 2 3)) - (except [e [Exception]] (assert (in "def" (str e)))))) + (try (eval '(list-comp 1 2 3 4)) + (except [e [Exception]] (assert (in "list_comp" (str e))))) + (try (eval '(set-comp 1 2 3 4)) + (except [e [Exception]] (assert (in "set_comp" (str e)))))) (defn test-for-loop [] @@ -1631,17 +1631,17 @@ macros() (import [io [StringIO]])) (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 (isinstance (read stdin-buffer) HyExpression)) "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)) 1)) "EOF test" - (def stdin-buffer (StringIO "(+ 2 2)")) + (setv stdin-buffer (StringIO "(+ 2 2)")) (read stdin-buffer) (try (read stdin-buffer) diff --git a/tests/native_tests/mathematics.hy b/tests/native_tests/mathematics.hy index 83f39ab..606cc9f 100644 --- a/tests/native_tests/mathematics.hy +++ b/tests/native_tests/mathematics.hy @@ -177,17 +177,17 @@ (.append result result-row)) result)]) -(def first-test-matrix (HyTestMatrix [[1 2 3] - [4 5 6] - [7 8 9]])) +(setv first-test-matrix (HyTestMatrix [[1 2 3] + [4 5 6] + [7 8 9]])) -(def second-test-matrix (HyTestMatrix [[2 0 0] - [0 2 0] - [0 0 2]])) +(setv second-test-matrix (HyTestMatrix [[2 0 0] + [0 2 0] + [0 0 2]])) -(def product-of-test-matrices (HyTestMatrix [[ 2 4 6] - [ 8 10 12] - [14 16 18]])) +(setv product-of-test-matrices (HyTestMatrix [[ 2 4 6] + [ 8 10 12] + [14 16 18]])) (defn test-matmul [] "NATIVE: test matrix multiplication" diff --git a/tests/native_tests/tag_macros.hy b/tests/native_tests/tag_macros.hy index 3ba7182..39eb3ab 100644 --- a/tests/native_tests/tag_macros.hy +++ b/tests/native_tests/tag_macros.hy @@ -89,7 +89,7 @@ (deftag t [expr] `(, ~@expr)) - (def a #t[1 2 3]) + (setv a #t[1 2 3]) (assert (= (type a) tuple)) (assert (= (, 1 2 3) a)))