ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* METAPOST vardef returning multiple and non-numeric types as a single answer?
@ 2020-03-15 10:22 Gerben Wierda
  2020-03-16  8:31 ` Taco Hoekwater
  0 siblings, 1 reply; 8+ messages in thread
From: Gerben Wierda @ 2020-03-15 10:22 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

I would like a vardef macro to return the pair of a picture and a bounding box. I don't want to use the setbound operator, because as soon as I do that, I cannot access the components of the picture anymore with pathpart.

I found metapost vardef returning multiple values <https://tex.stackexchange.com/questions/448740/metapost-vardef-returning-multiple-values> on StackExchange but that is about returning multiple values of the same numeric type. Is it possible to have vardef return a set of (picture, path) or (picture, picture) in some way? If not, I need to make two vardef macros for each result type.

I tried many things, amongst which:

vardef Foo( expr w, h)
% define pic
pic
end group,begingroup
% define pic
pic
endif

picture foo[]; foo = Foo( 2, 4);


[-- Attachment #1.2: Type: text/html, Size: 11438 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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-15 10:22 METAPOST vardef returning multiple and non-numeric types as a single answer? Gerben Wierda
@ 2020-03-16  8:31 ` Taco Hoekwater
  2020-03-16  8:53   ` Gerben Wierda
  0 siblings, 1 reply; 8+ messages in thread
From: Taco Hoekwater @ 2020-03-16  8:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 15 Mar 2020, at 11:22, Gerben Wierda <gerben.wierda@rna.nl> wrote:
> 
> I would like a vardef macro to return the pair of a picture and a bounding box. I don't want to use the setbound operator, because as soon as I do that, I cannot access the components of the picture anymore with pathpart.
> 
> I found metapost vardef returning multiple values on StackExchange but that is about returning multiple values of the same numeric type. Is it possible to have vardef return a set of (picture, path) or (picture, picture) in some way? If not, I need to make two vardef macros for each result type.

Sure, using that same endgroup,begingroup trick. But since there is no multi-assignment in metapost that doesn’t you help much.
Either two separate vardefs or passing the to-be-assigned variable(s) as an extra argument(s) are generally more practical.

Best wishes,
Taco



___________________________________________________________________________________
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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  8:31 ` Taco Hoekwater
@ 2020-03-16  8:53   ` Gerben Wierda
  2020-03-16  9:10     ` Taco Hoekwater
  0 siblings, 1 reply; 8+ messages in thread
From: Gerben Wierda @ 2020-03-16  8:53 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hola! So, the arguments passed to macros are by reference and not by value? I could have known of course, they are simple expansions, but I’d like to be sure. IfI assign to a variable inside a vardef macro and that variable is not ’save’d I’m changing the original?

G

> On 16 Mar 2020, at 09:31, Taco Hoekwater <taco@elvenkind.com> wrote:
> 
> 
> 
>> On 15 Mar 2020, at 11:22, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>> 
>> I would like a vardef macro to return the pair of a picture and a bounding box. I don't want to use the setbound operator, because as soon as I do that, I cannot access the components of the picture anymore with pathpart.
>> 
>> I found metapost vardef returning multiple values on StackExchange but that is about returning multiple values of the same numeric type. Is it possible to have vardef return a set of (picture, path) or (picture, picture) in some way? If not, I need to make two vardef macros for each result type.
> 
> Sure, using that same endgroup,begingroup trick. But since there is no multi-assignment in metapost that doesn’t you help much.
> Either two separate vardefs or passing the to-be-assigned variable(s) as an extra argument(s) are generally more practical.
> 
> Best wishes,
> Taco
> 
> 
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________

___________________________________________________________________________________
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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  8:53   ` Gerben Wierda
@ 2020-03-16  9:10     ` Taco Hoekwater
  2020-03-16  9:23       ` Taco Hoekwater
  0 siblings, 1 reply; 8+ messages in thread
From: Taco Hoekwater @ 2020-03-16  9:10 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 16 Mar 2020, at 09:53, Gerben Wierda <gerben.wierda@rna.nl> wrote:
> 
> Hola! So, the arguments passed to macros are by reference and not by value? I could have known of course, they are simple expansions, but I’d like to be sure. IfI assign to a variable inside a vardef macro and that variable is not ’save’d I’m changing the original?

Yes. (A simple test would have confirmed that)

Taco


___________________________________________________________________________________
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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  9:10     ` Taco Hoekwater
@ 2020-03-16  9:23       ` Taco Hoekwater
  2020-03-16  9:38         ` Hans Hagen
  2020-03-16  9:48         ` Gerben Wierda
  0 siblings, 2 replies; 8+ messages in thread
From: Taco Hoekwater @ 2020-03-16  9:23 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 16 Mar 2020, at 10:10, Taco Hoekwater <taco@elvenkind.com> wrote:
> 
> 
> 
>> On 16 Mar 2020, at 09:53, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>> 
>> Hola! So, the arguments passed to macros are by reference and not by value? I could have known of course, they are simple expansions, but I’d like to be sure. IfI assign to a variable inside a vardef macro and that variable is not ’save’d I’m changing the original?
> 
> Yes. (A simple test would have confirmed that)

Oops, sorry, no! I was wrong on that… it must be too early for me.

  vardef Foo(text y) = 
    scantokens(y&":=5”);
  enddef;
  Foo("x");

This would work, but that is weird. This also works, and that is why
I had the erroneous memory that it would work always:

  vardef Foo(expr a)(text y) = 
    y[a] := 5;
  enddef;
  numeric foo[]; 
  foo[1] = 6;
  Foo(1,foo);

or you could use global variables, of course.

Main point: Metapost is not an easy language to grasp. If you want to
really understand how it works, you should study the Metafont book by DEK.
As you can clearly see, even experienced users are likely to make mistakes.


Best wishes,
Taco




___________________________________________________________________________________
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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  9:23       ` Taco Hoekwater
@ 2020-03-16  9:38         ` Hans Hagen
  2020-03-16  9:52           ` Gerben Wierda
  2020-03-16  9:48         ` Gerben Wierda
  1 sibling, 1 reply; 8+ messages in thread
From: Hans Hagen @ 2020-03-16  9:38 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Taco Hoekwater

On 3/16/2020 10:23 AM, Taco Hoekwater wrote:
> 
> 
>> On 16 Mar 2020, at 10:10, Taco Hoekwater <taco@elvenkind.com> wrote:
>>
>>
>>
>>> On 16 Mar 2020, at 09:53, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>>>
>>> Hola! So, the arguments passed to macros are by reference and not by value? I could have known of course, they are simple expansions, but I’d like to be sure. IfI assign to a variable inside a vardef macro and that variable is not ’save’d I’m changing the original?
>>
>> Yes. (A simple test would have confirmed that)
> 
> Oops, sorry, no! I was wrong on that… it must be too early for me.
> 
>    vardef Foo(text y) =
>      scantokens(y&":=5”);
>    enddef;
>    Foo("x");
> 
> This would work, but that is weird. This also works, and that is why
> I had the erroneous memory that it would work always:
> 
>    vardef Foo(expr a)(text y) =
>      y[a] := 5;
>    enddef;
>    numeric foo[];
>    foo[1] = 6;
>    Foo(1,foo);
> 
> or you could use global variables, of course.
> 
> Main point: Metapost is not an easy language to grasp. If you want to
> really understand how it works, you should study the Metafont book by DEK. > As you can clearly see, even experienced users are likely to make 
mistakes.
it's the mixture of delayed parsing and expansion and ... no other 
language like that i guess (which at the same time is its charm)

passing "text y" is (i think) not that that far from the scantokens 
(running over already tokenized vs running over a string while 
tokenizing it ... but these input models (are we in scanning file input, 
string input, tokenlist) can be kind of confusing

that said, after muy share of mp programming i would not have thought 
about the text y as pass by reference, also because it's only true for 
these pseudo arrays (maybe suffixes also work)

Hans

-----------------------------------------------------------------
                                           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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  9:23       ` Taco Hoekwater
  2020-03-16  9:38         ` Hans Hagen
@ 2020-03-16  9:48         ` Gerben Wierda
  1 sibling, 0 replies; 8+ messages in thread
From: Gerben Wierda @ 2020-03-16  9:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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



> On 16 Mar 2020, at 10:23, Taco Hoekwater <taco@elvenkind.com> wrote:
> 
> 
> 
>> On 16 Mar 2020, at 10:10, Taco Hoekwater <taco@elvenkind.com> wrote:
>> 
>> 
>> 
>>> On 16 Mar 2020, at 09:53, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>>> 
>>> Hola! So, the arguments passed to macros are by reference and not by value? I could have known of course, they are simple expansions, but I’d like to be sure. IfI assign to a variable inside a vardef macro and that variable is not ’save’d I’m changing the original?
>> 
>> Yes. (A simple test would have confirmed that)
> 
> Oops, sorry, no! I was wrong on that… it must be too early for me.

Coffee! Thanks anyway, Taco, your help is really appreciated.

> 
>  vardef Foo(text y) = 
>    scantokens(y&":=5”);
>  enddef;
>  Foo("x");
> 
> This would work, but that is weird.

Which is somewhat intuitive to parse as a human. I understand it as that after the last statement x has the value 5.

> This also works, and that is why
> I had the erroneous memory that it would work always:
> 
>  vardef Foo(expr a)(text y) = 
>    y[a] := 5;
>  enddef;
>  numeric foo[]; 
>  foo[1] = 6;
>  Foo(1,foo);

This was initially complete gobbledegook to me. But I now think that after the last statement foo[1] equals 5. Very weird/counterintuitive that y[a] := 5 works here without the use of scantokens. The first one is less weird to me because scantokens actually calls the METAPOST parser on a string as if read from a file.

> or you could use global variables, of course.
> 
> Main point: Metapost is not an easy language to grasp. If you want to
> really understand how it works, you should study the Metafont book by DEK.

I actually have that book and did a try small bit of METAFONT long, long ago to create a logo in it. Maybe that explains things better. (Unearthing the book for later use…)

> As you can clearly see, even experienced users are likely to make mistakes.

The main problem with all of this (ConTeXt, METAPOST, etc.) remains the learning curve and the lack of good and up-to-date educational materials. I.e. the METAPOST manual is terse, probably because it relies on people understanding the background that is in the METAFONT book.

And, if one is used to thinking in terms of functional programming, all that ‘expansion’ stuff (‘replacement’  programming?) is very counterintuitive for me. Lua then adds a functional model to the mix (or I think it does). Very hard to make progress. I learned Python in a day and produced a complex program in a matter of weeks. This is far less easy. And not being able so far to see what intermediate results lea to errors adds to the slow going.

I.e., the whole chapter on vardef in the METAFONT book is 

G

> 
> 
> Best wishes,
> Taco
> 
> 
> 
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________


[-- Attachment #1.2.1: Type: text/html, Size: 6144 bytes --]

[-- Attachment #1.2.2: dangerous-bend.jpeg --]
[-- Type: image/jpeg, Size: 10230 bytes --]

[-- Attachment #1.2.3: dangerous-bend.jpeg --]
[-- Type: image/jpeg, Size: 10230 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] 8+ messages in thread

* Re: METAPOST vardef returning multiple and non-numeric types as a single answer?
  2020-03-16  9:38         ` Hans Hagen
@ 2020-03-16  9:52           ` Gerben Wierda
  0 siblings, 0 replies; 8+ messages in thread
From: Gerben Wierda @ 2020-03-16  9:52 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

Maybe a nice little explanation (educational with good examples) of ‘how to use pass by reference’ and ‘building and using complex data structures’ would be useful for people. Say, some ‘patterns’.

G  

> On 16 Mar 2020, at 10:38, Hans Hagen <j.hagen@xs4all.nl> wrote:
> 
> that said, after muy share of mp programming i would not have thought about the text y as pass by reference, also because it's only true for these pseudo arrays (maybe suffixes also work)


[-- Attachment #1.2: Type: text/html, Size: 1671 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] 8+ messages in thread

end of thread, other threads:[~2020-03-16  9:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-15 10:22 METAPOST vardef returning multiple and non-numeric types as a single answer? Gerben Wierda
2020-03-16  8:31 ` Taco Hoekwater
2020-03-16  8:53   ` Gerben Wierda
2020-03-16  9:10     ` Taco Hoekwater
2020-03-16  9:23       ` Taco Hoekwater
2020-03-16  9:38         ` Hans Hagen
2020-03-16  9:52           ` Gerben Wierda
2020-03-16  9:48         ` Gerben Wierda

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).