4 ms·
I agree. They are quite incomprehensible. I looked at the Ruby version https://github.com/cognitect-labs/transducers-ruby https://github.com/cognitect-labs/tran
by pmontra 12y ago
I agree. They are quite incomprehensible. I looked at the Ruby version https://github.com/cognitect-labs/transducers-ruby https://github.com/cognitect-labs/transducers-ruby because I'm better at Ruby than at JS and Ruby is usually more readable than JS. No way to understand it. I had to dig into the documentation and learn how it works instead of figuring it out from the way it looks.
The less efficient equivalent is
(1..100).select {|n| n.even?}.map {|n| n * 2}.take(5) which is self evident to anybody with a couple of days of Ruby on his back.
I think tranducers are a good idea with still the wrong syntax.
Probably the Ruby way to transducers would be along these lines.
(1..100).transduce.select {|n| n.even?}.map {|n| n * 2}.take(5).run
like we build SQL queries with AREL. Maybe this will never work but a possible implementation is adding transduce to a bunch of classes (in this case: Range), having transduce return an object (a Transduceable?) augmented with transducing equivalents of some methods, letting those methods pick sensible defaults for the composition operation and initial value based on the type of the object on the left. Maybe adding transduce to Enumerable is enough to get all the most important classes covered. Probably we need a further method call to actually run the transducing operation we built, that's why the final .run
This is probably not as general as the transducer ruby gem even if we enhance it by passing the reducer and the initial value as arguments as in transduce(reducer: :push, initial_value: []) but it should cover most of the use cases and anybody would be able to read it. This makes the difference when having to urgently fix bugs in the code somebody else wrote two years before and then left the company.