Opened 7 years ago

Last modified 7 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 Tade0)

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

  1. Functions without implicit potential side-effects(this) are easier to reason about.
  2. 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:

  1. https://docs.python.org/2/library/functions.html
  2. http://ramdajs.com/docs/

BTW: I volunteer to take care of this.

Change History (1)

comment:1 Changed 7 years ago by Tade0

Description: modified (diff)
Note: See TracTickets for help on using tickets.
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy