Update tutorial.
- Fix a couple of typos - Use open() built-in instead of file() - Update (for) and (with) examples
This commit is contained in:
parent
76b9e7c5cd
commit
b8ef4ccc3c
@ -276,7 +276,7 @@ You might notice above that if you have code like:
|
|||||||
(body-if-true)
|
(body-if-true)
|
||||||
(body-if-false))
|
(body-if-false))
|
||||||
|
|
||||||
But wait! What if you want to execute more than one statment in the
|
But wait! What if you want to execute more than one statement in the
|
||||||
body of one of these?
|
body of one of these?
|
||||||
|
|
||||||
You can do the following:
|
You can do the following:
|
||||||
@ -289,7 +289,7 @@ You can do the following:
|
|||||||
(print "and why not, let's keep talking about how true it is!))
|
(print "and why not, let's keep talking about how true it is!))
|
||||||
(print "this one's still simply just false"))
|
(print "this one's still simply just false"))
|
||||||
|
|
||||||
You can see that we used "do" to wrap multiple statments. If you're
|
You can see that we used "do" to wrap multiple statements. If you're
|
||||||
familiar with other lisps, this is the equivalent of "progn"
|
familiar with other lisps, this is the equivalent of "progn"
|
||||||
elsewhere.
|
elsewhere.
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ The equivalent in hy would be:
|
|||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(for (i (range 10))
|
(for [i (range 10)]
|
||||||
(print (+ "'i' is now at " (str i))))
|
(print (+ "'i' is now at " (str i))))
|
||||||
|
|
||||||
|
|
||||||
@ -330,12 +330,12 @@ Python's context managers ('with' statements) are used like this:
|
|||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(with [f (file "/tmp/data.in")]
|
(with [[f (open "/tmp/data.in")]]
|
||||||
(print (.read f)))
|
(print (.read f)))
|
||||||
|
|
||||||
which is equivalent to::
|
which is equivalent to::
|
||||||
|
|
||||||
with file("/tmp/data.in") as f:
|
with open("/tmp/data.in") as f:
|
||||||
print f.read()
|
print f.read()
|
||||||
|
|
||||||
And yes, we do have lisp comprehensions! In Python you might do::
|
And yes, we do have lisp comprehensions! In Python you might do::
|
||||||
@ -435,7 +435,7 @@ The Hy equivalent:
|
|||||||
Finally, of course we need classes! In python we might have a class
|
Finally, of course we need classes! In python we might have a class
|
||||||
like::
|
like::
|
||||||
|
|
||||||
class FooBar (object):
|
class FooBar(object):
|
||||||
def __init__(self, x):
|
def __init__(self, x):
|
||||||
self.x = x
|
self.x = x
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user