cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
hogi
Level XII

Replace Head() ?

In the Scripting breakout room of Discovery Summit 2024 we just discussed if there is a JSL function to replace the head of an expression.

 

Something like

Replace Head(myExpression, Expr(original head()), Expr(new head()));

to convert 

original head(args)

into

new head(args)

In this context, one might think of Substitute. Besides replacing strings

Substitute ("hello", "h", "J", "l", "f", "o", "")

one can use Substitute also to replace Expressions, like in 

Substitute({1+2},Expr(add()),Expr(subtract()))


It gets close, but it is far from optimal, e.g.

Substitute({1,2,{3,4}}, Expr(List()),Expr(Show()));

will return  Show( 1, 2, Show( 3, 4 ) )

: (

 

A possible solution: convert the expression to a string and use Regex, then convert back.
this hurts when you know the magic of Expression handling : )

 

The best solution up to now - via expression handling:
Instead of replacing the head, extract all the arguments and insert them into the new head.

var = {1,2,{3,4}};
MyExpr = Expr(Show());

for(i=1, i<=NArg(var), i++,
	InsertInto(MyExpr, Arg(var, i))
);
NameExpr(MyExpr);

 

hogi_1-1726692298724.png

2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Replace Head() ?

If you don't have lists it is a bit easier to replace just the head. With lists it gets more complicated and I would go with option of just rebuilding the expression.

 

I was writing wish list request inspired by Python's str.replace when I came across this item Substitute First Occurrence . Substituting just the first occurrence has already been suggested and it would cover the most "needed" occurrence counts (1 and all).

-Jarmo

View solution in original post

jthi
Super User

Re: Replace Head() ?

There all sorts of options (good, bad, easy to understand, difficult to understand and everything in between and mixed).

 

These will modify the original expressions, so copies of the expressions (myexpr1_copy = Name Expr(myexpr1); for example) should be used if that is not desired

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);
-Jarmo

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Replace Head() ?

If you don't have lists it is a bit easier to replace just the head. With lists it gets more complicated and I would go with option of just rebuilding the expression.

 

I was writing wish list request inspired by Python's str.replace when I came across this item Substitute First Occurrence . Substituting just the first occurrence has already been suggested and it would cover the most "needed" occurrence counts (1 and all).

-Jarmo
hogi
Level XII

Re: Replace Head() ?

Ok, thanks.

 

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 ) );
		),


edit: 
[I just found "i" in my HERE namespace - didn't expect that:
I use for each most of the time.
-->  "i" added as local variable of the function

 

edit2:
don't use this code !!! can lead to wrong results: JMP Expr() weirdness, can anyone explain? 
-> use @jthi 's solution below!

View more...
convertToList = Function( {myExpr},
	{tmp},
	tmp = Name Expr( myExpr );
	Remove From( tmp, 1, N Arg( tmp ) );
)
jthi
Super User

Re: Replace Head() ?

There all sorts of options (good, bad, easy to understand, difficult to understand and everything in between and mixed).

 

These will modify the original expressions, so copies of the expressions (myexpr1_copy = Name Expr(myexpr1); for example) should be used if that is not desired

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);
-Jarmo
hogi
Level XII

Re: Replace Head() ?

wow, this is very clever!
look into the recycle bin of Remove From()

cool!