reddit didnt send notification about your response despite i was looking forward to it, ugh. we're both fighting the good fight whichever of us is right, and i adore the challenge. the difference seems also very close to a merely aesthetic preference, i could imagine myself attracted to the fluent interface as well, but my objections to it are actually FP-oriented. it was clear that you're not deploying a full OO arsenal, only encapsulation, and not every OO concept is necessarily evil by design. creating a set of closures with their own lexical scope is analogous to constructing an object with methods and private/public properties. ```class Flow{ constructor(x){this.x=x;} then(){} }``` is just syntactic sugar for your `x=>({x,then(){}})` as well. to create a monad/fluent interface, the methods need to return the "identity" every time, which you achieve by creating new instances/closures with the same structure, instead of returning the same object with stored and mutated local scope (this.x). To me both sound like tradeoffs, while using first-class functions in my case has the "readability" drawback that you mention, but also a benefit in not having to track an excessive, hybrid data-structure that's arguably one of the original sins of OOP. I think one way the readability drawback can be solved is declaring parts of the composition, as i mentioned (eg. `compose(either(parsename,swap("n/a")),"toUpperCase")` etc.). Regarding how the strings resolve to method calls, it's a feature i thought to include, that instead of having to spell out the boilerplate `a=>a["name"]`, property access is attempted by default in the composition, and if it's a function, it gets invoked too (`a=>a["trim"]()`). `compose(String.prototype.trim)` works just as well, but an even cooler feature is that plural input and output are also handled, so you can write `compose(2,3,sum)(1)`, and it will result in `sum(1,2,3)`. Therefore when a string is not a property of the object in context, it gets appended to the context (`compose(0,"b","c",collect,"join")(["a"]) -> "abc" `). I haven't written a nice article about my shinnanigans like you, but I would be happy to see how we can convince each other about which are better tradeoffs. I created a matrix/discord channel for such discussions if you would be interested too :) : https://www.reddit.com/r/learnjavascript/comments/16sszup/frameworkless_functional_javascript_discordmatrix/
I am happy to join your chat, not to debate, but to learn about the pros and cons of different development approaches. Regarding the "avoiding boilerplate" feature, I have also considered ways to make it more elegant. I call it placeholders, and I used Proxy to track all deep chains.
const propertyProxy = f => new Proxy(f, {
get: (_, key) => {
const g = x => f(x)[key]
return propertyProxy(g)
}
})
const methodProxy = f => new Proxy(f, {
get: (_, key) => {
const g = (...args) => x => f(x)[key](...args)
return methodProxy(g)
}
})
const P = propertyProxy(x => x)
const R = methodProxy(x => x)
I would like to combine R with P to have one placeholder to use this way: P.users.at(0).name.toUpperCase(), but I have not yet found a solution for that.
View all comments
Show parent comment
3
u/miracleranger Dec 20 '23
reddit didnt send notification about your response despite i was looking forward to it, ugh.
we're both fighting the good fight whichever of us is right, and i adore the challenge.
the difference seems also very close to a merely aesthetic preference, i could imagine myself attracted to the fluent interface as well, but my objections to it are actually FP-oriented.
it was clear that you're not deploying a full OO arsenal, only encapsulation, and not every OO concept is necessarily evil by design.
creating a set of closures with their own lexical scope is analogous to constructing an object with methods and private/public properties.
```class Flow{
constructor(x){this.x=x;}
then(){}
}```
is just syntactic sugar for your `x=>({x,then(){}})` as well.
to create a monad/fluent interface, the methods need to return the "identity" every time, which you achieve by creating new instances/closures with the same structure, instead of returning the same object with stored and mutated local scope (this.x).
To me both sound like tradeoffs, while using first-class functions in my case has the "readability" drawback that you mention, but also a benefit in not having to track an excessive, hybrid data-structure that's arguably one of the original sins of OOP.
I think one way the readability drawback can be solved is declaring parts of the composition, as i mentioned (eg. `compose(either(parsename,swap("n/a")),"toUpperCase")` etc.). Regarding how the strings resolve to method calls, it's a feature i thought to include, that instead of
having to spell out the boilerplate `a=>a["name"]`, property access is attempted by default in the composition, and if it's a function, it gets invoked too (`a=>a["trim"]()`). `compose(String.prototype.trim)` works just as well, but an even cooler feature is that plural input and output are also handled, so you can write `compose(2,3,sum)(1)`, and it will result in `sum(1,2,3)`. Therefore when a string is not a property of the object in context, it gets appended to the context (`compose(0,"b","c",collect,"join")(["a"]) -> "abc" `).
I haven't written a nice article about my shinnanigans like you, but I would be happy to see how we can convince each other about which are better tradeoffs. I created a matrix/discord channel for such discussions if you would be interested too :) : https://www.reddit.com/r/learnjavascript/comments/16sszup/frameworkless_functional_javascript_discordmatrix/