Method Chaining, Postfix Operator, and OOP
x.f.g. its extremely convenient, widely loved, in ruby too. ♥ ♥ ♥
- however, there's a troublesome aspect, namely, object and properties.
- method chaining relies on object properties to work, which is a semantic issue.
- in most languages there's no postfix operator.
- To chain functions, f must return a object and that object must have next f as property.
- then, you use the dot notation for property access, to achieve method chaining.
- the problem with this is that we rely on a semantic property for its side effect of syntactic convenience.
x.f(a).g(b)is written as
x[f](a)[g](b). Would you still want to chain?
- to be able to chain arbitrary functions, you need a postfix operator. unix pipe x|f|g, Wolfram Language x//f//g
Note: a related concept is function composition. It has somewhat the same purpose of postfix operator with respect to syntax convenience. Because, it avoids nesting, and lets you write sequential application of functions in a sequential manner. [see What's Function, What's Operator?]
In lisp language that doesn't use operators, function composition is the natural solution.
For example, in Clojure, it's called “threading”.
(-> x f1 f2 f3)
is equivalent to
(f3 (f2 (f1 x))).
[see Clojure: Function Chaining]
[see JS: Pipe Function Instead of Method Chaining]