-
Notifications
You must be signed in to change notification settings - Fork 3
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
Comments
reduce
and accumulate
.reduce
and accumulate
.
The initial idea of There needs to be a way to state the arguments explicitly using a keyword. ;; => 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 |
On further thought, if we need to provide an argument list, we could really just ;; => 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)) |
The command |
These commands work similarly, except that their argument orders are switched.
accumulate
calls the function with the accumulation variable as the secondargument, 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)
, whichworks like
funcall
and whereposition
is an index in thefuncall
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 commandcurrently does.
The commands
accumulate
andreduce
would be kept to meet user expectationsfrom Iterate, but would just be wrappers around this new command, similar to
nums-up
andnums-down
being wrappers fornums
.Open to naming suggestions.
funcall
would be an accurate name, but might beconfusing in
loopy-iter
.The text was updated successfully, but these errors were encountered: