ヘッド()を置き換えますか?Discovery Summit 2024 のスクリプト ブレイクアウト ルームで、式の先頭を置き換える JSL 関数があるかどうかについて議論しました。
たとえば
Replace Head(myExpression, <STRIKE>Expr(original head()),</STRIKE> Expr(new head()));
編集: Expr(元のヘッド())
...変換する
original head(args)
の中へ
new head(args)
この文脈では、Substituteを思い浮かべるかもしれません。文字列を置き換えるだけでなく、
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);

自動化とスクリプトRe: Head() を置き換えますか?リストがない場合は、ヘッドだけを置き換える方が少し簡単です。リストがあると複雑になるので、式を再構築するオプションを選択します。
Python の str.replace にヒントを得てウィッシュ リストのリクエストを書いていたときに、この項目Substitute First Occurrenceに出会いました。最初の出現部分のみを置換することは既に提案されており、最も「必要な」出現回数 (1 とすべて) をカバーします。
Re: 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 ) );
) Re: 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);Re: Head() を置き換えますか?わあ、これはとても賢いですね!
Remove From()のごみ箱を調べる
いいね!
Re: 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 )
Re: 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
but + 1x Expr() - カスタム関数が原因ですか?!?
Re: Head() を置き換えますか?@jthi は先ほど、各 + 出力からの変換について言及しました
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))
Re: 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");
))
)
); 新しいヘッドは式ではなく文字列として提供する必要があることに注意してください。