From a878a7f7d5832318a6a36b0ebc04bd0a63881643 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Tue, 2 Apr 2013 19:12:33 -0500 Subject: [PATCH] List comprehensions example --- docs/language/index.rst | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/language/index.rst b/docs/language/index.rst index c1c24e7..e0e085a 100644 --- a/docs/language/index.rst +++ b/docs/language/index.rst @@ -303,8 +303,6 @@ The equivalent in hy would be: (for (i (range 10)) (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 example: @@ -325,6 +323,24 @@ Comments start with semicolons: ; (print "but this will not") (+ 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! --------