Fix grammar
This commit is contained in:
parent
4219faf532
commit
538d36a7c6
@ -4,24 +4,24 @@ Lazy sequences
|
||||
|
||||
.. versionadded:: 0.12.0
|
||||
|
||||
Sequences module contains few macros for declaring sequences that are evaluated
|
||||
only as much as the client code requests elements. Compared to generators they
|
||||
allow accessing same element multiple times. Since they cache calculated
|
||||
values, they aren't suited for infinite sequences. However, the implementation
|
||||
allows recursive definition of sequences, without resulting recursive
|
||||
computation.
|
||||
The Sequences module contains few macros for declaring sequences that are
|
||||
evaluated only as much as the client code requests elements. Compared to
|
||||
generators, they allow accessing same the element multiple times. Since they
|
||||
cache calculated values, they aren't suited for infinite sequences. However,
|
||||
the implementation allows for recursive definition of sequences, without
|
||||
resulting recursive computation.
|
||||
|
||||
To use these macros you need to require them and import other types like:
|
||||
To use these macros, you need to require them and import other types like:
|
||||
|
||||
.. code-block:: hy
|
||||
|
||||
(require [hy.contrib.sequences [defseq seq]])
|
||||
(import [hy.contrib.sequences [Sequence end-sequence]])
|
||||
|
||||
Simplest sequence can be defined as: ``(seq [n] n)``. This defines a sequence
|
||||
that starts as: ``[0 1 2 3 ...]`` and continues forever. In order to define
|
||||
a finite sequence, ``end-sequence`` needs to be called to signal end of
|
||||
the sequence:
|
||||
The simplest sequence can be defined as ``(seq [n] n)``. This defines a
|
||||
sequence that starts as ``[0 1 2 3 ...]`` and continues forever. In order to
|
||||
define a finite sequence, ``end-sequence`` needs to be called to signal the end
|
||||
of the sequence:
|
||||
|
||||
.. code-block:: hy
|
||||
|
||||
@ -30,12 +30,13 @@ the sequence:
|
||||
(cond [(< n 5) n]
|
||||
[true (end-sequence)]))
|
||||
|
||||
This creates following sequence: ``[0 1 2 3 4]``. For such a sequence ``len``
|
||||
returns amount of items in sequence and negative indexing is suported. Because
|
||||
both of thse require evaluating whole sequence, calling such a function would
|
||||
take forever (or at least until available memory has been exhausted).
|
||||
This creates following sequence: ``[0 1 2 3 4]``. For such a sequence, ``len``
|
||||
returns the amount of items in the sequence and negative indexing is supported.
|
||||
Because both of thse require evaluating the whole sequence, calling such a
|
||||
function would take forever (or at least until available memory has been
|
||||
exhausted).
|
||||
|
||||
Sequence can be defined recursively. Canonical example of fibonacci numbers
|
||||
Sequences can be defined recursively. The canonical example of fibonacci numbers
|
||||
is defined as:
|
||||
|
||||
.. code-block:: hy
|
||||
@ -47,7 +48,7 @@ is defined as:
|
||||
[true (+ (get fibonacci (- n 1))
|
||||
(get fibonacci (- n 2)))]))
|
||||
|
||||
This results sequence of ``[0 1 1 2 3 5 8 13 21 34 ...]``.
|
||||
This results the sequence of ``[0 1 1 2 3 5 8 13 21 34 ...]``.
|
||||
|
||||
.. _seq:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user