ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* How to let a macro check the previous value of #1 the last time the same macro was called?
       [not found] <1050338993.1893942.1641737797345.ref@mail.yahoo.com>
@ 2022-01-09 14:16 ` Joel via ntg-context
  2022-01-09 15:03   ` Wolfgang Schuster via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Joel via ntg-context @ 2022-01-09 14:16 UTC (permalink / raw)
  To: ntg-context; +Cc: Joel


[-- Attachment #1.1: Type: text/plain, Size: 798 bytes --]

Is there a way for a macro to check the previous value of #1, the last time that same macro was called?

Here is a minimum working example, pretending that `\previousvalue` is equal to #1 from the last time the same macro was called:

    \define[1]\mymacro{
        \if\previousvalue=#1
        same as last time
    \else
        it is different from last time
    \fi
    }

\starttext

    \mymacro{cat}
    \mymacro{cat}
    \mymacro{mouse}
    \mymacro{mouse}
    \mymacro{cat}

\stoptext

This would print:

    it is different from last time <--it was never called previously
    same as last time
    it is different from last time
    same as last time
    it is different from last time

--Joel

[-- Attachment #1.2: Type: text/html, Size: 1285 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-09 14:16 ` How to let a macro check the previous value of #1 the last time the same macro was called? Joel via ntg-context
@ 2022-01-09 15:03   ` Wolfgang Schuster via ntg-context
  2022-01-10  8:52     ` Henning Hraban Ramm via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Wolfgang Schuster via ntg-context @ 2022-01-09 15:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Joel via ntg-context; +Cc: Wolfgang Schuster

Joel via ntg-context schrieb am 09.01.2022 um 15:16:
> Is there a way for a macro to check the previous value of #1, the last 
> time that same macro was called?
> 
> Here is a minimum working example, pretending that `\previousvalue` is 
> equal to #1 from the last time the same macro was called:
> 
> [...]

To check is the current value differs from the last one you need a temp 
macro where you store the current value at the end of your command to 
check it in the next call.

\let\previousmymacro\empty

\define[1]\mymacro
   {\edef\currentmymacro{#1}%
    \ifx\previousmymacro\currentmymacro
      same as last time
    \else
      it is different from last time
    \fi
    \let\previousmymacro\currentmymacro}

\starttext

\startlines
cat: \mymacro{cat}
cat: \mymacro{cat}
mouse: \mymacro{mouse}
mouse: \mymacro{mouse}
cat: \mymacro{cat}
\stoplines

\stoptext

Wolfgang

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-09 15:03   ` Wolfgang Schuster via ntg-context
@ 2022-01-10  8:52     ` Henning Hraban Ramm via ntg-context
  2022-01-10 19:21       ` Wolfgang Schuster via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Henning Hraban Ramm via ntg-context @ 2022-01-10  8:52 UTC (permalink / raw)
  To: ntg-context; +Cc: Henning Hraban Ramm

Am 09.01.22 um 16:03 schrieb Wolfgang Schuster via ntg-context:
> Joel via ntg-context schrieb am 09.01.2022 um 15:16:
>> Is there a way for a macro to check the previous value of #1, the last 
>> time that same macro was called?
> 
> To check is the current value differs from the last one you need a temp 
> macro where you store the current value at the end of your command to 
> check it in the next call.

Would it make more sense, or would it be “cleaner” to use a variable?

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-10  8:52     ` Henning Hraban Ramm via ntg-context
@ 2022-01-10 19:21       ` Wolfgang Schuster via ntg-context
  2022-01-10 23:15         ` Henning Hraban Ramm via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Wolfgang Schuster via ntg-context @ 2022-01-10 19:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Henning Hraban Ramm via ntg-context
  Cc: Wolfgang Schuster

Henning Hraban Ramm via ntg-context schrieb am 10.01.2022 um 09:52:
> Am 09.01.22 um 16:03 schrieb Wolfgang Schuster via ntg-context:
>> Joel via ntg-context schrieb am 09.01.2022 um 15:16:
>>> Is there a way for a macro to check the previous value of #1, the 
>>> last time that same macro was called?
>>
>> To check is the current value differs from the last one you need a 
>> temp macro where you store the current value at the end of your 
>> command to check it in the next call.
>
> Would it make more sense, or would it be “cleaner” to use a variable?

You can get rid of the temp variable before the command definition but 
now you have to access it with a different method in \mymacro, below is 
one way (LMTX only) but \setvariable and \getvariable work as well.

\define[1]\mymacro
   {\iftok{#1}{\getvalue{previousmymacro}}%
      same as last time
    \else
      it is different from last time
    \fi
    \setvalue{previousmymacro}{#1}}

Wolfgang

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-10 19:21       ` Wolfgang Schuster via ntg-context
@ 2022-01-10 23:15         ` Henning Hraban Ramm via ntg-context
  2022-01-11  4:22           ` Aditya Mahajan via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Henning Hraban Ramm via ntg-context @ 2022-01-10 23:15 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Henning Hraban Ramm

Am 10.01.22 um 20:21 schrieb Wolfgang Schuster:
> Henning Hraban Ramm via ntg-context schrieb am 10.01.2022 um 09:52:
>> Would it make more sense, or would it be “cleaner” to use a variable?
> 
> You can get rid of the temp variable before the command definition but 
> now you have to access it with a different method in \mymacro, below is 
> one way (LMTX only) but \setvariable and \getvariable work as well.
> 
> \define[1]\mymacro
>    {\iftok{#1}{\getvalue{previousmymacro}}%
>       same as last time
>     \else
>       it is different from last time
>     \fi
>     \setvalue{previousmymacro}{#1}}

That was the approach that I meant. Thank you.

But would you consider one way to be better?

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-10 23:15         ` Henning Hraban Ramm via ntg-context
@ 2022-01-11  4:22           ` Aditya Mahajan via ntg-context
  2022-01-11  8:20             ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 7+ messages in thread
From: Aditya Mahajan via ntg-context @ 2022-01-11  4:22 UTC (permalink / raw)
  To: Henning Hraban Ramm via ntg-context; +Cc: Aditya Mahajan

[-- Attachment #1: Type: text/plain, Size: 1430 bytes --]

On Tue, 11 Jan 2022, Henning Hraban Ramm via ntg-context wrote:

> Am 10.01.22 um 20:21 schrieb Wolfgang Schuster:
> > Henning Hraban Ramm via ntg-context schrieb am 10.01.2022 um 09:52:
> >> Would it make more sense, or would it be “cleaner” to use a variable?
> > 
> > You can get rid of the temp variable before the command definition but 
> > now you have to access it with a different method in \mymacro, below is 
> > one way (LMTX only) but \setvariable and \getvariable work as well.
> > 
> > \define[1]\mymacro
> >    {\iftok{#1}{\getvalue{previousmymacro}}%
> >       same as last time
> >     \else
> >       it is different from last time
> >     \fi
> >     \setvalue{previousmymacro}{#1}}
> 
> That was the approach that I meant. Thank you.
> 
> But would you consider one way to be better?

Just for fun: lua code

\startluacode
  local previous = nil

  interfaces.implement {
    name = "mymacro",
    public = true, 
    arguments = "string",
    actions = function (current) 
          if current == previous then
              context("same as last time")
          else
              context("Different!")
          end
          previous = current
      end,
  }

\stopluacode

\starttext

\startlines
cat: \mymacro{cat}
cat: \mymacro{cat}
mouse: \mymacro{mouse}
mouse: \mymacro{mouse}
cat: \mymacro{cat}
\stoplines

\stoptext

Aditya

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to let a macro check the previous value of #1 the last time the same macro was called?
  2022-01-11  4:22           ` Aditya Mahajan via ntg-context
@ 2022-01-11  8:20             ` Hans Hagen via ntg-context
  0 siblings, 0 replies; 7+ messages in thread
From: Hans Hagen via ntg-context @ 2022-01-11  8:20 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen

On 1/11/2022 5:22 AM, Aditya Mahajan via ntg-context wrote:
> On Tue, 11 Jan 2022, Henning Hraban Ramm via ntg-context wrote:
> 
>> Am 10.01.22 um 20:21 schrieb Wolfgang Schuster:
>>> Henning Hraban Ramm via ntg-context schrieb am 10.01.2022 um 09:52:
>>>> Would it make more sense, or would it be “cleaner” to use a variable?
>>>
>>> You can get rid of the temp variable before the command definition but
>>> now you have to access it with a different method in \mymacro, below is
>>> one way (LMTX only) but \setvariable and \getvariable work as well.
>>>
>>> \define[1]\mymacro
>>>     {\iftok{#1}{\getvalue{previousmymacro}}%
>>>        same as last time
>>>      \else
>>>        it is different from last time
>>>      \fi
>>>      \setvalue{previousmymacro}{#1}}
>>
>> That was the approach that I meant. Thank you.
>>
>> But would you consider one way to be better?
> 
> Just for fun: lua code
> 
> \startluacode
>    local previous = nil
> 
>    interfaces.implement {
>      name = "mymacro",
>      public = true,
>      arguments = "string",
>      actions = function (current)
>            if current == previous then
>                context("same as last time")
>            else
>                context("Different!")
>            end
>            previous = current
>        end,
>    }
> 
> \stopluacode
> 
> \starttext
> 
> \startlines
> cat: \mymacro{cat}
> cat: \mymacro{cat}
> mouse: \mymacro{mouse}
> mouse: \mymacro{mouse}
> cat: \mymacro{cat}
> \stoplines
> 
> \stoptext

keep in mind that what you do is global (probably what one wants here):

\startlines
cat: \mymacro{cat}
cat: \mymacro{cat}
{mouse: \mymacro{mouse}}
{mouse: \mymacro{mouse}}
cat: \mymacro{cat}
\stoplines


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-01-11  8:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1050338993.1893942.1641737797345.ref@mail.yahoo.com>
2022-01-09 14:16 ` How to let a macro check the previous value of #1 the last time the same macro was called? Joel via ntg-context
2022-01-09 15:03   ` Wolfgang Schuster via ntg-context
2022-01-10  8:52     ` Henning Hraban Ramm via ntg-context
2022-01-10 19:21       ` Wolfgang Schuster via ntg-context
2022-01-10 23:15         ` Henning Hraban Ramm via ntg-context
2022-01-11  4:22           ` Aditya Mahajan via ntg-context
2022-01-11  8:20             ` Hans Hagen via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).