# Is it possible to pipe(afterthought apply function) with options?

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?

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

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

• Awesome thanks, is there a way to combine this w/ the prefix form e.g. First[#]&@Image@hexToRGB["#99c361"] // DominantColors[#, 1, "HexRGBColor"] & ? – skyfire Apr 2 at 13:13
• ah nvm, just need to add parentheses – skyfire Apr 2 at 13:16
• @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 2 at 13:16
• @skyfire The precedence of // is very low, lower than @; that of & is even lower. – Michael E2 Apr 2 at 13:22
• 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 2 at 13:47

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"

• nice, was not aware of OptionsPattern[] – skyfire Apr 2 at 13:15
• @skyfire You can also use OptionsPattern[DominantColors] to restrict to the options of DominantColors. – Michael E2 Apr 2 at 13:20
• Thank you @MichaelE2. I added the argument to OptionsPattern. – kglr Apr 2 at 13:23