<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Replace Head() ? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801233#M97687</link>
    <description>&lt;P&gt;wow, this is very clever!&lt;BR /&gt;look into the recycle bin of &lt;FONT face="courier new,courier"&gt;Remove From()&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;cool!&lt;/P&gt;</description>
    <pubDate>Sun, 22 Sep 2024 12:44:03 GMT</pubDate>
    <dc:creator>hogi</dc:creator>
    <dc:date>2024-09-22T12:44:03Z</dc:date>
    <item>
      <title>Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/800061#M97581</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Replace Head(myExpression, &lt;STRIKE&gt;Expr(original head()),&lt;/STRIKE&gt; Expr(new head()));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;edit:&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;Expr(original head())&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;... to convert&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;original head(args)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;into&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;new head(args)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this context, one might think of Substitute. Besides replacing strings&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Substitute ("hello", "h", "J", "l", "f", "o", "")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;one can use Substitute also to replace Expressions, like in&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Substitute({1+2},Expr(add()),Expr(subtract()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;It gets close, but it is far from optimal, e.g.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Substitute({1,2,{3,4}}, Expr(List()),Expr(Show()));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;will return&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;Show( 1, 2, Show( 3, 4 ) ) &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;: (&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A possible solution: convert the expression to a string and use Regex, then convert back.&lt;BR /&gt;this hurts when you know the magic of Expression handling : )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The best solution up to now - via expression handling:&lt;BR /&gt;Instead of replacing the head, extract all the arguments and insert them into the new head.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;var = {1,2,{3,4}};
MyExpr = Expr(Show());

for(i=1, i&amp;lt;=NArg(var), i++,
	InsertInto(MyExpr, Arg(var, i))
);
NameExpr(MyExpr);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_1-1726692298724.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/68438i9AEB3DC94402A117/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_1-1726692298724.png" alt="hogi_1-1726692298724.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 21:44:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/800061#M97581</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-01-03T21:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/800207#M97595</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was writing wish list request inspired by Python's str.replace when I came across this item&amp;nbsp;&lt;LI-MESSAGE title="Substitute First Occurrence" uid="574766" url="https://community.jmp.com/t5/JMP-Wish-List/Substitute-First-Occurrence/m-p/574766#U574766" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;. Substituting just the first occurrence has already been suggested and it would cover the most "needed" occurrence counts (1 and all).&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 11:20:54 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/800207#M97595</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-09-19T11:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801217#M97683</link>
      <description>&lt;P&gt;Ok, thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;convertToList=
		Function( {myExpr},{myList,i},
			myList = {};
			For( i = 1, i &amp;lt;= N Arg( Name Expr( myExpr ) ), i++,
				Insert Into( myList, Arg( Name Expr( myExpr ), i ) )
			);
			Return( Name Expr( myList ) );
		),&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;U&gt;edit:&lt;/U&gt;&amp;nbsp;&lt;BR /&gt;[I just found "i" in my HERE namespace - didn't expect that:&lt;BR /&gt;I use &lt;FONT face="courier new,courier"&gt;for each&lt;/FONT&gt; most of the time.&lt;BR /&gt;--&amp;gt;&amp;nbsp; "&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;"&amp;nbsp;added as local variable of the function&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit2:&lt;BR /&gt;don't use this code !!! can lead to wrong results:&amp;nbsp;&lt;LI-MESSAGE title="JMP Expr() weirdness, can anyone explain?" uid="400469" url="https://community.jmp.com/t5/Discussions/JMP-Expr-weirdness-can-anyone-explain/m-p/400469#U400469" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;BR /&gt;-&amp;gt; use &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;'s solution below!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;convertToList = Function( {myExpr},
	{tmp},
	tmp = Name Expr( myExpr );
	Remove From( tmp, 1, N Arg( tmp ) );
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;</description>
      <pubDate>Tue, 24 Sep 2024 05:17:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801217#M97683</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2024-09-24T05:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801232#M97686</link>
      <description>&lt;P&gt;There all sorts of options (good, bad, easy to understand, difficult to understand and everything in between and mixed).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Sep 2024 12:14:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801232#M97686</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-09-22T12:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801233#M97687</link>
      <description>&lt;P&gt;wow, this is very clever!&lt;BR /&gt;look into the recycle bin of &lt;FONT face="courier new,courier"&gt;Remove From()&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;cool!&lt;/P&gt;</description>
      <pubDate>Sun, 22 Sep 2024 12:44:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/801233#M97687</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2024-09-22T12:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/825821#M100637</link>
      <description>&lt;P&gt;I just found another solution - it's based on the superpowers of For Each : )&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_1-1735936408581.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/71672i5228AFAB36AF3AF3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_1-1735936408581.png" alt="hogi_1-1735936408581.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 20:34:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/825821#M100637</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-01-03T20:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/825823#M100638</link>
      <description>&lt;P&gt;I am happy to announce - a quite universal &lt;FONT face="courier new,courier"&gt;Replace Head()&lt;/FONT&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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))); // *)
		),
		&amp;lt;&amp;lt; description("takes the first argument and replaces its head with the second argument"),
		&amp;lt;&amp;lt; 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()));			
		))
	)
);&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;added&amp;nbsp; to&amp;nbsp;&lt;LI-MESSAGE title="helpful Custom Functions" uid="825740" url="https://community.jmp.com/t5/Discussions/helpful-Custom-Functions/m-p/825740#U825740" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;*) like in&amp;nbsp;&lt;BR /&gt;&lt;A href="https://community.jmp.com/t5/JMP-Wish-List/Substitute-First-Occurrence/idc-p/825822/highlight/true#M6863" target="_blank" rel="noopener"&gt;https://community.jmp.com/t5/JMP-Wish-List/Substitute-First-Occurrence/idc-p/825822/highlight/true#M6863&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;but &lt;FONT face="courier new,courier"&gt;+ 1x Expr()&lt;/FONT&gt; - due to the custom function ?!?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 22:42:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/825823#M100638</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-01-03T22:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/828102#M100987</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp; just mentioned&lt;FONT face="courier new,courier"&gt; Transfrom Each + Output&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.jmp.com/t5/Discussions/Slicing-Associative-Arrays/m-p/828081/highlight/true#M100979" target="_blank" rel="noopener"&gt;https://community.jmp.com/t5/Discussions/Slicing-Associative-Arrays/m-p/828081/highlight/true#M100979&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is an option to replace the head:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_0-1737192974030.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/72027iEB6560703279FE29/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hogi_0-1737192974030.png" alt="hogi_0-1737192974030.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which works like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ex = Transform Each( {value},{10, 20, 30}, Output( "Expression", "My Values" ),	value + 1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and it also works for expressions : )&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;myexpr = Expr(Max(1, Max(1,2)));

Transform Each({entry},Name Expr(myExpr), Output( "Expression", "List"),Name Expr(entry))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 09:39:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/828102#M100987</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-01-18T09:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: Replace Head() ?</title>
      <link>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/828103#M100988</link>
      <description>&lt;P&gt;This reduces the function to:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 ) );
		),
		&amp;lt;&amp;lt; description("takes the first argument and replaces its head with the second argument - secnd argument: String!!"),
		&amp;lt;&amp;lt; 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");			
		))
	)
); &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please note that the new Head has to be provided as String, not as an expression !&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 09:45:53 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Replace-Head/m-p/828103#M100988</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-01-18T09:45:53Z</dc:date>
    </item>
  </channel>
</rss>

