Skip to content

proper reprs for Hy models #1360

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

Merged
merged 7 commits into from
Sep 18, 2017
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
190 changes: 146 additions & 44 deletions docs/contrib/walk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ Example:

.. code-block:: hy

=> (import [hy.contrib.walk [walk]])
=> (setv a '(a b c d e f))
=> (walk ord identity a)
(97 98 99 100 101 102)
=> (walk ord first a)
97
=> (import [hy.contrib.walk [walk]])
=> (setv a '(a b c d e f))
=> (walk ord identity a)
HyExpression([
97,
98,
99,
100,
101,
102])
=> (walk ord first a)
97

postwalk
---------
Expand All @@ -41,25 +47,73 @@ 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]]]))
=> (defn walking [x]
(print "Walking:" x)
x )
=> (postwalk walking trail)
Walking: 1
Walking: 2
Walking: 3
Walking: (1 2 3)
Walking: 4
Walking: 5
Walking: 6
Walking: 7
Walking: (7)
Walking: (5 6 [7])
Walking: (4 [5 6 [7]])
Walking: ([1 2 3] [4 [5 6 [7]]])
([1 2 3] [4 [5 6 [7]]])
=> (import [hy.contrib.walk [postwalk]])
=> (def trail '([1 2 3] [4 [5 6 [7]]]))
=> (defn walking [x]
... (print "Walking:" x :sep "\n")
... x)
=> (postwalk walking trail)
Walking:
1
Walking:
2
Walking:
3
Walking:
HyExpression([
HyInteger(1),
HyInteger(2),
HyInteger(3)])
Walking:
4
Walking:
5
Walking:
6
Walking:
7
Walking:
HyExpression([
HyInteger(7)])
Walking:
HyExpression([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])
Walking:
HyExpression([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])
Walking:
HyExpression([
HyList([
HyInteger(1),
HyInteger(2),
HyInteger(3)]),
HyList([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])])
HyExpression([
HyList([
HyInteger(1),
HyInteger(2),
HyInteger(3)]),
HyList([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])])

prewalk
--------
Expand All @@ -73,22 +127,70 @@ 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]]]))
=> (defn walking [x]
(print "Walking:" x)
x )
=> (prewalk walking trail)
Walking: ([1 2 3] [4 [5 6 [7]]])
Walking: [1 2 3]
Walking: 1
Walking: 2
Walking: 3
Walking: [4 [5 6 [7]]]
Walking: 4
Walking: [5 6 [7]]
Walking: 5
Walking: 6
Walking: [7]
Walking: 7
([1 2 3] [4 [5 6 [7]]])
=> (import [hy.contrib.walk [prewalk]])
=> (def trail '([1 2 3] [4 [5 6 [7]]]))
=> (defn walking [x]
... (print "Walking:" x :sep "\n")
... x)
=> (prewalk walking trail)
Walking:
HyExpression([
HyList([
HyInteger(1),
HyInteger(2),
HyInteger(3)]),
HyList([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])])
Walking:
HyList([
HyInteger(1),
HyInteger(2),
HyInteger(3)])
Walking:
1
Walking:
2
Walking:
3
Walking:
HyList([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])
Walking:
4
Walking:
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])
Walking:
5
Walking:
6
Walking:
HyList([
HyInteger(7)])
Walking:
7
HyExpression([
HyList([
HyInteger(1),
HyInteger(2),
HyInteger(3)]),
HyList([
HyInteger(4),
HyList([
HyInteger(5),
HyInteger(6),
HyList([
HyInteger(7)])])])])
56 changes: 36 additions & 20 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ doto
.. code-block:: clj

=> (doto [] (.append 1) (.append 2) .reverse)
[2 1]
[2, 1]

.. code-block:: clj

Expand All @@ -878,7 +878,7 @@ doto
=> (.append collection 2)
=> (.reverse collection)
=> collection
[2 1]
[2, 1]


eval-and-compile
Expand Down Expand Up @@ -1362,9 +1362,10 @@ alternatively be written using the apostrophe (``'``) symbol.
.. code-block:: clj

=> (setv x '(print "Hello World"))
; variable x is set to expression & not evaluated
=> x
(u'print' u'Hello World')
=> x ; varible x is set to unevaluated expression
HyExpression([
HySymbol('print'),
HyString('Hello World')])
=> (eval x)
Hello World

Expand Down Expand Up @@ -1673,12 +1674,17 @@ is aliased to the tilde (``~``) symbol.

.. code-block:: clj

(def name "Cuddles")
(quasiquote (= name (unquote name)))
;=> (u'=' u'name' u'Cuddles')

`(= name ~name)
;=> (u'=' u'name' u'Cuddles')
=> (setv nickname "Cuddles")
=> (quasiquote (= nickname (unquote nickname)))
HyExpression([
HySymbol('='),
HySymbol('nickname'),
'Cuddles'])
=> `(= nickname ~nickname)
HyExpression([
HySymbol('='),
HySymbol('nickname'),
'Cuddles'])


unquote-splice
Expand All @@ -1694,15 +1700,25 @@ into the form. ``unquote-splice`` is aliased to the ``~@`` syntax.

.. code-block:: clj

(def nums [1 2 3 4])
(quasiquote (+ (unquote-splice nums)))
;=> ('+' 1 2 3 4)

`(+ ~@nums)
;=> ('+' 1 2 3 4)

`[1 2 ~@(if (< (nth nums 0) 0) nums)]
;=> ('+' 1 2)
=> (setv nums [1 2 3 4])
=> (quasiquote (+ (unquote-splice nums)))
HyExpression([
HySymbol('+'),
1,
2,
3,
4])
=> `(+ ~@nums)
HyExpression([
HySymbol('+'),
1,
2,
3,
4])
=> `[1 2 ~@(if (neg? (first nums)) nums)]
HyList([
HyInteger(1),
HyInteger(2)])

Here, the last example evaluates to ``('+' 1 2)``, since the condition
``(< (nth nums 0) 0)`` is ``False``, which makes this ``if`` expression
Expand Down
Loading