1

Is it possible to construct an argument such that any function applied to this argument returns itself, i.e. (the slot is used to allow for additional arguments in the function)

x = getfunc;
anyfunction[x]
(* Out: anyfunction[#]& *)

More specifically, I like to use an existing function in various places of the argument without changing the function. E.g. if x is a list, I want any function which is applied to x to be mapped on x without changing the function by using Map, i.e. something like

x = {getfunc@a, getfunc@b}
anyfunction[x]
(* Out: {anyfunction[#]&[a],anyfunction[#]&[b]} *)
MarcoB
  • 67,153
  • 18
  • 91
  • 189
Passi
  • 11
  • 2
  • 4
    This is a very weird request, and seems to be a very clear example of an XY problem. – AccidentalFourierTransform Jun 06 '18 at 14:43
  • While I agree with @AccidentalFourierTransform, if you really want to achieve something like this, this answer should give you a good starting point. – Lukas Lang Jun 06 '18 at 14:45
  • Thanks @Mathe172, with getfunc /: anyfunc_[getfunc[x_]] := anyfunc[#] & I can solve the problem: anyfunc[getfunc[x]] returns anyfunc[#]&. Modifying the right-hand side can then solve all other related problems like my example with Map. – Passi Jun 06 '18 at 15:05
  • "the slot is used to allow for additional arguments in the function" - either I didn't get it or it is exactly the opposite. Slot makes it a one argument function, ## would allow additional arguments, just like anyfunction alone. – Kuba Jun 06 '18 at 16:21
  • @Kuba sorry, I was a bit sloppy. What I meant was the possibility getfunc /: anyfunc_[getfunc[x_], opt___] := anyfunc[#, opt] & which is a trivial extension. – Passi Jun 06 '18 at 16:32
  • @MarcoB I found the solution to my question by use of that thread, although it seems to be a different topic. If the policy is to mark it as duplicate anyways, I can confirm it. – Passi Jun 06 '18 at 23:52
  • @Passi It is really up to you. You can accept the suggestion, which will close the question and mark it as duplicate. A few people have also agreed to the possible duplication through the review process. If a few more do, the question will also be closed as a duplicate. Closing does not reflect poorly on the question at all. It is just housekeeping: it links your question with the previous one and indicates that yours has an answer elsewhere. It therefore increases the chances that others with the same problem will find either one of the two linked questions and arrive at a solution. – MarcoB Jun 08 '18 at 04:26
  • Thanks for the explantions. – Passi Jun 08 '18 at 11:48

0 Answers0