Skip to content

Commit

Permalink
Improve efficiency and various clean-ups.
Browse files Browse the repository at this point in the history
- Add `loopy--instr-let-var*`, `loopy--instr-let-const*`,
  `loopy--instr-let-var`, `loopy--instr-let-const`.  These macros are similar
  to `macroexp-let2*` in that the `const` versions try to pass constant values
  directly without creating a variable in the Loopy expansion.
  - Update `array`, `array-ref`, `cons`, `list`,  `list-ref`,
    `seq`, `seq-ref`, `seq-index`, `adjoin`, `union`, `nunion`

  - Don't update `numbers` until after we remove the non-keyword args.

- Replace some uses of `seq-let` with `cl-destructuring-bind`.

- Use `loopy--bind-main-body` in some places.

- Add some TODOs.

- Fix `list-ref` tests to not modify literal constant list.
- Fix `seq-ref` tests to not modify literal constant list.

- Make `loopy--find-start-by-end-dir-vals` return the test function.

- Add `:test` to `array`, `array-ref`, `sequence`, `sequence-index`, and
  `sequence-ref`.
  - When going up on lists, use `nthcdr` instead of `elt`.

- Add `sequence`, `sequence-ref`, `sequence-index`, `array`, and `array-ref`
  tests for `:downfrom` and `:upfrom` as needed.
  • Loading branch information
okamsn committed Nov 8, 2023
1 parent 3819e0f commit d16a42e
Show file tree
Hide file tree
Showing 4 changed files with 915 additions and 449 deletions.
41 changes: 30 additions & 11 deletions doc/loopy-doc.org
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,14 @@ error to use the same iteration variable for multiple iteration commands.
(finally-return t))
#+end_src

Unlike ~cl-loop~ and like Common Lisp's ~iterate~, arguments of the iteration
commands are evaluated only once. For example, while iterating through numbers,
you can't suddenly change the direction of the iteration in the middle of the
loop, nor can you change the final numeric value. Similarly, the function used
to iterate through the list in the =list= command is the same for the entire
loop. This restriction allows for producing more efficient code.


*** Generic Iteration
:PROPERTIES:
:CUSTOM_ID: generic-iteration
Expand Down Expand Up @@ -1364,7 +1372,8 @@ variants =numbers-up= and =numbers-down=.
=from= and =to=; it cannot be used with keywords that already describe a
direction and ending condition. To match the behavior of ~cl-loop~, the
default testing function is ~#'<=~. When =test= is given, =by= can be
negative.
negative. As there is no default end value when =test= is given, =to= must
also be given.

#+begin_src emacs-lisp
;; => (10 9.5 9.0 8.5 8.0 7.5 7.0 6.5 6.0 5.5)
Expand Down Expand Up @@ -1714,9 +1723,19 @@ source sequences.
This command also has the aliases =seqing= and =sequencing=.

=KEYS= is one or several of =from=, =upfrom=, =downfrom=, =to=, =upto=,
=downto=, =above=, =below=, =by=, and =index=. =index= names the variable
used to store the index being accessed. For others, see the =numbers=
command.
=downto=, =above=, =below=, =by=, =test=, and =index=. =index= names the
variable used to store the index being accessed. For the others, see the
=numbers= command.

#+ATTR_TEXINFO: :tag Warning
#+begin_quote
Array elements can be accessed in constant time, but not list elements. For
lists, the =sequence= command is fastest when moving forwards through the
list. In that case, the command does not have to search from the beginning of
the list each time to find the next element. The =sequence= command can be
noticeably slower for lists when working backwards or when the =test=
parameter (for which direction cannot be assumed) is provided.
#+end_quote

If multiple sequences are given, then these keyword arguments apply to the
resulting sequence of distributed elements.
Expand Down Expand Up @@ -1794,10 +1813,10 @@ iterate.
aliases =seqf=, =arrayf=, =listf=, and =stringf= of the =seq-ref= command.

=KEYS= is one or several of =from=, =upfrom=, =downfrom=, =to=, =upto=,
=downto=, =above=, =below=, and =by=. For their meaning, see the =numbers=
command. This command is very similar to =numbers=, except that it can
automatically end the loop when the final element is reached. With
=numbers=, one would first need to explicitly calculate the length of the
=downto=, =above=, =below=, =by=, and =test=. For their meaning, see the
=numbers= command. This command is very similar to =numbers=, except that it
can automatically end the loop when the index of the final element is reached.
With =numbers=, one would first need to explicitly calculate the length of the
sequence.

Similar to =numbers=, for efficiency, =VAR= is initialized to the starting
Expand Down Expand Up @@ -2028,9 +2047,9 @@ the accessed index during the loop.
~setf~-able place.

=KEYS= is one or several of =from=, =upfrom=, =downfrom=, =to=, =upto=,
=downto=, =above=, =below=, =by=, and =index=. =index= names the variable
used to store the index being accessed. For others, see the =numbers=
command.
=downto=, =above=, =below=, =by=, =test=, and =index=. =index= names the
variable used to store the index being accessed. For others, see the
=numbers= command.

#+BEGIN_SRC emacs-lisp
;; => (7 7 7 7)
Expand Down
Loading

0 comments on commit d16a42e

Please sign in to comment.