2019-02-07 14:57:35 +01:00
|
|
|
;; Copyright 2019 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
Give `require` the same features as `import` (#1142)
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
2016-11-03 08:35:58 +01:00
|
|
|
(require [hy.contrib.loop [loop]])
|
2014-01-09 04:54:49 +01:00
|
|
|
(import sys)
|
|
|
|
|
|
|
|
(defn tco-sum [x y]
|
|
|
|
(loop [[x x] [y y]]
|
|
|
|
(cond
|
|
|
|
[(> y 0) (recur (inc x) (dec y))]
|
|
|
|
[(< y 0) (recur (dec x) (inc y))]
|
|
|
|
[True x])))
|
|
|
|
|
|
|
|
(defn non-tco-sum [x y]
|
|
|
|
(cond
|
|
|
|
[(> y 0) (inc (non-tco-sum x (dec y)))]
|
|
|
|
[(< y 0) (dec (non-tco-sum x (inc y)))]
|
|
|
|
[True x]))
|
|
|
|
|
|
|
|
(defn test-loop []
|
|
|
|
;; non-tco-sum should fail
|
|
|
|
(try
|
|
|
|
(setv n (non-tco-sum 100 10000))
|
2015-08-09 08:41:11 +02:00
|
|
|
(except [e RuntimeError]
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert True))
|
2014-01-09 04:54:49 +01:00
|
|
|
(else
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert False)))
|
2014-01-09 04:54:49 +01:00
|
|
|
|
|
|
|
;; tco-sum should not fail
|
|
|
|
(try
|
|
|
|
(setv n (tco-sum 100 10000))
|
2015-08-09 08:41:11 +02:00
|
|
|
(except [e RuntimeError]
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert False))
|
2014-01-09 04:54:49 +01:00
|
|
|
(else
|
|
|
|
(assert (= n 10100)))))
|
|
|
|
|
|
|
|
(defn test-recur-in-wrong-loc []
|
|
|
|
(defn bad-recur [n]
|
|
|
|
(loop [[i n]]
|
|
|
|
(if (= i 0)
|
|
|
|
0
|
|
|
|
(inc (recur (dec i))))))
|
|
|
|
|
|
|
|
(try
|
|
|
|
(bad-recur 3)
|
2015-08-09 08:41:11 +02:00
|
|
|
(except [e TypeError]
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert True))
|
2014-01-09 04:54:49 +01:00
|
|
|
(else
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert False))))
|
2017-03-31 00:49:10 +02:00
|
|
|
|
|
|
|
(defn test-recur-string []
|
|
|
|
"test that `loop` doesn't touch a string named `recur`"
|
|
|
|
(assert (= (loop [] (+ "recur" "1")) "recur1")))
|