gpt4 book ai didi

postscript - 在 PostScript : How to place some stack elements (computed at run-time) into a procedure that can be assigned to a name? 中

转载 作者:行者123 更新时间:2023-12-02 08:20:07 26 4
gpt4 key购买 nike

在运行时之前已知值为 10 和 20 的示例,以便更好地理解以下实际问题:

/point1 { 10 20 } def 将数字 10 和 20 放入(匿名)过程然后将其分配给名称 point1(因此它不再是匿名的)。然后可以使用 name point ,即每当解释器找到它时,它将执行 { 10 20 },即 10 和 20 将被压入堆栈。

执行def前后的堆栈:

Stacke before:             Stack after:
{ 10 20 } -
/point1

Dict before: Dict after:
- point1 --> { 10 20 }

现在是真正的问题:假设将在运行时计算两个值 10 和 20。如何分配它们(或任意数量的顶部 n 堆栈元素)到一个给定的名字以便以后使用它?

Stacke before:             Stack after:
<y> -
<x>
/<name>

Dict before: Dict after:
- <name> --> { <x> <y> }

最佳答案

在后记中,过程只是设置了可执行标志 的数组。因此,您可以构造一个数组(随心所欲),然后对其调用 cvx

/x 3 def
/y 4 def
[ x y ] cvx % { 3 4 }
x y [ 3 1 roll ] cvx
x y 2 array astore cvx
{ //x //y }
({//x //y}) cvx exec
({//x //y}) token pop exch pop

因此,对于您假设的程序,可以这样完成:

/makepairproc { % x y  ->  { x y }
[ 3 1 roll ] cvx
} def

您可以做的另一件有趣的事情是同时拥有一个可执行数组和一个相同底层数组 的文字数组。您可以使用一个定义为过程 名称,另一个定义为目标。这样您就可以更新内容而无需每次都分配新的内存。

/point1 { 10 20 } def
/point1arr //point1 cvlit def

30 40 point1arr astore %update contents
point1 % 30 40 %execute contents

关于postscript - 在 PostScript : How to place some stack elements (computed at run-time) into a procedure that can be assigned to a name? 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38002304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com