Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A big let and with change #898

Merged
merged 2 commits into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,17 @@ Examples of usage:

.. code-block:: clj

=>(let [[collection {}]]
=>(let [collection {}]
... (assoc collection "Dog" "Bark")
... (print collection))
{u'Dog': u'Bark'}

=>(let [[collection {}]]
=>(let [collection {}]
... (assoc collection "Dog" "Bark" "Cat" "Meow")
... (print collection))
{u'Cat': u'Meow', u'Dog': u'Bark'}

=>(let [[collection [1 2 3 4]]]
=>(let [collection [1 2 3 4]]
... (assoc collection 2 None)
... (print collection))
[1, 2, None, 4]
Expand Down Expand Up @@ -463,8 +463,8 @@ Parameters may have the following keywords in front of them:
.. code-block:: clj

=> (defn zig-zag-sum [&rest numbers]
(let [[odd-numbers (list-comp x [x numbers] (odd? x))]
[even-numbers (list-comp x [x numbers] (even? x))]]
(let [odd-numbers (list-comp x [x numbers] (odd? x))
even-numbers (list-comp x [x numbers] (even? x))]
(- (sum odd-numbers) (sum even-numbers))))

=> (zig-zag-sum)
Expand All @@ -486,7 +486,7 @@ Parameters may have the following keywords in front of them:
.. code-block:: clj

=> (defn compare [a b &kwonly keyfn [reverse false]]
... (let [[result (keyfn a b)]]
... (let [result (keyfn a b)]
... (if (not reverse)
... result
... (- result))))
Expand Down Expand Up @@ -786,8 +786,8 @@ list. Example usage:

.. code-block:: clj

=> (let [[animals {"dog" "bark" "cat" "meow"}]
... [numbers ["zero" "one" "two" "three"]]]
=> (let [animals {"dog" "bark" "cat" "meow"}
... numbers ["zero" "one" "two" "three"]]
... (print (get animals "dog"))
... (print (get numbers 2)))
bark
Expand Down Expand Up @@ -988,30 +988,24 @@ example showcases this behaviour:

.. code-block:: clj

=> (let [[x 5]] (print x)
... (let [[x 6]] (print x))
=> (let [x 5] (print x)
... (let [x 6] (print x))
... (print x))
5
6
5

The ``let`` macro takes two parameters: a vector defining *variables* and the
*body* which gets executed. *variables* is a vector where each element is either
a single variable or a vector defining a variable value pair. In the case of a
single variable, it is assigned value ``None``; otherwise, the supplied value is
used.

.. code-block:: clj

=> (let [x [y 5]] (print x y))
None 5
The ``let`` macro takes two parameters: a vector defining *variables*
and the *body* which gets executed. *variables* is a vector of
variable and value pairs.

Note that the variable assignments are executed one by one, from left to right.
The following example takes advantage of this:

.. code-block:: clj

=> (let [[x 5] [y (+ x 1)]] (print x y))
=> (let [x 5
y (+ x 1)] (print x y))
5 6


Expand Down Expand Up @@ -1050,15 +1044,15 @@ to modify variables through nested ``let`` or ``fn`` scopes:

.. code-block:: clj

(let [[x 0]]
(let [x 0]
(for [y (range 10)]
(let [[z (inc y)]]
(let [z (inc y)]
(nonlocal x) ; allow the setv to "jump scope" to resolve x
(setv x (+ x y))))
x)

(defn some-function []
(let [[x 0]]
(let [x 0]
(register-some-callback
(fn [stuff]
(nonlocal x)
Expand Down Expand Up @@ -1369,18 +1363,18 @@ manner. The archetypical example of using ``with`` is when processing files.

.. code-block:: clj

(with [[arg (expr)]] block)
(with [arg (expr)] block)

(with [[(expr)]] block)
(with [(expr)] block)

(with [[arg (expr)] [(expr)]] block)
(with [arg (expr) (expr)] block)

The following example will open the ``NEWS`` file and print its content to the
screen. The file is automatically closed after it has been processed.

.. code-block:: clj

(with [[f (open "NEWS")]] (print (.read f)))
(with [f (open "NEWS")] (print (.read f)))


with-decorator
Expand Down Expand Up @@ -1467,9 +1461,9 @@ expands to:

.. code-block:: hy

(let [[a (gensym)
[b (gensym)
[c (gensym)]]
(let [a (gensym)
b (gensym)
c (gensym)]
...)

.. seealso::
Expand Down
4 changes: 2 additions & 2 deletions docs/language/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1100,10 +1100,10 @@ if *from-file* ends before a complete expression can be parsed.
=> ; assuming "example.hy" contains:
=> ; (print "hello")
=> ; (print "hyfriends!")
=> (with [[f (open "example.hy")]]
=> (with [f (open "example.hy")]
... (try
... (while true
... (let [[exp (read f)]]
... (let [exp (read f)]
... (do
... (print "OHY" exp)
... (eval exp))))
Expand Down
16 changes: 8 additions & 8 deletions docs/language/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ A first pass might be something like:
.. code-block:: hy

(defmacro nif [expr pos-form zero-form neg-form]
`(let [[obscure-name ~expr]]
`(let [obscure-name ~expr]
(cond [(pos? obscure-name) ~pos-form]
[(zero? obscure-name) ~zero-form]
[(neg? obscure-name) ~neg-form])))
Expand All @@ -396,8 +396,8 @@ such an occasion. A much better version of ``nif`` would be:
.. code-block:: hy

(defmacro nif [expr pos-form zero-form neg-form]
(let [[g (gensym)]]
`(let [[~g ~expr]]
(let [g (gensym)]
`(let [~g ~expr]
(cond [(pos? ~g) ~pos-form]
[(zero? ~g) ~zero-form]
[(neg? ~g) ~neg-form]))))
Expand All @@ -415,9 +415,9 @@ expands to:

.. code-block:: hy

(let [[a (gensym)
[b (gensym)
[c (gensym)]]
(let [a (gensym)
b (gensym)
c (gensym)]
...)

so our re-written ``nif`` would look like:
Expand All @@ -426,7 +426,7 @@ so our re-written ``nif`` would look like:

(defmacro nif [expr pos-form zero-form neg-form]
(with-gensyms [g]
`(let [[~g ~expr]]
`(let [~g ~expr]
(cond [(pos? ~g) ~pos-form]
[(zero? ~g) ~zero-form]
[(neg? ~g) ~neg-form]))))
Expand All @@ -440,7 +440,7 @@ Our final version of ``nif``, built with ``defmacro/g!`` becomes:
.. code-block:: hy

(defmacro/g! nif [expr pos-form zero-form neg-form]
`(let [[~g!res ~expr]]
`(let [~g!res ~expr]
(cond [(pos? ~g!res) ~pos-form]
[(zero? ~g!res) ~zero-form]
[(neg? ~g!res) ~neg-form]))))
Expand Down
4 changes: 2 additions & 2 deletions docs/style-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ Layout & Indentation

.. code-block:: clj

(let [[foo (bar)]
[qux (baz)]]
(let [foo (bar)]
qux (baz)]
(foo qux))


Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Python's context managers (``with`` statements) are used like this:

.. code-block:: clj

(with [[f (open "/tmp/data.in")]]
(with [f (open "/tmp/data.in")]
(print (.read f)))

which is equivalent to::
Expand Down
2 changes: 1 addition & 1 deletion eg/twisted/get-page.hy
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(reactor.stop))

(defn get-page [url]
(let [[d (getPage url)]]
(let [d (getPage url)]
(d.addCallback get-page-size)
(d.addErrback log-error)
(d.addCallback finish)))
Expand Down
4 changes: 2 additions & 2 deletions hy/contrib/alias.hy
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

(defmacro defn-alias [names lambda-list &rest body]
"define one function with several names"
(let [[main (first names)]
[aliases (rest names)]]
(let [main (first names)
aliases (rest names)]
(setv ret `(do (defn ~main ~lambda-list ~@body)))
(for* [name aliases]
(.append ret
Expand Down
23 changes: 12 additions & 11 deletions hy/contrib/anaphoric.hy
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


(defmacro ap-if (test-form &rest args)
`(let [[it ~test-form]] (if it ~@args)))
`(let [it ~test-form] (if it ~@args)))


(defmacro ap-each [lst &rest body]
Expand All @@ -37,7 +37,7 @@
(defmacro ap-each-while [lst form &rest body]
"Evalutate the body form for each element in the list while the
predicate form evaluates to True."
`(let [[p (lambda [it] ~form)]]
`(let [p (lambda [it] ~form)]
(for [it ~lst]
(if (p it)
~@body
Expand All @@ -46,16 +46,17 @@

(defmacro ap-map [form lst]
"Yield elements evaluated in the form for each element in the list."
(let [[v (gensym 'v)] [f (gensym 'f)]]
`(let [[~f (lambda [it] ~form)]]
(let [v (gensym 'v)
f (gensym 'f)]
`(let [~f (lambda [it] ~form)]
(for [~v ~lst]
(yield (~f ~v))))))


(defmacro ap-map-when [predfn rep lst]
"Yield elements evaluated for each element in the list when the
predicate function returns True."
`(let [[f (lambda [it] ~rep)]]
`(let [f (lambda [it] ~rep)]
(for [it ~lst]
(if (~predfn it)
(yield (f it))
Expand All @@ -64,7 +65,7 @@

(defmacro ap-filter [form lst]
"Yield elements returned when the predicate form evaluates to True."
`(let [[pred (lambda [it] ~form)]]
`(let [pred (lambda [it] ~form)]
(for [val ~lst]
(if (pred val)
(yield val)))))
Expand All @@ -85,15 +86,15 @@
(defmacro ap-first [predfn lst]
"Yield the first element that passes `predfn`"
(with-gensyms [n]
`(let [[~n None]]
`(let [~n None]
(ap-each ~lst (when ~predfn (setv ~n it) (break)))
~n)))


(defmacro ap-last [predfn lst]
"Yield the last element that passes `predfn`"
(with-gensyms [n]
`(let [[~n None]]
`(let [~n None]
(ap-each ~lst (none? ~n)
(when ~predfn
(setv ~n it)))
Expand All @@ -103,10 +104,10 @@
(defmacro ap-reduce [form lst &optional [initial-value None]]
"Anaphoric form of reduce, `acc' and `it' can be used for a form"
(if (none? initial-value)
`(let [[acc (car ~lst)]]
`(let [acc (car ~lst)]
(ap-each (cdr ~lst) (setv acc ~form))
acc)
`(let [[acc ~initial-value]]
`(let [acc ~initial-value]
(ap-each ~lst (setv acc ~form))
acc)))

Expand All @@ -115,7 +116,7 @@
"Pushes a value through several forms.
(Anaphoric version of -> and ->>)"
(if (empty? forms) var
`(ap-pipe (let [[it ~var]] ~(first forms)) ~@(rest forms))))
`(ap-pipe (let [it ~var] ~(first forms)) ~@(rest forms))))


(defmacro ap-compose [&rest forms]
Expand Down
4 changes: 2 additions & 2 deletions hy/contrib/botsbuildbots.hy
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
(do
(import [requests])

(let [[r (requests.get
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]]
(let [r (requests.get
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]
(repeat r.text)))
(except [e ImportError]
(repeat "Botsbuildbots requires `requests' to function."))))
4 changes: 2 additions & 2 deletions hy/contrib/curry.hy
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


(defn curry [func]
(let [[sig (.getargspec inspect func)]
[count (len sig.args)]]
(let [sig (.getargspec inspect func)
count (len sig.args)]

(fn [&rest args]
(if (< (len args) count)
Expand Down
6 changes: 3 additions & 3 deletions hy/contrib/loop.hy
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@


(defmacro/g! fnr [signature &rest body]
(let [[new-body (recursive-replace 'recur g!recur-fn body)]]
(let [new-body (recursive-replace 'recur g!recur-fn body)]
`(do
(import [hy.contrib.loop [--trampoline--]])
(with-decorator
Expand All @@ -85,7 +85,7 @@
;; If recur is used in a non-tail-call position, None is returned, which
;; causes chaos. Fixing this to detect if recur is in a tail-call position
;; and erroring if not is a giant TODO.
(let [[fnargs (map (fn [x] (first x)) bindings)]
[initargs (map second bindings)]]
(let [fnargs (map (fn [x] (first x)) bindings)
initargs (map second bindings)]
`(do (defnr ~g!recur-fn [~@fnargs] ~@body)
(~g!recur-fn ~@initargs))))
10 changes: 5 additions & 5 deletions hy/contrib/meth.hy
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

(defmacro route-with-methods [name path methods params &rest code]
"Same as route but with an extra methods array to specify HTTP methods"
`(let [[deco (apply app.route [~path]
{"methods" ~methods})]]
(with-decorator deco
(defn ~name ~params
(do ~@code)))))
`(let [deco (apply app.route [~path]
{"methods" ~methods})]
(with-decorator deco
(defn ~name ~params
(do ~@code)))))

;; Some macro examples
(defmacro route [name path params &rest code]
Expand Down
Loading