List comprehensions example
This commit is contained in:
parent
65b162cda2
commit
a878a7f7d5
@ -303,8 +303,6 @@ The equivalent in hy would be:
|
|||||||
(for (i (range 10))
|
(for (i (range 10))
|
||||||
(print (+ "'i' is now at " (str i))))
|
(print (+ "'i' is now at " (str i))))
|
||||||
|
|
||||||
TODO: explain the extra power of hy's for, the list comprehensions
|
|
||||||
aspect ;)
|
|
||||||
|
|
||||||
You can also import and make use of various python libraries. For
|
You can also import and make use of various python libraries. For
|
||||||
example:
|
example:
|
||||||
@ -325,6 +323,24 @@ Comments start with semicolons:
|
|||||||
; (print "but this will not")
|
; (print "but this will not")
|
||||||
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
||||||
|
|
||||||
|
And yes, we do have lisp comprehensions! In Python you might do::
|
||||||
|
|
||||||
|
odds_squared = [
|
||||||
|
pow(num, 2)
|
||||||
|
for num in range(100)
|
||||||
|
if num % 2 == 1]
|
||||||
|
|
||||||
|
In hy, you could do these like:
|
||||||
|
|
||||||
|
.. code-block:: clj
|
||||||
|
|
||||||
|
; and a little more complex
|
||||||
|
(setv odds-squared
|
||||||
|
(list-comp
|
||||||
|
(pow num 2)
|
||||||
|
(num (range 100))
|
||||||
|
(= (% num 2) 1))
|
||||||
|
|
||||||
|
|
||||||
Protips!
|
Protips!
|
||||||
--------
|
--------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user