Merge pull request #1483 from vodik/remove-def

Just remove `def`
This commit is contained in:
Tuukka Turto 2018-01-31 08:54:59 +02:00 committed by GitHub
commit cf87de5cc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 `%**`
* 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

View File

@ -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)

View File

@ -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

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.
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]

View File

@ -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)

View File

@ -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)))

View File

@ -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:

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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)

View File

@ -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))

View File

@ -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 []
@ -1693,17 +1693,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)

View File

@ -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"

View File

@ -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)))