1
$\begingroup$

Learning the wolfram Language, in section 32

"The Wolfram Language" // Characters[#]& /. Alternatives @@Characters["aeiou"] -> Nothing

Returns {T,h,e, ,W,o,l,f,r,a,m, ,L,a,n,g,u,a,g,e}

I expected it to return {T,h, ,W,l,f,r,m, ,L,n,g,g}

Changing it to

Characters["The Wolfram Language"] /. Alternatives @@Characters["aeiou"] -> Nothing

Returns the expected result, why?

$\endgroup$
2
  • 3
    $\begingroup$ Be careful with afterthought (//) notation. In this case, everything after // is grouped together. You can see this by repeatedly clicking Characters and see how the selection expands. You'd need to write ("The Wolfram Language" // Characters[#]&) /. ... to make this work. $\endgroup$ Aug 25, 2020 at 20:23
  • 1
    $\begingroup$ Related: (30425) $\endgroup$
    – Natas
    Aug 25, 2020 at 20:25

1 Answer 1

2
$\begingroup$

Characters[#] & /. Alternatives @@ Characters["aeiou"] -> Nothing is processed together. You can see this by quadruple-clicking on Characters[#] -- the extended highlighting shows the operator associations and order of processing.

Processing the operations together is equivalent to constructing f, as here:

f = Characters[#] & /. Alternatives @@ Characters["aeiou"] -> Nothing;

"The Wolfram Language" // f

{T,h,e, ,W,o,l,f,r,a,m, ,L,a,n,g,u,a,g,e}

As well as the method you show, the operator binding can be fixed like this:

ReplaceAll["The Wolfram Language" // Characters[#] &, 
 Alternatives @@ Characters["aeiou"] -> Nothing]

{T,h, ,W,l,f,r,m, ,L,n,g,g}

$\endgroup$
4
  • $\begingroup$ I'm on wolfram cloud, does quadruple clicking work there? It seems not to. Also, is there a way to modify execution order with parentheses, etc? $\endgroup$ Aug 25, 2020 at 20:28
  • $\begingroup$ Ah, this does work: ("The Wolfram Language"//Characters[#]&)/.Alternatives@@Characters["aeiou"]->Nothing Gotcha, that's helpful $\endgroup$ Aug 25, 2020 at 20:30
  • $\begingroup$ So, essentially, I should think about // not as piping syntax (as I'm used to from shell scripts) but rather as a convenient lambda creator. It takes everything prior to the // and treats it as arguments and everything after // and treats it as a function $\endgroup$ Aug 25, 2020 at 20:34
  • $\begingroup$ You may find this informative: Special Ways to Input Expressions : i.e. "You should notice that // has very low precedence. " $\endgroup$ Aug 25, 2020 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.