How to define a functional that produces at runtime a function by evaluating selected parts in its definition?Pattern matching Association in rulesHow do I write a ValueQ function that only returns True if there exists an OwnValue?How to make a function that evaluates an expression?function definition with a given list of parameters: how to use Evaluate[] properly?How to properly define this function of functionHow to use a function definition in another functionHow to map a function to all sub-parts of an expression, except sub-parts passing some testDefine function that differentiates w.r.t. its argument before assigning a valueIs using repeatedly a function that outputs its arguments efficient?How to delay a function definition in NestListEvaluating function before its plotting
Cross out words with TikZ: line opacity
Is the EU really banning "toxic propellants" in 2020? How is that going to work?
Is this popular optical illusion made of a grey-scale image with coloured lines?
The grades of the students in a class
How do I find SFDX CLI default installation folder on Mac?
A coworker mumbles to herself when working. How can I ask her to stop?
How to prevent a single-element caster from being useless against immune foes?
How can flights operated by the same company have such different prices when marketed by another?
Backpacking with incontinence
Skipping same old introductions
Normally Closed Optoisolators
Feedback diagram
Why do we need a voltage divider when we get the same voltage at the output as the input?
If I buy and download a game through second Nintendo account do I own it on my main account too?
Can black block with a hanging piece in a back rank mate situation?
Can machine learning learn a function like finding maximum from a list?
Should I put my name first or last in the team members list?
Why are sugars in whole fruits not digested the same way sugars in juice are?
When did J.K. Rowling decide to make Ron and Hermione a couple?
UX writing: When to use "we"?
How to trick a fairly simplistic kill-counter?
Has the US government provided details on plans to deal with AIDS and childhood cancer?
Is there a general term for the items in a directory?
Oath of redemption: Does Emmissary of Peace reflect damage taken from Aura of the Guardian?
How to define a functional that produces at runtime a function by evaluating selected parts in its definition?
Pattern matching Association in rulesHow do I write a ValueQ function that only returns True if there exists an OwnValue?How to make a function that evaluates an expression?function definition with a given list of parameters: how to use Evaluate[] properly?How to properly define this function of functionHow to use a function definition in another functionHow to map a function to all sub-parts of an expression, except sub-parts passing some testDefine function that differentiates w.r.t. its argument before assigning a valueIs using repeatedly a function that outputs its arguments efficient?How to delay a function definition in NestListEvaluating function before its plotting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Consider the following functional as an example:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild][
URLParse[url]/."slot"->Inactive[StringReplace][Slot[]," "->"_"]
]
],Inactive[x_]:>x];
If I evaluate it using the following command:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
It fails probably because the Slot[]
is not aligning with the Function
.
But if I do in-place evaluation of urlModifier["https://www.somewebsite.com/path/slot"]
and use the result as input:
URLBuild[<|"Scheme"->"https","User"->None,"Domain"->"www.somewebsite.com","Port"->None,"Path"->"","path",StringReplace[Slot[]," "->"_"],"Query"->,"Fragment"->None|>]&["hello world!"]
It works fine.
What should I do to urlModifier
to make accept the Slot[]
for its Function
that it creates?
evaluation
$endgroup$
add a comment |
$begingroup$
Consider the following functional as an example:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild][
URLParse[url]/."slot"->Inactive[StringReplace][Slot[]," "->"_"]
]
],Inactive[x_]:>x];
If I evaluate it using the following command:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
It fails probably because the Slot[]
is not aligning with the Function
.
But if I do in-place evaluation of urlModifier["https://www.somewebsite.com/path/slot"]
and use the result as input:
URLBuild[<|"Scheme"->"https","User"->None,"Domain"->"www.somewebsite.com","Port"->None,"Path"->"","path",StringReplace[Slot[]," "->"_"],"Query"->,"Fragment"->None|>]&["hello world!"]
It works fine.
What should I do to urlModifier
to make accept the Slot[]
for its Function
that it creates?
evaluation
$endgroup$
add a comment |
$begingroup$
Consider the following functional as an example:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild][
URLParse[url]/."slot"->Inactive[StringReplace][Slot[]," "->"_"]
]
],Inactive[x_]:>x];
If I evaluate it using the following command:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
It fails probably because the Slot[]
is not aligning with the Function
.
But if I do in-place evaluation of urlModifier["https://www.somewebsite.com/path/slot"]
and use the result as input:
URLBuild[<|"Scheme"->"https","User"->None,"Domain"->"www.somewebsite.com","Port"->None,"Path"->"","path",StringReplace[Slot[]," "->"_"],"Query"->,"Fragment"->None|>]&["hello world!"]
It works fine.
What should I do to urlModifier
to make accept the Slot[]
for its Function
that it creates?
evaluation
$endgroup$
Consider the following functional as an example:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild][
URLParse[url]/."slot"->Inactive[StringReplace][Slot[]," "->"_"]
]
],Inactive[x_]:>x];
If I evaluate it using the following command:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
It fails probably because the Slot[]
is not aligning with the Function
.
But if I do in-place evaluation of urlModifier["https://www.somewebsite.com/path/slot"]
and use the result as input:
URLBuild[<|"Scheme"->"https","User"->None,"Domain"->"www.somewebsite.com","Port"->None,"Path"->"","path",StringReplace[Slot[]," "->"_"],"Query"->,"Fragment"->None|>]&["hello world!"]
It works fine.
What should I do to urlModifier
to make accept the Slot[]
for its Function
that it creates?
evaluation
evaluation
edited 8 hours ago
user13892
asked 8 hours ago
user13892user13892
1,8187 silver badges19 bronze badges
1,8187 silver badges19 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Summary
The root cause of the surprising behaviour is that Function
does not recognize Slot[]
when it appears within an association object (as opposed to an association constructor). To fix it, we must either "unwrap" the association object returned by URLParse
or use a different approach altogether.
The Problem: Association Objects vs. Association Constructors
We can demonstrate the problem with a simpler example:
f = With[ "a" -> #1 , assoc &]
(* <|"a" -> #1|> & *)
The function definition looks okay. But it will not work:
f["hello world!"]
(* <|"a" -> #1|> *)
We can see the structural difference between an association constructor and an association object using TreeForm
:
Function[<| "a" -> # |>] // TreeForm (* constructor *)
Function[Evaluate[<| "a" -> # |>]] // TreeForm (* object *)
More discussion on this distinction can be found in (148095). See the section labelled Ambiguity of Association.
Fixing the Original Definition
We can change the original definition to unwrap the association object during the initial evaluation and to recreate it upon use. We do that using Normal@URLParse[...]
and Inactive[URLBuild@*Association]
. The updated definition looks like this:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild@*Association][
Normal@URLParse[url]/."slot"->Inactive[StringReplace][Slot[1]," "->"_"]
]
],Inactive[x_]:>x];
... so then:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
(* "https://www.somewebsite.com/path/hello_world%21" *)
Alternative Definitions
There are simpler ways to express this operation.
For example, we could define the urlModifier
operator like this:
ClearAll[urlModifier]
urlModifier[url_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[#, " "->"_"]] &
We could also define it explicitly as a "curried form":
ClearAll[urlModifier]
urlModifier[url_][s_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[s," "->"_"]]
The Function
form offers an advantage over the curried form in that, if desired, we could perform the initial URL-parsing at the moment of creation instead of every time the generated function is used:
ClearAll[urlModifier]
urlModifier[url_] :=
With[parsedUrl = URLParse[url]
, URLBuild[parsedUrl /. "slot" -> StringReplace[#, " "->"_"]] &
]
All of these definitions give the same results as the corrected original.
$endgroup$
add a comment |
$begingroup$
fixspace = StringReplace[#, " " -> "_"] &;
slotfiller[url_] := StringTemplate[StringReplace[url, "slot" -> "``"]]
urlModifier02[url_] := URLBuild@URLParse@slotfiller[url][fixspace@#] &;
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f203198%2fhow-to-define-a-functional-that-produces-at-runtime-a-function-by-evaluating-sel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Summary
The root cause of the surprising behaviour is that Function
does not recognize Slot[]
when it appears within an association object (as opposed to an association constructor). To fix it, we must either "unwrap" the association object returned by URLParse
or use a different approach altogether.
The Problem: Association Objects vs. Association Constructors
We can demonstrate the problem with a simpler example:
f = With[ "a" -> #1 , assoc &]
(* <|"a" -> #1|> & *)
The function definition looks okay. But it will not work:
f["hello world!"]
(* <|"a" -> #1|> *)
We can see the structural difference between an association constructor and an association object using TreeForm
:
Function[<| "a" -> # |>] // TreeForm (* constructor *)
Function[Evaluate[<| "a" -> # |>]] // TreeForm (* object *)
More discussion on this distinction can be found in (148095). See the section labelled Ambiguity of Association.
Fixing the Original Definition
We can change the original definition to unwrap the association object during the initial evaluation and to recreate it upon use. We do that using Normal@URLParse[...]
and Inactive[URLBuild@*Association]
. The updated definition looks like this:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild@*Association][
Normal@URLParse[url]/."slot"->Inactive[StringReplace][Slot[1]," "->"_"]
]
],Inactive[x_]:>x];
... so then:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
(* "https://www.somewebsite.com/path/hello_world%21" *)
Alternative Definitions
There are simpler ways to express this operation.
For example, we could define the urlModifier
operator like this:
ClearAll[urlModifier]
urlModifier[url_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[#, " "->"_"]] &
We could also define it explicitly as a "curried form":
ClearAll[urlModifier]
urlModifier[url_][s_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[s," "->"_"]]
The Function
form offers an advantage over the curried form in that, if desired, we could perform the initial URL-parsing at the moment of creation instead of every time the generated function is used:
ClearAll[urlModifier]
urlModifier[url_] :=
With[parsedUrl = URLParse[url]
, URLBuild[parsedUrl /. "slot" -> StringReplace[#, " "->"_"]] &
]
All of these definitions give the same results as the corrected original.
$endgroup$
add a comment |
$begingroup$
Summary
The root cause of the surprising behaviour is that Function
does not recognize Slot[]
when it appears within an association object (as opposed to an association constructor). To fix it, we must either "unwrap" the association object returned by URLParse
or use a different approach altogether.
The Problem: Association Objects vs. Association Constructors
We can demonstrate the problem with a simpler example:
f = With[ "a" -> #1 , assoc &]
(* <|"a" -> #1|> & *)
The function definition looks okay. But it will not work:
f["hello world!"]
(* <|"a" -> #1|> *)
We can see the structural difference between an association constructor and an association object using TreeForm
:
Function[<| "a" -> # |>] // TreeForm (* constructor *)
Function[Evaluate[<| "a" -> # |>]] // TreeForm (* object *)
More discussion on this distinction can be found in (148095). See the section labelled Ambiguity of Association.
Fixing the Original Definition
We can change the original definition to unwrap the association object during the initial evaluation and to recreate it upon use. We do that using Normal@URLParse[...]
and Inactive[URLBuild@*Association]
. The updated definition looks like this:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild@*Association][
Normal@URLParse[url]/."slot"->Inactive[StringReplace][Slot[1]," "->"_"]
]
],Inactive[x_]:>x];
... so then:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
(* "https://www.somewebsite.com/path/hello_world%21" *)
Alternative Definitions
There are simpler ways to express this operation.
For example, we could define the urlModifier
operator like this:
ClearAll[urlModifier]
urlModifier[url_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[#, " "->"_"]] &
We could also define it explicitly as a "curried form":
ClearAll[urlModifier]
urlModifier[url_][s_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[s," "->"_"]]
The Function
form offers an advantage over the curried form in that, if desired, we could perform the initial URL-parsing at the moment of creation instead of every time the generated function is used:
ClearAll[urlModifier]
urlModifier[url_] :=
With[parsedUrl = URLParse[url]
, URLBuild[parsedUrl /. "slot" -> StringReplace[#, " "->"_"]] &
]
All of these definitions give the same results as the corrected original.
$endgroup$
add a comment |
$begingroup$
Summary
The root cause of the surprising behaviour is that Function
does not recognize Slot[]
when it appears within an association object (as opposed to an association constructor). To fix it, we must either "unwrap" the association object returned by URLParse
or use a different approach altogether.
The Problem: Association Objects vs. Association Constructors
We can demonstrate the problem with a simpler example:
f = With[ "a" -> #1 , assoc &]
(* <|"a" -> #1|> & *)
The function definition looks okay. But it will not work:
f["hello world!"]
(* <|"a" -> #1|> *)
We can see the structural difference between an association constructor and an association object using TreeForm
:
Function[<| "a" -> # |>] // TreeForm (* constructor *)
Function[Evaluate[<| "a" -> # |>]] // TreeForm (* object *)
More discussion on this distinction can be found in (148095). See the section labelled Ambiguity of Association.
Fixing the Original Definition
We can change the original definition to unwrap the association object during the initial evaluation and to recreate it upon use. We do that using Normal@URLParse[...]
and Inactive[URLBuild@*Association]
. The updated definition looks like this:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild@*Association][
Normal@URLParse[url]/."slot"->Inactive[StringReplace][Slot[1]," "->"_"]
]
],Inactive[x_]:>x];
... so then:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
(* "https://www.somewebsite.com/path/hello_world%21" *)
Alternative Definitions
There are simpler ways to express this operation.
For example, we could define the urlModifier
operator like this:
ClearAll[urlModifier]
urlModifier[url_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[#, " "->"_"]] &
We could also define it explicitly as a "curried form":
ClearAll[urlModifier]
urlModifier[url_][s_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[s," "->"_"]]
The Function
form offers an advantage over the curried form in that, if desired, we could perform the initial URL-parsing at the moment of creation instead of every time the generated function is used:
ClearAll[urlModifier]
urlModifier[url_] :=
With[parsedUrl = URLParse[url]
, URLBuild[parsedUrl /. "slot" -> StringReplace[#, " "->"_"]] &
]
All of these definitions give the same results as the corrected original.
$endgroup$
Summary
The root cause of the surprising behaviour is that Function
does not recognize Slot[]
when it appears within an association object (as opposed to an association constructor). To fix it, we must either "unwrap" the association object returned by URLParse
or use a different approach altogether.
The Problem: Association Objects vs. Association Constructors
We can demonstrate the problem with a simpler example:
f = With[ "a" -> #1 , assoc &]
(* <|"a" -> #1|> & *)
The function definition looks okay. But it will not work:
f["hello world!"]
(* <|"a" -> #1|> *)
We can see the structural difference between an association constructor and an association object using TreeForm
:
Function[<| "a" -> # |>] // TreeForm (* constructor *)
Function[Evaluate[<| "a" -> # |>]] // TreeForm (* object *)
More discussion on this distinction can be found in (148095). See the section labelled Ambiguity of Association.
Fixing the Original Definition
We can change the original definition to unwrap the association object during the initial evaluation and to recreate it upon use. We do that using Normal@URLParse[...]
and Inactive[URLBuild@*Association]
. The updated definition looks like this:
ClearAll[urlModifier];
urlModifier[url_]:=ReplaceAll[Function@Evaluate[
Inactive[URLBuild@*Association][
Normal@URLParse[url]/."slot"->Inactive[StringReplace][Slot[1]," "->"_"]
]
],Inactive[x_]:>x];
... so then:
urlModifier["https://www.somewebsite.com/path/slot"]["hello world!"]
(* "https://www.somewebsite.com/path/hello_world%21" *)
Alternative Definitions
There are simpler ways to express this operation.
For example, we could define the urlModifier
operator like this:
ClearAll[urlModifier]
urlModifier[url_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[#, " "->"_"]] &
We could also define it explicitly as a "curried form":
ClearAll[urlModifier]
urlModifier[url_][s_] :=
URLBuild[URLParse[url] /. "slot" -> StringReplace[s," "->"_"]]
The Function
form offers an advantage over the curried form in that, if desired, we could perform the initial URL-parsing at the moment of creation instead of every time the generated function is used:
ClearAll[urlModifier]
urlModifier[url_] :=
With[parsedUrl = URLParse[url]
, URLBuild[parsedUrl /. "slot" -> StringReplace[#, " "->"_"]] &
]
All of these definitions give the same results as the corrected original.
edited 5 hours ago
answered 5 hours ago


WReachWReach
54.7k2 gold badges118 silver badges217 bronze badges
54.7k2 gold badges118 silver badges217 bronze badges
add a comment |
add a comment |
$begingroup$
fixspace = StringReplace[#, " " -> "_"] &;
slotfiller[url_] := StringTemplate[StringReplace[url, "slot" -> "``"]]
urlModifier02[url_] := URLBuild@URLParse@slotfiller[url][fixspace@#] &;
$endgroup$
add a comment |
$begingroup$
fixspace = StringReplace[#, " " -> "_"] &;
slotfiller[url_] := StringTemplate[StringReplace[url, "slot" -> "``"]]
urlModifier02[url_] := URLBuild@URLParse@slotfiller[url][fixspace@#] &;
$endgroup$
add a comment |
$begingroup$
fixspace = StringReplace[#, " " -> "_"] &;
slotfiller[url_] := StringTemplate[StringReplace[url, "slot" -> "``"]]
urlModifier02[url_] := URLBuild@URLParse@slotfiller[url][fixspace@#] &;
$endgroup$
fixspace = StringReplace[#, " " -> "_"] &;
slotfiller[url_] := StringTemplate[StringReplace[url, "slot" -> "``"]]
urlModifier02[url_] := URLBuild@URLParse@slotfiller[url][fixspace@#] &;
answered 7 hours ago
AlanAlan
6,85211 silver badges26 bronze badges
6,85211 silver badges26 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f203198%2fhow-to-define-a-functional-that-produces-at-runtime-a-function-by-evaluating-sel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown