From b6d730c0447c116085cdad5de7e51067167c451d Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 9 May 2013 15:35:47 -0500 Subject: [PATCH] Documenting: tuples and argument formatting --- docs/tutorial.rst | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 019564b..3e83834 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -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! ========