替換頭()?在 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 ) )

可能的解決方案:將表達式轉換為字串並使用 Regex,然後再轉換回來。
當您了解表達式處理的魔力時,這會很痛苦:)
迄今為止最好的解決方案 - 透過表達式處理:
不要替換頭部,而是提取所有參數並將它們插入到新頭部中。
var = {1,2,{3,4}};
MyExpr = Expr(Show());
for(i=1, i<=NArg(var), i++,
InsertInto(MyExpr, Arg(var, i))
);
NameExpr(MyExpr);

自動化和腳本編寫回覆:替換 Head() ?如果您沒有列表,則僅替換頭部會更容易一些。對於列表,它會變得更加複雜,我會選擇僅重建表達式。
當我遇到這個專案Substitute First Occurrence時,我正在編寫願望清單請求,靈感來自 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))