Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize commands reduce and accumulate. #125

Closed
okamsn opened this issue Aug 7, 2022 · 3 comments
Closed

Generalize commands reduce and accumulate. #125

okamsn opened this issue Aug 7, 2022 · 3 comments
Labels
enhancement New feature or request user input needed Opinion-based changes

Comments

@okamsn
Copy link
Owner

okamsn commented Aug 7, 2022

These commands work similarly, except that their argument orders are switched.
accumulate calls the function with the accumulation variable as the second
argument, while reduce uses the accumulation variable as the first argument.

It feels like there should be a command that is a more generic form of both.
Something like (some-command VAR FUNC [EXPRS] &keys position init), which
works like funcall and where position is an index in the funcall
arguments.

  • (cmd VAR FUNC) would be equivalent to (set VAR (FUNC VAR))
  • (cmd VAR FUNC EXPR) would be equivalent to (set VAR (FUNC VAR EXPR)) and
    (reduce VAR FUNC EXPR)
  • (cmd VAR FUNC EXPR :position 1) would be equivalent to
    (set VAR (FUNC EXPR VAR)) and (accumulate VAR FUNC EXPR)
  • (cmd VAR FUNC EXPR1 EXPR2 EXPR3 :position 2) would be
    (set VAR (FUNC EXPR1 EXPR2 VAR EXPR3)), which no accumulation command
    currently does.

The commands accumulate and reduce would be kept to meet user expectations
from Iterate, but would just be wrappers around this new command, similar to
nums-up and nums-down being wrappers for nums.

Open to naming suggestions. funcall would be an accurate name, but might be
confusing in loopy-iter.

@okamsn okamsn changed the title Improve commands reduce and accumulate. Generalize commands reduce and accumulate. Aug 7, 2022
@okamsn okamsn added user input needed Opinion-based changes enhancement New feature or request labels Aug 7, 2022
@okamsn
Copy link
Owner Author

okamsn commented Aug 19, 2022

The initial idea of (cmd VAR FUNC [EXPRS]) won't work, since (cmd VAR FUNC EXPR1 EXPR2) can't be distinguished from (cmd FUNC EXPR1 EXPR2 EXPR3) when
using an implicit variable.

There needs to be a way to state the arguments explicitly using a keyword.
Since we do that, maybe we don't need a position argument, just always a
complete list of arguments. For example, (cmd VAR FUNC &key args init),
in which the below would be equivalent.

;; => 16
(loopy (list i '(1 2 3))
       (reduce i #'+ :init 10))

(loopy (list i '(1 2 3))
       (cmd #'+ :args (loopy-result i) :init 10))
;; => 16
(loopy (list i '(1 2 3))
       (reduce my-sum i #'+ :init 10)
       (finally-return my-sum))

(loopy (list i '(1 2 3))
       (cmd my-sum #'+ :args (my-sum i) :init 10)
       (finally-return my-sum))

For implicit returns, one would need to know that loopy-result is the variable
name, which is documented. This wouldn't work for split variables, but we are
deprecating them anyway. There is also no optimizations that could be made with
these commands, since they just call functions, so explicitly accessing the
value during the loop should be fine.

@okamsn
Copy link
Owner Author

okamsn commented Aug 20, 2022

On further thought, if we need to provide an argument list, we could really just
provide the entire expression. All explicit accumulation commands can be
described using set, so really the most generic accumulation command would be
a simple set-accum, as in (set-accum VAR EXPR &key init). Then, the
previous examples are as follows.

;; => 16
(loopy (list i '(1 2 3))
       (reduce i #'+ :init 10))

(loopy (list i '(1 2 3))
       (set-accum (+ loopy-result i) :init 10))
;; => 16
(loopy (list i '(1 2 3))
       (reduce my-sum i #'+ :init 10)
       (finally-return my-sum))

(loopy (list i '(1 2 3))
       (set-accum my-sum (+ my-sum i) :init 10)
       (finally-return my-sum))

okamsn added a commit that referenced this issue Aug 20, 2022
This is an accumulating version of `set`, which generalizes the ideas of
`reduce` and `accumulate`.  See also issue #125 and PR
#129.
okamsn added a commit that referenced this issue Aug 20, 2022
This is an accumulating version of `set`, which generalizes the ideas of
`reduce` and `accumulate`.  See also issue #125 and PR
#129.
@okamsn
Copy link
Owner Author

okamsn commented Aug 20, 2022

The command set-accum was added in PR #129.

@okamsn okamsn closed this as completed Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request user input needed Opinion-based changes
Projects
None yet
Development

No branches or pull requests

1 participant