|
|
|
|
|
by binrec
2622 days ago
|
|
It's technically possible to write in a concatenative style in a non-concatenative language, but in most cases, it's nowhere near idiomatic. How would you define a function that squares a number in a concatenative style in Javascript? Here's the best I can come up with: function compose(...funcs) {
return stack => funcs.reduce((acc, cur) => cur(acc), stack)
}
function dup(stack) {
stack.push(...[,,].fill(stack.pop()))
return stack;
}
function mul(stack) {
stack.push(stack.pop() * stack.pop())
return stack;
}
const square = () => compose(dup, mul)
console.log(square()([4]))
Then again, this could be a trick question someday - if the pipeline proposal goes through, it'll be possible to write idiomatic concatenative Javascript. (If you want to play with the pipeline operator, see here: https://defseg.io/etc/pipeline.html) You'd still need to define all your functions as accepting a stack parameter, but you wouldn't need a compose function, so you could do something like this: function dup(stack) {
stack.push(...[,,].fill(stack.pop()))
return stack;
}
function mul(stack) {
stack.push(stack.pop() * stack.pop())
return stack;
}
const square = stack => stack |> dup |> mul
console.log([4] |> square)
console.log([2] |> square |> square)
Now to write an ECMA proposal for recursive combinators... /s |
|
You are using a stack which is not immutable.
A lot of javascript programmers think they are doing functional programming but they don't get the essence of it. Yes you know map, yes you know reduce. But you don't get it.
functional programming is in essence a mathematical function rather than a procedural function. If you can fit your entire program into a single expression or a single function without computational steps or procedures you have achieved functional programming.
Immutability is a side effect of converting everything into a single mathematical expression. Concatenative programming and functional programming are the same thing as imperative programming BUT with more restrictions so of course you can do concatenative programming in javascript AND you can be idiomatic.
You are overcomplicating the concept. Literally:
is still concatenative.all concatenative programming is, is using the compose operator to compose your functions into a single function.
for the given:
if your goal is to compute (2x)^2procedural is this:
functional (without concatenative): Above, the functions are applied on defined immutable variables. Order doesn't matter as long as you keep all definitions the same. While order happens sort of as a consequence to this, the concept for functional programming is to get rid of procedures.concatenative (still a valid functional program):
In the above, a function application is only used here as the last step. The IO step where functional AND concatenative programming breaks down, but literally for concatenative programming your entire programming should be all made up of function definitions and composed functions and no declared variables and no applied functions. Just apply one function in the last step for IO and your program is concatenative.Javascript has been giving bootcampers completely wrong ideas about functional programming and what it is. Learn a real functional programming lang to actually get it. Haskell is a good one.
Also your examples literally defeat the purpose of functional and concatenative programming. It makes the code worse. I would literally not think concatenative programming or functional programming is better if I saw that code for the first time. I would avoid functional programming like the plague if it caused your code to blow up into a mess like that. I'm not here to stretch my brain just to square a number.