Guest User

Untitled

a guest
Oct 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. partialFunction[func_, params__] :=
  2. Function[Null, func[params, ##], HoldAllComplete]
  3.  
  4. partialFunction[func_Symbol, params__] :=
  5.  
  6. Module[{func, cached = func},
  7. Options[func] = Options[cached];
  8. func[args___] :=
  9. cached[params, args];
  10. Format[func] =
  11. RawBoxes@
  12. BoxForm`ArrangeSummaryBox[
  13. "partialFunction",
  14. func,
  15. None,
  16. {
  17. cached,
  18. BoxForm`MakeSummaryItem[{"Args: ", Length@{params}},
  19. StandardForm]
  20. },
  21. {},
  22. StandardForm
  23. ];
  24. func~SetAttributes~HoldAllComplete;
  25. func
  26. ];
  27. partialFunction[func_, params__] :=
  28.  
  29. Function[Null, func[params, ##], HoldAllComplete]
  30.  
  31. m1 = Module[{m1 = MemoryInUse[]},
  32. partialFunction[Print, RandomReal[{}, {1000, 1000}]];
  33. m1
  34. ];
  35. MemoryInUse[] - m1
  36.  
  37. 8006848
  38.  
  39. ClearAll[pFun, initpFun];
  40.  
  41. SetAttributes[pFunHold, HoldAllComplete];
  42. SetAttributes[initpFun, HoldAllComplete];
  43.  
  44. initpFun[Fun_, Parameters__] :=
  45. pFun[<|"Function" -> Fun, "Parameters" -> pFunHold[Parameters]|>];
  46.  
  47. pFun /: F_pFun[args___] := With[{f = F[[1]]["Function"]},
  48. ReleaseHold[
  49. Hold[f["Parameters", args]] /.
  50. "Parameters" -> F[[1]]["Parameters"] /. pFunHold[x_] :> x]
  51. ];
  52.  
  53. pFun /: Format[
  54. pFun[a_Association]] := "Insert the box expression of your choice
  55. here."
  56.  
  57. pf = initpFun[Print, RandomReal[{}, {10, 10} 100]]
  58.  
  59. pf[]
Add Comment
Please, Sign In to add comment