6
$\begingroup$

The documentation says that using Infix ~, the following input

f[e1,e2,e3,...]

can be rewritten as

e1~f~e2~f~e3~...

So, for the sake of example, consider the following list defined below.

samplelist = {1,2,3,4,5,6,7,8,9}

If I wanted to Partition this list into subsets of 3 with an offset of 2, I would do so by entering

Partition[samplelist, 3, 2]

which would output

{{1, 2, 3}, {3, 4, 5}, {5, 6, 7}, {7, 8, 9}}

Logic then follows that if I wanted to achieve the same result using Infix, I should input

samplelist~Partition~3~Partition~2

But, this instead returns

{{{1, 2, 3}, {4, 5, 6}}}

What am I doing wrong here? How can I use Infix correctly to achieve the desired result?

$\endgroup$
  • 6
    $\begingroup$ The documentation is slightly misleading, but it doesn't actually say that f[a,b,c] can be given as a~f~b~f~c, if you read carefully. It says that Infix will print it in that form. a~f~b~f~c is in fact f[f[a, b], c]. Only use ~infix~ notation for functions that take two arguments. IMO it's quite confusing to use such a notation when the function is not associative anyway ... $\endgroup$ – Szabolcs Jun 29 '15 at 10:08
  • $\begingroup$ Ah, that's what I started to suspect. After playing around with it, I thought that stringing multiple infixes along made it like a pseudo nested function. Thanks for clarifying the info :) $\endgroup$ – A is for Ambition Jun 29 '15 at 10:10
  • $\begingroup$ Related: (6955), (23818), (39538) $\endgroup$ – Mr.Wizard Jun 29 '15 at 10:55
  • 1
    $\begingroup$ @Szabolcs How is it any more confusing for a non-associative function than e.g. x // f // g // h is? Either form requires one to know in which direction a particular operator must be read. Or do you prefer explicit bracketing in all such cases? $\endgroup$ – Mr.Wizard Jun 29 '15 at 11:11
9
$\begingroup$

The documentation you linked to is more about how Infix is used to define output formats. You can find more information on how to use functions in infix form in the tutorials Special Ways to Input Expressions and Operator Input Forms.
If you really want to use Partition in the infix form with an offset, you can use

samplelist ~Partition~ Sequence[3, 2]

{{1, 2, 3}, {3, 4, 5}, {5, 6, 7}, {7, 8, 9}}

$\endgroup$
7
$\begingroup$

As commented by Szabolcs ~infix~ syntax works only with two arguments. Karsten's clever use of Sequence does not change this fact.

As a leading proponent of ~infix~ syntax around here I believe that that binary nature is one of two primary benefits this notation has, the other being reduction of stacked [[[ brackets ]]]. One can at a glance tell from ~function~ that function is being used with two arguments; this imparts information that it not present in function[ where one must keep scanning to determine the number of arguments. Similarly function @ and // function can be seen at a glance to have one argument.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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