Opened 8 years ago
Last modified 8 years ago
#16754 closed New Feature
Consider changes in CKEDITOR.tools.array — at Version 1
Reported by: | Tade0 | Owned by: | |
---|---|---|---|
Priority: | Normal | Milestone: | |
Component: | General | Version: | |
Keywords: | Cc: |
Description (last modified by )
CKEDITOR.tools.array
is an indispensable set of utilities when targeting such browsers as IE8.
There is room for improvement, though. One such improvement would be to arrange the parameters of the methods so that partial application is easier.
Proposed function signatures
filter( fn, array ) forEach( fn, array ) map( fn, array ) reduce( fn, initialValue, array ) indexOf( value, array )
Note the lack of this
argument. This can be realized by:
bind( fn, context, ...args )
which also enables partial application(functioning polyfills are available).
Why
- Functions without implicit potential side-effects(
this
) are easier to reason about. - Using partial application enables us to do composition and promotes code reusability.
Example 1: (ES2015 syntax for brevity)
function getAscendant( condition, node ) const getParentDiv = bind( getAscendant, null, node => node.name == 'div'); const getParentP = bind( getAscendant, null, node => node.name == 'p'); // later var parentDiv = getParentDiv( someNode ); // somewhere else var parentP = getParentP( someOtherNode );
Example 2: (inspired by ticket:16745)
// Rewrite this: var nameIs = function( name ) { return function( element ) { return element.name == name; }; }, isLi = nameIs( 'li' ), lis = filter( isLi, nodeList ); // list of "li" elements. // As this: var nameIs = function( name, element ) { return element.name == name }, isLi = bind( nameIs, null, 'li' ), lis = filter( isLi, nodeList ); // list of "li" elements.
These are of course rudimentary examples. One could conceivably go much further with this with currying.
Further reading
There's a large set of functions that are easy to implement and bring good value as long as composition is possible. For a list check out:
BTW: I volunteer to take care of this.