Message Boards Message Boards

4 Replies
0 Total Likes
View groups...
Share this post:

Composition, RightComposition, design decisions

Posted 9 years ago

Dear Community,

Why do we need both Composition and RightComposition? Why couldn't both f @* g and g /* f be a shorthand for the same expression, Composition[f, g]? What is the advantage of the current design over just having a single composition operator with different notations, similar to how f@x, f[x] and x//f are different notations for the same?

I'm sure there's something I'm missing. There's no practical motivation for this question, just curiosity.

POSTED BY: Szabolcs Horvát
4 Replies

I got an email saying that Erik Mahieu has responded to this thread, but I cannot see his response when I open the website. Can anyone else see it? EDIT:: Strange, after several re-loads it didn't appear. But as soon as I posted this, his response appeared as well.

POSTED BY: Szabolcs Horvát

Heisenberg at work (or play, maybe).

POSTED BY: Daniel Lichtblau
Posted 9 years ago

Indeed, 4 ways to get the same result. But what's wrong with that? Is Mathematica not famous for having several ways to achieve the same end result? But with thousands of functions and more coming, it gets really crowded. especially in our limited human memory...

In[16]:= Composition[f, g, h][x]
RightComposition[h, g, f][x]
h /* g /* f@x

Out[16]= f[g[h[x]]]

Out[17]= f[g[h[x]]]

Out[18]= f[g[h[x]]]

Out[19]= f[g[h[x]]]

The same for RightComposition:

In[27]:= RightComposition[f, g, h][x]
f /* g /* h@x
Composition[h, g, f][x]

Out[27]= h[g[f[x]]]

Out[28]= h[g[f[x]]]

Out[29]= h[g[f[x]]]

Out[30]= h[g[f[x]]]
POSTED BY: Erik Mahieu

Indeed, 4 ways to get the same result.

Actually, two ways. I meant that there are two truly distinct expressions which seem to have the same functionality, not that there are several notations for it.

It is clear to me what it is advantageous to have both the @* and /* notations. What is not clear to me is whether there's a good reason have these notations correspond to distinct expressions.

But what's wrong with that? Is Mathematica not famous for having several ways to achieve the same end result?

There's nothing wrong with it. Rather, I'm curious if there's a compelling reason to distinguish between them. I suspected that this was a carefully considered decision and that there are some interesting reasons that are not immediately obvious.

If we were to look for counter-reasons: it increases complexity when one needs to handle Composition in a special way. On example: while normally Composition and RightComposition behave the same way except for the ordering, this is not the case within Dataset. op1 /* op2 won't have the same effect as op2 @* op1. Perhaps this is a bug (is it?) arising from the fact the RightComposition requires special handling within Dataset and this special handling wasn't implemented for Composition ... ?

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract