Piping and Function Composition Equivalence
Had a divine revelation, that is, how pipe operator is equivalent to function composition.
Pipe operator, e.g. in unix
| or as dot operator in ruby etc, is very useful.
In general, operators can be considered as a function. (in many function languages, operators can be used with function call syntax.)
But there's also function composition, i.e.
pipe(x,f,g) == compose(f,g)(x)
function compose is also quite useful. But, pipe and compose seems very similar. I wish not to have to write both, because it seems redundant.
(Note, between pipe and function composition, pipe is far more useful.)
So, the problem is, is there a way to consider pipe and function composition as equivalent , so that you can implement just one function in a programing language library, and it can be practically use for both piping and compose?
Note: pipe, is mathematically the same as nested function call.
Thought for 1 hour, and found an answer that's mathematically sane!
the math sane answer is, just write a function composition function.
Allow each argument to be a non-function e.g. 3.
When a non-function argument is encountered,
treat it as constant
function, i.e. 3 is a function that always returns 3. This way,
is same as
x is a non-function value such as 3,
compose(g,f,x) also returns a
Note, this works fine in languages that's not strictly typed.
compose(g,f,x) return a function or a value, depending on whether
x is a function or a value.
But in strictly typed languages, you may not have a return value whose type depends on input parameters. But such languages have ways to resolve that. e.g. a type defined to be or.
#haskell #ocaml #clojure
treating data (such as number) as (constant) function is a powerful idea. Because it lets you treat function and data in a uniform way.
Unix Pipe, Dot Notation, Postfix Notation
If you have a question, put $5 at patreon and message me.