Documenting: tuples and argument formatting

This commit is contained in:
Christopher Allan Webber 2013-05-09 15:35:47 -05:00
parent 60c1a1ba06
commit b6d730c044

View File

@ -188,6 +188,8 @@ hy. Let's experiment with this in the hy interpreter::
... "cat" "meow"}
...
{'dog': 'bark', 'cat': 'meow'}
=> (, 1 2 3)
(1, 2, 3)
(You may notice that at present, the common lisp method of quoting
things like so:
@ -375,6 +377,52 @@ In hy, you could do these like:
; (8, 'A'), (8, 'B'), (8, 'C'), (8, 'D'), (8, 'E'), (8, 'F'), (8, 'G'), (8, 'H')]
Python has support for various fancy argument and keyword arguments.
In python we might see::
>>> def optional_arg(pos1, pos2, keyword1=None, keyword2=42):
... return [pos1, pos2, keyword1, keyword2]
...
>>> optional_arg(1, 2)
[1, 2, None, 42]
>>> optional_arg(1, 2, 3, 4)
[1, 2, 3, 4]
>>> optional_arg(keyword1=1, pos2=2, pos1=3, keyword2=4)
[3, 2, 1, 4]
The same thing in Hy:
.. code-block:: clj
=> (defn optional_arg [pos1 pos2 &optional keyword1 [keyword2 88]]
... [pos1 pos2 keyword1 keyword2])
=> (optional_arg 1 2)
[1 2 None 42]
=> (optional_arg 1 2 3 4)
[1 2 3 4]
=> (kwapply (optional_arg)
... {"keyword1" 1
... "pos2" 2
... "pos1" 3
... "keyword2" 4})
...
[3, 2, 1, 4]
See how we use kwapply to handle the fancy pssing? :)
Hy also supports **args and **kwargs. In Python::
def some_func(foo, bar, *args, **kwargs):
import pprint
pprint.pprint((foo, bar, args, kwargs))
The Hy equivalent:
(defn some_func [foo bar &rest args &kwargs kwargs]
(import pprint)
(pprint.pprint (, foo bar args kwargs)))
Protips!
========