3 ms·
What are the OP's conclusion from this experiment ? I was expecting the FP languages solutions to be exceptionally short and elegant, but after a quick glance
by fermigier 6y ago
What are the OP's conclusion from this experiment ?
I was expecting the FP languages solutions to be exceptionally short and elegant, but after a quick glance it seems to me that the Python and Python-like languages (e.g. Nim, Zig, etc.) are "winning" (YMMV of course).
- OskarS 6y agoThe lesson for me is that this particular problem is exceptionally well suited for imperative code, and makes functional code look terrible in comparison. It's not just the "Python-likes" that do well: both the C and C++ code (C++ in particular) are very straightforward, simple and very readable, but any functional language solution that uses reduce or whatever looks terrible in comparison. The striking example is the JavaScript one: the simple imperative solution (for_of.js) is perfectly fine and lovely, but the other ones (map_with_state.js in particular) are total nightmares. Most of them are (almost certainly) significantly less performant than the simple imperative ones. Not all problems are like this, of course, there are many examples where functional style code shines. But certainly there are many problems like this one, where imperative code is just the superior paradigm.
- Scarbutt 6y agoThe lesson for me is that this particular problem is exceptionally well suited for imperative code, and makes functional code look terrible in comparison. This is the case for 80% of real world code in IME.
- higerordermap 6y agoAs long as there aren't too many variables keeping state, imperative should be fine.
- zelphirkalt 6y agoI think this is perhaps only a repository for keeping things or approaches, which has surfaced here, because of the fame of the author. The results and code might be rather individual preferences or ad-hoc solutions to typical problems. The data in the example does not require a lot of recursion to be elegantly processed. It is too simple an example for that. The second aspect is, that the example code might have different priorities than the viewer. If I want to process a tree concurrently on multiple cores, then perhaps I should not be using much mutable state. However, if my priority is not concurrency and multi-core, then perhaps I can write it in very little code in Python and similar languages. When my priorities shift to concurrency and multi-core and lets say I got a deeply nested tree, I might have to adapt all these examples and suddenly the FP languages shine.
- _old_dude_ 6y agoI'm not sure the problem reflect anything, while updating a data structure like this is fairly standard, the lesson counter is reset globally, which is not a common operation. For me the problem is too artificial to be meaningful.
- nickjj 6y ago> For me the problem is too artificial to be meaningful. The problem came from implementing a real feature into a course platform. The data structure contains a list of sections and lessons to build a table of contents. The position stores the order they are being displayed in. In the course viewing page lessons are annotated with a "#N" where N is the position and it goes from 1 all the way until the last lesson's count. This way folks could be watching the course and be like "Hey, I'm on lesson #56 and have a question". But on the course description page the table of contents was displayed in such a way where the position / lesson count was reset for each section. Although in the real life implementation this code is wrapped into a function and the reset_lesson_position is being supplied by a boolean function argument which controls whether or not the lesson's position gets reset for every section. We added it as an attribute to the section directly for the sake of this example to keep the problem more scoped to the core idea of the problem.
- _old_dude_ 6y agoIt makes a lot of more sense. `resetLessonPosition` is not a member of the data structure but a parameter of the function that generates the positions and it has an effect on all sections.
- lukashrb 6y agoThe author is aware of the fact that mutability makes this kind of problem easier [0] [0] https://twitter.com/josevalim/status/1379771275627921409?s=19 https://twitter.com/josevalim/status/1379771275627921409?s=1...
- yakubin 6y agoThe Scheme example is shorter than any of the Python ones.