From 3a66a2d1bed27ee5d8a127dd7e55155b2afdc508 Mon Sep 17 00:00:00 2001 From: Tuukka Turto Date: Mon, 28 Apr 2014 21:09:06 +0300 Subject: [PATCH 1/2] document dict-comp and genexpr --- docs/language/api.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/language/api.rst b/docs/language/api.rst index fe3575e..0bbe021 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -271,6 +271,21 @@ however is called only for every other value in the list. (side-effect2 x))) +dict-comp +--------- + +`dict-comp` is used to create dictionaries. It takes two or three parameters. +The first two parameters are for controlling the return value +(key-value pair), while the third is used to select items from a sequence. The +fourth and optional parameter can be used to filter out some of the items in +the list based on a conditional expression. + +.. code-block:: clj + + => (dict-comp x (* x 2) [x (range 10)] (odd? x)) + {1: 2, 3: 6, 9: 18, 5: 10, 7: 14} + + do / progn ---------- @@ -629,6 +644,24 @@ normally. If the execution is halted with `break`, the `else` does not execute. loop finished +genexpr +------- + +`genexpr` is used to create generator expressions. It takes two or three parameters. +The first parameter is the expression controlling the return value, while +the second is used to select items from a list. The third and optional +parameter can be used to filter out some of the items in the list based on a +conditional expression. `genexpr` is similar to `list-comp`, except that it returns +an iterable that evaluates values one by one instead of evaluating them immediately. + +.. code-block:: clj + + => (def collection (range 10)) + => (def filtered (genexpr x [x collection] (even? x))) + => (list filtered) + [0, 2, 4, 6, 8] + + .. _gensym: gensym From 01fa9d6a3c0502a975c4253f16e09a47248b69de Mon Sep 17 00:00:00 2001 From: Tuukka Turto Date: Mon, 28 Apr 2014 21:35:28 +0300 Subject: [PATCH 2/2] document set-comp --- docs/language/api.rst | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/language/api.rst b/docs/language/api.rst index 0bbe021..e2b7d12 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -274,13 +274,13 @@ however is called only for every other value in the list. dict-comp --------- -`dict-comp` is used to create dictionaries. It takes two or three parameters. +`dict-comp` is used to create dictionaries. It takes three or four parameters. The first two parameters are for controlling the return value (key-value pair), while the third is used to select items from a sequence. The fourth and optional parameter can be used to filter out some of the items in -the list based on a conditional expression. +the sequence based on a conditional expression. -.. code-block:: clj +.. code-block:: hy => (dict-comp x (* x 2) [x (range 10)] (odd? x)) {1: 2, 3: 6, 9: 18, 5: 10, 7: 14} @@ -654,7 +654,7 @@ parameter can be used to filter out some of the items in the list based on a conditional expression. `genexpr` is similar to `list-comp`, except that it returns an iterable that evaluates values one by one instead of evaluating them immediately. -.. code-block:: clj +.. code-block:: hy => (def collection (range 10)) => (def filtered (genexpr x [x collection] (even? x))) @@ -1036,6 +1036,22 @@ element: [1, 2, 3, 4, 5, 6, 7, 8, 9] +set-comp +-------- + +`set-comp` is used to create sets. It takes two or three parameters. +The first parameter is for controlling the return value, while the second is +used to select items from a sequence. The third and optional parameter can be +used to filter out some of the items in the sequence based on a conditional +expression. + +.. code-block:: hy + + => (setv data [1 2 3 4 5 2 3 4 5 3 4 5]) + => (set-comp x [x data] (odd? x)) + {1, 3, 5} + + slice -----