替换 Head() ?在 2024 年 Discovery Summit 的脚本分组讨论室中,我们刚刚讨论了是否有一个 JSL 函数可以替换表达式的头。
类似于
Replace Head(myExpression, <STRIKE>Expr(original head()),</STRIKE> Expr(new head()));
编辑: Expr(原始 head())
... 转换
original head(args)
进入
new head(args)
在这种情况下,人们可能会想到替换。除了替换字符串
Substitute ("hello", "h", "J", "l", "f", "o", "")也可以使用 Substitute 来替换表达式,例如
Substitute({1+2},Expr(add()),Expr(subtract()))
虽然很接近,但还远远不够,例如
Substitute({1,2,{3,4}}, Expr(List()),Expr(Show()));将返回Show( 1, 2, Show( 3, 4 ) )
:(
一种可能的解决方案:将表达式转换为字符串并使用正则表达式,然后转换回来。
当您了解表达式处理的魔力时,这会很痛苦:)
到目前为止最好的解决方案-通过表达式处理:
不要替换头部,而是提取所有参数并将它们插入到新的头部中。
var = {1,2,{3,4}};
MyExpr = Expr(Show());
for(i=1, i<=NArg(var), i++,
InsertInto(MyExpr, Arg(var, i))
);
NameExpr(MyExpr);

自动化和脚本回复:更换 Head()?如果您没有列表,那么只替换头部会更容易一些。有了列表,事情就变得更加复杂了,我会选择只重建表达式。
当我遇到此项目“替换首次出现”时,我正在编写愿望清单请求,灵感来自 Python 的 str.replace。已经有人建议只替换第一次出现的情况,这将涵盖最“需要”的出现次数(1 次和全部)。
回复:更换 Head()?好的,谢谢。
convertToList=
Function( {myExpr},{myList,i},
myList = {};
For( i = 1, i <= N Arg( Name Expr( myExpr ) ), i++,
Insert Into( myList, Arg( Name Expr( myExpr ), i ) )
);
Return( Name Expr( myList ) );
),
编辑:
[我刚刚在我的 HERE 命名空间中发现了“i”——没想到:
我大部分时间都使用它。
--> “ i ” 添加为函数的局部变量
编辑2:
不要使用此代码!会导致错误结果: JMP Expr() 奇怪,有人可以解释吗?
-> 使用下面@jthi的解决方案!
查看更多...convertToList = Function( {myExpr},
{tmp},
tmp = Name Expr( myExpr );
Remove From( tmp, 1, N Arg( tmp ) );
) 回复:更换 Head()?有各种各样的选择(好的、坏的、容易理解的、难以理解的以及介于两者之间的和混合的)。
这些将修改原始表达式,因此如果不需要,应使用表达式的副本(例如 myexpr1_copy = Name Expr(myexpr1);)
Names Default To Here(1);
// Replace outmost Max with Min
myexpr1 = Expr(Max(1, Max(1,2)));
args = Remove From(myexpr1, 1, N Arg(myexpr1));
myexpr_new = Substitute(args, Head(List()), Expr(Min()));
Show(myexpr_new);
// Change outmost List into Max
myexpr2 = {1, {1,2}};
var = Head(myexpr);
Substitute Into(var, Expr(List()), Expr(Show()));
While(N Items(arg = Remove From(myexpr2, 1)), Insert Into(var, arg[1]));
show(var);
// "Change" outmost Max into list
myexpr3 = Expr(Max(1, Max(1,2)));
mylist = Remove From(myexpr3, 1, N Arg(myexpr3));
Show(mylist);回复:更换 Head()?哇,这非常聪明!
查看Remove From()的回收站
凉爽的!
回复:更换 Head()?我刚刚找到了另一个解决方案 - 它基于 For Each 的超能力:)

myexpr1 = Expr(Max(1, Max(1,2)));
myexpr_new = Expr (min());
For each({arg},Name Expr(myExpr1),
Insert Into(myexpr_new, Name Expr(arg))
);
show(myexpr_new )
回复:更换 Head()?我很高兴地宣布——一个非常通用的Replace Head() :
Add Custom Functions(
New Custom Function(
"hogi",
"replace head",
Function( {orig, new Head},
{Default Local},
mydummy = Expr( dummy() );
For Each( {arg}, Name Expr( orig ), Insert Into( mydummy, arg ) );
x = Substitute( Name Expr( mydummy ), Expr( dummy() ), Name Expr( new Head ) );
//Eval(Substitute(Expr(Print(Expr(_x_))), Expr(_x_), Name Expr(x)));
Eval(Substitute(Expr(return(Expr(Expr(_x_)))), Expr(_x_), Name Expr(x))); // *)
),
<< description("takes the first argument and replaces its head with the second argument"),
<< example(Expr(
hogi:replace head({1,2,{3,4}}, Expr(Show()));
orig = Expr(Max(1,2,3));
hogi:replace head(Name Expr(orig), Expr(Show()));
))
)
);添加到有用的自定义功能
*) 就像
https://community.jmp.com/t5/JMP-Wish-List/Substitute-First-Occurrence/idc-p/825822/highlight/true#M6863
但是+ 1x Expr() - 由于自定义函数?!?
回复:更换 Head()?这将函数简化为:
Add Custom Functions(
New Custom Function(
"hogi",
"replace head",
Function( {orig, new Head},
{Default Local},
Transform Each( {arg}, Name Expr( orig ), Output("Expression", new Head), Name expr(arg ) );
),
<< description("takes the first argument and replaces its head with the second argument - secnd argument: String!!"),
<< example(Expr(
hogi:replace head({1,2,{3,4}}, "Show");
orig = myexpr = Expr(Max(1, Max(1,2)));
hogi:replace head(Name Expr(orig), "List");
))
)
); 请注意,新的 Head 必须以字符串形式提供,而不是以表达式形式提供!
回复:更换 Head()?@jthi刚刚提到了Transfrom Each + Output
https://community.jmp.com/t5/Discussions/Slicing-Associative-Arrays/mp/828081/highlight/true#M100979
有一个选项可以更换头部:

它的工作原理如下:
ex = Transform Each( {value},{10, 20, 30}, Output( "Expression", "My Values" ), value + 1);并且它也适用于表达式:)
myexpr = Expr(Max(1, Max(1,2)));
Transform Each({entry},Name Expr(myExpr), Output( "Expression", "List"),Name Expr(entry))