<p class="wp-block-paragraph">I need some moment to fully understand this:</p>
<pre class="wp-block-preformatted">const compose = ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );
</pre>
<p class="wp-block-paragraph">reduce method is trickly: when initialValue is not given, the first element of the array is taken as first argument of reduce’s callback parameter, so f is the first argument, g is the second, (…args) is the formal parameters (passed to the resulting function), at the end of the reduction all …fns are applyied in reverse order (i.e. compose(f,g,h) g is applyed before f, and h is applyed before g, resulting in (…args) => f(g(h(…args)) ).</p>
<p class="wp-block-paragraph">But once I got it it happen to be really an elegant way to write it:</p>
<pre class="wp-block-preformatted">const $d = $data.mergeMap( compose(Rx.Observable.from, r=>r.split('\n')) );
</pre>
<p class="wp-block-paragraph">and not:</p>
<pre class="wp-block-preformatted">const $d = $data.mergeMap(r=>Rx.Observable.from(r.split('\n')));
</pre>
<p class="wp-block-paragraph">that is less readable</p>
<p class="wp-block-paragraph">Most of this is related to the propose of epic pattern on redux-observable: <a href="https://github.com/danielecr/react-redux-strap" target="_blank" rel="noopener" title="">https://github.com/danielecr/react-redux-strap</a></p>
<p class="wp-block-paragraph"></p>
I need some moment to fully understand this:
const compose = ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );
reduce method is trickly: when initialValue is not given, the first element of the array is taken as first argument of reduce’s callback parameter, so f is the first argument, g is the second, (…args) is the formal parameters (passed to the resulting function), at the end of the reduction all …fns are applyied in reverse order (i.e. compose(f,g,h) g is applyed before f, and h is applyed before g, resulting in (…args) => f(g(h(…args)) ).
But once I got it it happen to be really an elegant way to write it:
const $d = $data.mergeMap( compose(Rx.Observable.from, r=>r.split('\n')) );
and not:
const $d = $data.mergeMap(r=>Rx.Observable.from(r.split('\n')));
that is less readable
Most of this is related to the propose of epic pattern on redux-observable: https://github.com/danielecr/react-redux-strap