argmax



coding armax functionality each time from scratch have bored me to hell, so
i've decided to make a macro for this. i.e. simple case:

CL-USER> (with-find-optimal (check >)
(loop for i from 1 to 100
do (check i)))
100
100

with key being a function:

CL-USER> (with-find-optimal (check > -)
(loop for i from 1 to 100
do (check (cons i i))))
1
-1


with key being custom code:

CL-USER> (with-find-optimal (check >
(lambda (cons) (* (car cons) (cdr cons))))
(loop for i from 1 to 100
do (check (cons i i))))
(100 . 100)
10000

i suspect i'm not the only one doing such macro, but i'm not able to find
any of them, and don't even know how it could be called..
something tells me that fancy LOOP substitutions like ITERATE have this
built-in, but it would be overkill to use ITERATE just for this thing.
also, with my macro CHECK is lexical function, so it can be used in fairly
complex cases -- passed to other functions etc.

here's my code. i don't really like it (nor i like interface), but it seems
to work..

(defmacro with-find-optimal ((check compare &optional key) &body body)
(unless key (setf key 'identity))
(with-gensyms (option optimal optimal-value initialized option-value)
`(let (,optimal ,optimal-value ,initialized)
(flet ((,check (,option)
(let ((,option-value (,key ,option)))
(if ,initialized
(when (,compare ,option-value ,optimal-value)
(setf ,optimal ,option
,optimal-value ,option-value))
(setf ,optimal ,option
,optimal-value ,option-value
,initialized t)))))
,@body
(values ,optimal ,optimal-value)))))


.



Relevant Pages

  • Re: PC Word Macro Not Working in Mac Word
    ... I wrote my original in Mac Word, ... .Execute Replace:=wdReplaceAll ... Do ' A post-test loop! ... I did a test macro with basically just the code you ...
    (microsoft.public.mac.office.word)
  • RE: Loop Training: Password Finder
    ... 'The cycles on your g loop would be phenominal in your case. ... Dim Solution As String ... I have a macro below, ... > Exit Sub ...
    (microsoft.public.excel.programming)
  • RE: Word 2003 crashes with 2003 vba macro
    ... I found out that the loop in this case never ... One of the ways in which a macro can cause Word to hang is by having a loop, ... As the cycles of the loop proceed, pos1 increases until the cursor is ... Dim i As Integer, pos As Single, pos1 As Integer ...
    (microsoft.public.word.vba.general)
  • Re: User initiated break/actions during macro execution
    ... > an infinite loop until the user presses a stop button. ... > speed of the macro as it runs (the idea being, ... > to allow users initiated actions DURING macro execution. ...
    (microsoft.public.excel.programming)
  • Re: nested grinding..
    ... Next" in the following macro: ... Dim data As Range, stab As Table, ttab As Table ... Set scat = stab.Cell.Range ... For a Do While ..Loop, it would be structured as follows (this is not an ...
    (microsoft.public.word.vba.beginners)