#16754 closed New Feature (wontfix)
Consider changes in CKEDITOR.tools.array
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.
Change History (3)
comment:1 Changed 8 years ago by
Description: | modified (diff) |
---|
comment:2 Changed 8 years ago by
Status: | new → confirmed |
---|
comment:3 Changed 8 years ago by
Resolution: | → wontfix |
---|---|
Status: | confirmed → closed |
As noted, we need standard Array API polyfills - as such developers are expecting those to match standard JS Array API.
I can see how there's an easy possibility to provide partial application-friendly APIs, simply by writing a plugin with such API, that would expose it as a dedicated namespace.