6

For example, I have the following

hexToRGB = RGBColor @@ (IntegerDigits[#~StringDrop~1~FromDigits~16, 256, 3]/255.) &;

Image@hexToRGB["#99c361"] // DominantColors["HexRGBColor"] (* why not? *)

To be clear, I am well aware that I can write

DominantColors[#, 1, "HexRGBColor"] &@Image@hexToRGB["#99c361"]

But there doesn't seem to be a postfix form for pure functions, or is there?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
skyfire
  • 477
  • 2
  • 7

2 Answers2

7

Because currying is not performed automatically. But you can do the following:

Image@hexToRGB["#99c361"] // DominantColors[#, 1, "HexRGBColor"] & 
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • Awesome thanks, is there a way to combine this w/ the prefix form e.g. First[#]&@Image@hexToRGB["#99c361"] // DominantColors[#, 1, "HexRGBColor"] & ? – skyfire Apr 02 '20 at 13:13
  • ah nvm, just need to add parentheses – skyfire Apr 02 '20 at 13:16
  • 1
    @skyfire Syntax precedence determines the order @ and // are applied. My guess is that you want Image@hexToRGB["#99c361"] // First@ DominantColors[#, 1, "HexRGBColor"] & – Michael E2 Apr 02 '20 at 13:16
  • @MichaelE2 Yes, that is better than mine. Sort of confused on how mathematica parses its expressions now though. The 3 methods in this answer make it unclear how the expression gets evaluated. – skyfire Apr 02 '20 at 13:18
  • 4
    @skyfire The precedence of // is very low, lower than @; that of & is even lower. – Michael E2 Apr 02 '20 at 13:22
  • @MichaelE2 Ah I understand now. Is there a reference on operator precedence's? – skyfire Apr 02 '20 at 13:25
  • 3
    The link in my comment is to an answer discussing precedence, which also includes a link to a tutorial in the docs. – Michael E2 Apr 02 '20 at 13:47
5

You can define an operator form for DominantColors by modifying its definition:

Unprotect[DominantColors]
DominantColors[s_String] := DominantColors[#, 1, s] &; 
Protect[DominantColors];

A better/safer alternative is to define your own function with its operator form that works like the function DominantColors:

ClearAll[dominantColors]
dominantColors[a__, o : OptionsPattern[DominantColors]] := DominantColors[a, o]
dominantColors[s_String] := dominantColors[#, 1, s] &

"#99c361" // hexToRGB // Image // dominantColors["HexRGBColor"] // First
"#99c361"
First @ dominantColors["HexRGBColor"] @ Image @ hexToRGB @ "#99c361"
 "#99c361"
kglr
  • 394,356
  • 18
  • 477
  • 896