3 ms·
It's super ironic that you used the term "composability" and didn't actually use functional composition for the second example. (do_thing . do_another_thing .
by empthought 5y ago
It's super ironic that you used the term "composability" and didn't actually use functional composition for the second example.
(do_thing . do_another_thing . do_something_else) some_array
- funklute 5y agoBut this kind of functional composition often doesn't have first-class support in the language syntax. That is, it often requires you to rely on helper functions or libraries. As a result, it's not nearly as readable or intuitively obvious as the chaining example (and this is in contrast to mathematical notation, where functional composition is very clear, in my opinion).
- dragonwriter 5y ago> But this kind of functional composition often doesn’t have first-class support in the language syntax. Are we talking about Ruby? Because it does. It has both: (do_thing >> do_another_thing >> do_the_last_thing)[arg0,…,argn] And: (do_the_last_thing << do_another_thing << do_thing)[arg0,…,argn] (Where the things in the composition are procs/lambdas, the closest thing Ruby has to functions as distinct from methods.)
- funklute 5y ago> Are we talking about Ruby? I wasn't thinking specifically of Ruby (and indeed I don't know Ruby, nor was I aware that it has this kind of support). Rather, I was thinking about programming languages in general, since the top-level comment seemed to be talking about the general principle. Out of 5 languages I use on a regular basis, only one of them has _some_ support for function composition. But even then, not as flexible as what you're demonstrating here. So my point is that across programming languages, the chaining approach is generally more readable than the composition approach. And that in turn favours methods over global functions. If I ever start using Ruby, I would probably be on the fence about which approach is better though.
- wtetzner 5y agoShouldn't it be the other way around? (do_something_else . do_another_thing . do_thing) some_array
- samatman 5y agoHe did in fact, you're using points to represent (ironic!) the point-free style, prefix style and circumfix style, they're all function composition. Which, believe it or not, they still teach function composition as good old g(f(x)) in high school algebra...