Merge pull request #1609 from pyx/fix-doc-typo
Documentation improvement
This commit is contained in:
commit
60c0f60a32
@ -25,7 +25,7 @@ with the arity overloaded one. Inspired by Clojures take on ``defn``.
|
|||||||
=> (fun 1 2 3)
|
=> (fun 1 2 3)
|
||||||
"a b c"
|
"a b c"
|
||||||
|
|
||||||
=> (defn add [a b]
|
=> (defn add [a b]
|
||||||
... (+ a b))
|
... (+ a b))
|
||||||
=> (add 1 2)
|
=> (add 1 2)
|
||||||
3
|
3
|
||||||
@ -47,13 +47,13 @@ on the code by `Adam Bard`_.
|
|||||||
=> (defmulti area [shape]
|
=> (defmulti area [shape]
|
||||||
... "calculate area of a shape"
|
... "calculate area of a shape"
|
||||||
... (:type shape))
|
... (:type shape))
|
||||||
|
|
||||||
=> (defmethod area "square" [square]
|
=> (defmethod area "square" [square]
|
||||||
... (* (:width square)
|
... (* (:width square)
|
||||||
... (:height square)))
|
... (:height square)))
|
||||||
|
|
||||||
=> (defmethod area "circle" [circle]
|
=> (defmethod area "circle" [circle]
|
||||||
... (* (** (:radius circle) 2)
|
... (* (** (:radius circle) 2)
|
||||||
... 3.14))
|
... 3.14))
|
||||||
|
|
||||||
=> (default-method area [shape]
|
=> (default-method area [shape]
|
||||||
@ -75,7 +75,7 @@ at least key :type. The value that corresponds to this key is returned and
|
|||||||
is used to selected between different implementations.
|
is used to selected between different implementations.
|
||||||
|
|
||||||
``defmethod`` defines a possible implementation for multimethod. It works
|
``defmethod`` defines a possible implementation for multimethod. It works
|
||||||
otherwise in the same way as ``defn``, but has an extra parameters
|
otherwise in the same way as ``defn``, but has an extra parameters
|
||||||
for specifying multimethod and which calls are routed to this specific
|
for specifying multimethod and which calls are routed to this specific
|
||||||
implementation. In the example, shapes with "square" as :type are routed to
|
implementation. In the example, shapes with "square" as :type are routed to
|
||||||
first function and shapes with "circle" as :type are routed to second
|
first function and shapes with "circle" as :type are routed to second
|
||||||
|
@ -16,12 +16,12 @@ Macros
|
|||||||
profile/calls
|
profile/calls
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
``profile/calls`` allows you to create a call graph visualization.
|
``profile/calls`` allows you to create a call graph visualization.
|
||||||
**Note:** You must have `Graphviz <http://www.graphviz.org/Home.php>`_
|
**Note:** You must have `Graphviz <http://www.graphviz.org/Home.php>`_
|
||||||
installed for this to work.
|
installed for this to work.
|
||||||
|
|
||||||
|
|
||||||
Usage: `(profile/calls (body))`
|
Usage: `(profile/calls (body))`
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
@ -50,8 +50,8 @@ Example:
|
|||||||
hey there
|
hey there
|
||||||
<pstats.Stats instance at 0x14ff320>
|
<pstats.Stats instance at 0x14ff320>
|
||||||
2 function calls in 0.000 seconds
|
2 function calls in 0.000 seconds
|
||||||
|
|
||||||
Random listing order was used
|
Random listing order was used
|
||||||
|
|
||||||
ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
|
ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
|
||||||
1 0.000 0.000 0.000 0.000 {print}
|
1 0.000 0.000 0.000 0.000 {print}
|
||||||
|
@ -55,7 +55,7 @@ This results in the sequence ``[0 1 1 2 3 5 8 13 21 34 ...]``.
|
|||||||
seq
|
seq
|
||||||
===
|
===
|
||||||
|
|
||||||
Usage: ``(seq [n] (* n n)``
|
Usage: ``(seq [n] (* n n))``
|
||||||
|
|
||||||
Creates a sequence defined in terms of ``n``.
|
Creates a sequence defined in terms of ``n``.
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ point per form via the name instead of always the first or last argument.
|
|||||||
... :discovered {:year 1907
|
... :discovered {:year 1907
|
||||||
... :name "Sir Joseph Cooke Verco"}}])
|
... :name "Sir Joseph Cooke Verco"}}])
|
||||||
|
|
||||||
;; retrieve name of first entry
|
;; retrieve name of first entry
|
||||||
=> (as-> (first data) it
|
=> (as-> (first data) it
|
||||||
... (:name it))
|
... (:name it))
|
||||||
'hooded cuttlefish'
|
'hooded cuttlefish'
|
||||||
@ -164,7 +164,7 @@ point per form via the name instead of always the first or last argument.
|
|||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
In these examples, the REPL will report a tuple (e.g. `('Sepia prashadi',
|
In these examples, the REPL will report a tuple (e.g. `('Sepia prashadi',
|
||||||
'Sepia prashadi')`) as the result, but only a single value is actually
|
'Sepia prashadi')`) as the result, but only a single value is actually
|
||||||
returned.
|
returned.
|
||||||
|
|
||||||
@ -233,7 +233,7 @@ as the user enters *k*.
|
|||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(while True (if (= "k" (raw-input "? "))
|
(while True (if (= "k" (input "? "))
|
||||||
(break)
|
(break)
|
||||||
(print "Try again")))
|
(print "Try again")))
|
||||||
|
|
||||||
@ -296,7 +296,7 @@ As shown below, only the first matching result block is executed.
|
|||||||
... (cond [(< value 5) (print "value is smaller than 5")]
|
... (cond [(< value 5) (print "value is smaller than 5")]
|
||||||
... [(= value 5) (print "value is equal to 5")]
|
... [(= value 5) (print "value is equal to 5")]
|
||||||
... [(> value 5) (print "value is greater than 5")]
|
... [(> value 5) (print "value is greater than 5")]
|
||||||
... [True (print "value is something that it should not be")]))
|
... [True (print "value is something that it should not be")]))
|
||||||
|
|
||||||
=> (check-value 6)
|
=> (check-value 6)
|
||||||
value is greater than 5
|
value is greater than 5
|
||||||
@ -487,11 +487,11 @@ Parameters may have the following keywords in front of them:
|
|||||||
=> (defn total-value [value &optional [value-added-tax 10]]
|
=> (defn total-value [value &optional [value-added-tax 10]]
|
||||||
... (+ (/ (* value value-added-tax) 100) value))
|
... (+ (/ (* value value-added-tax) 100) value))
|
||||||
|
|
||||||
=> (total-value 100)
|
=> (total-value 100)
|
||||||
110.0
|
110.0
|
||||||
|
|
||||||
=> (total-value 100 1)
|
=> (total-value 100 1)
|
||||||
101.0
|
101.0
|
||||||
|
|
||||||
&kwargs
|
&kwargs
|
||||||
Parameter will contain 0 or more keyword arguments.
|
Parameter will contain 0 or more keyword arguments.
|
||||||
@ -525,7 +525,7 @@ Parameters may have the following keywords in front of them:
|
|||||||
|
|
||||||
=> (defn zig-zag-sum [&rest numbers]
|
=> (defn zig-zag-sum [&rest numbers]
|
||||||
(setv odd-numbers (list-comp x [x numbers] (odd? x))
|
(setv odd-numbers (list-comp x [x numbers] (odd? x))
|
||||||
even-numbers (list-comp x [x numbers] (even? x)))
|
even-numbers (list-comp x [x numbers] (even? x)))
|
||||||
(- (sum odd-numbers) (sum even-numbers)))
|
(- (sum odd-numbers) (sum even-numbers)))
|
||||||
|
|
||||||
=> (zig-zag-sum)
|
=> (zig-zag-sum)
|
||||||
@ -1115,8 +1115,8 @@ that ``import`` can be used.
|
|||||||
;; import sys as systest
|
;; import sys as systest
|
||||||
(import [tests.resources [kwtest function-with-a-dash]]
|
(import [tests.resources [kwtest function-with-a-dash]]
|
||||||
[os.path [exists
|
[os.path [exists
|
||||||
isdir :as dir?
|
isdir :as dir?
|
||||||
isfile :as file?]]
|
isfile :as file?]]
|
||||||
[sys :as systest])
|
[sys :as systest])
|
||||||
|
|
||||||
;; Import all module functions into current namespace
|
;; Import all module functions into current namespace
|
||||||
@ -1398,7 +1398,7 @@ repexpr]])``, but a less error-prone approach is to change the definition of
|
|||||||
(defmacro foo [n]
|
(defmacro foo [n]
|
||||||
`(do
|
`(do
|
||||||
(require mymodule)
|
(require mymodule)
|
||||||
(mymodule.repexpr ~n (raw-input "Gimme some input: "))))
|
(mymodule.repexpr ~n (input "Gimme some input: "))))
|
||||||
|
|
||||||
It's wise to use ``(require mymodule)`` here rather than ``(require [mymodule
|
It's wise to use ``(require mymodule)`` here rather than ``(require [mymodule
|
||||||
[repexpr]])`` to avoid accidentally shadowing a function named ``repexpr`` in
|
[repexpr]])`` to avoid accidentally shadowing a function named ``repexpr`` in
|
||||||
@ -1856,7 +1856,7 @@ will be 4 (``1+1 + 1+1``).
|
|||||||
=> (addition 1 1)
|
=> (addition 1 1)
|
||||||
4
|
4
|
||||||
=> (with-decorator inc2-decorator inc-decorator
|
=> (with-decorator inc2-decorator inc-decorator
|
||||||
... (defn addition [a b] (+ a b)))
|
... (defn addition [a b] (+ a b)))
|
||||||
=> (addition 1 1)
|
=> (addition 1 1)
|
||||||
8
|
8
|
||||||
|
|
||||||
|
@ -652,7 +652,7 @@ calling ``(f val-in-result val-in-latter)``.
|
|||||||
|
|
||||||
.. code-block:: hy
|
.. code-block:: hy
|
||||||
|
|
||||||
=> (merge-with (fn [x y] (+ x y)) {"a" 10 "b" 20} {"a" 1 "c" 30})
|
=> (merge-with + {"a" 10 "b" 20} {"a" 1 "c" 30})
|
||||||
{u'a': 11L, u'c': 30L, u'b': 20L}
|
{u'a': 11L, u'c': 30L, u'b': 20L}
|
||||||
|
|
||||||
|
|
||||||
@ -1201,9 +1201,9 @@ if *from-file* ends before a complete expression can be parsed.
|
|||||||
4
|
4
|
||||||
=> (import io)
|
=> (import io)
|
||||||
=> (setv buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
|
=> (setv buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
|
||||||
=> (eval (read :from_file buffer))
|
=> (eval (read :from-file buffer))
|
||||||
4
|
4
|
||||||
=> (eval (read :from_file buffer))
|
=> (eval (read :from-file buffer))
|
||||||
1
|
1
|
||||||
|
|
||||||
=> (with [f (open "example.hy" "w")]
|
=> (with [f (open "example.hy" "w")]
|
||||||
@ -1399,8 +1399,8 @@ are available. Some of their names have been changed:
|
|||||||
- ``groupby`` has been changed to ``group-by``
|
- ``groupby`` has been changed to ``group-by``
|
||||||
|
|
||||||
- ``takewhile`` has been changed to ``take-while``
|
- ``takewhile`` has been changed to ``take-while``
|
||||||
|
|
||||||
- ``dropwhile`` has been changed to ``drop-while``
|
- ``dropwhile`` has been changed to ``drop-while``
|
||||||
|
|
||||||
- ``filterfalse`` has been changed to ``remove``
|
- ``filterfalse`` has been changed to ``remove``
|
||||||
|
|
||||||
|
@ -323,13 +323,13 @@ Will turn into::
|
|||||||
else:
|
else:
|
||||||
_temp_name_here = False
|
_temp_name_here = False
|
||||||
|
|
||||||
print _temp_name_here
|
print(_temp_name_here)
|
||||||
|
|
||||||
|
|
||||||
OK, that was a bit of a lie, since we actually turn that statement
|
OK, that was a bit of a lie, since we actually turn that statement
|
||||||
into::
|
into::
|
||||||
|
|
||||||
print True if True else False
|
print(True if True else False)
|
||||||
|
|
||||||
By forcing things into an ``ast.expr`` if we can, but the general idea holds.
|
By forcing things into an ``ast.expr`` if we can, but the general idea holds.
|
||||||
|
|
||||||
@ -412,10 +412,11 @@ so our re-written ``nif`` would look like:
|
|||||||
|
|
||||||
(defmacro nif [expr pos-form zero-form neg-form]
|
(defmacro nif [expr pos-form zero-form neg-form]
|
||||||
(with-gensyms [g]
|
(with-gensyms [g]
|
||||||
`(setv [~g ~expr])
|
`(do
|
||||||
`(cond [(pos? ~g) ~pos-form]
|
(setv ~g ~expr)
|
||||||
[(zero? ~g) ~zero-form]
|
(cond [(pos? ~g) ~pos-form]
|
||||||
[(neg? ~g) ~neg-form])))
|
[(zero? ~g) ~zero-form]
|
||||||
|
[(neg? ~g) ~neg-form]))))
|
||||||
|
|
||||||
Finally, though we can make a new macro that does all this for us. :ref:`defmacro/g!`
|
Finally, though we can make a new macro that does all this for us. :ref:`defmacro/g!`
|
||||||
will take all symbols that begin with ``g!`` and automatically call ``gensym`` with the
|
will take all symbols that begin with ``g!`` and automatically call ``gensym`` with the
|
||||||
|
@ -14,7 +14,7 @@ In addition to regular numbers, standard notation from Python 3 for non-base 10
|
|||||||
integers is used. ``0x`` for Hex, ``0o`` for Octal, ``0b`` for Binary.
|
integers is used. ``0x`` for Hex, ``0o`` for Octal, ``0b`` for Binary.
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(print 0x80 0b11101 0o102 30)
|
(print 0x80 0b11101 0o102 30)
|
||||||
|
|
||||||
Underscores and commas can appear anywhere in a numeric literal except the very
|
Underscores and commas can appear anywhere in a numeric literal except the very
|
||||||
|
@ -30,7 +30,7 @@ Quickstart
|
|||||||
|
|
||||||
=> (+ "Hyllo " "World" "!")
|
=> (+ "Hyllo " "World" "!")
|
||||||
'Hyllo ' + 'World' + '!'
|
'Hyllo ' + 'World' + '!'
|
||||||
|
|
||||||
'Hyllo World!'
|
'Hyllo World!'
|
||||||
|
|
||||||
*OMG! That's amazing! I want to write a Hy program.*
|
*OMG! That's amazing! I want to write a Hy program.*
|
||||||
|
@ -120,7 +120,7 @@ This is the basic premise of Lisp. Lisp stands for "list
|
|||||||
processing"; this means that the structure of the program is
|
processing"; this means that the structure of the program is
|
||||||
actually lists of lists. (If you're familiar with Python lists,
|
actually lists of lists. (If you're familiar with Python lists,
|
||||||
imagine the entire same structure as above but with square brackets
|
imagine the entire same structure as above but with square brackets
|
||||||
instead, any you'll be able to see the structure above as both a
|
instead, and you'll be able to see the structure above as both a
|
||||||
program and a data structure.) This is easier to understand with more
|
program and a data structure.) This is easier to understand with more
|
||||||
examples, so let's write a simple Python program, test it, and then
|
examples, so let's write a simple Python program, test it, and then
|
||||||
show the equivalent Hy program::
|
show the equivalent Hy program::
|
||||||
@ -472,7 +472,7 @@ like::
|
|||||||
Return our copy of x
|
Return our copy of x
|
||||||
"""
|
"""
|
||||||
return self.x
|
return self.x
|
||||||
|
|
||||||
And we might use it like::
|
And we might use it like::
|
||||||
|
|
||||||
bar = FooBar(1)
|
bar = FooBar(1)
|
||||||
@ -492,20 +492,20 @@ In Hy:
|
|||||||
(defn get-x [self]
|
(defn get-x [self]
|
||||||
"Return our copy of x"
|
"Return our copy of x"
|
||||||
self.x))
|
self.x))
|
||||||
|
|
||||||
And we can use it like:
|
And we can use it like:
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(setv bar (FooBar 1))
|
(setv bar (FooBar 1))
|
||||||
(print (bar.get-x))
|
(print (bar.get-x))
|
||||||
|
|
||||||
Or using the leading dot syntax!
|
Or using the leading dot syntax!
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(print (.get-x (FooBar 1)))
|
(print (.get-x (FooBar 1)))
|
||||||
|
|
||||||
|
|
||||||
You can also do class-level attributes. In Python::
|
You can also do class-level attributes. In Python::
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user