A Proposal to Improve the Usefulness of Routine {CHAIN}.append

by Finnian Reilly (modified: 2018 May 22)

The Problem

Who has ever been frustrated by the fact that you cannot use the routine {CHAIN}.append to append arrays to a container conforming to CHAIN? This is because the argument to append must conform to class SEQUENCE, but class ARRAY does not. Arrays are a pretty fundamental container in any programming language, so the fact that you cannot append them to containers using this routine is in my view, a major oversight. Luckily the remedy is simple.

The Solution

Here is my proposed solution to the problem that should allow the routine to maintain backwards compatibility. Change the current definition of append to the following:

class CHAIN feature -- Element change append (items: ITERABLE [G]) do across items as it loop extend (it.item) end end

For better memory management and performance append could be redefined in DYNAMIC_CHAIN as follows. However it does require the addition of a new deferred routine: accommodate. Considering that DYNAMIC_CHAIN is unbounded it makes sense to include this routine. In the arrayed list implementation this can be renamed to resize. This should negate the need to make many re-definitions of append in various containers.

class DYNAMIC_CHAIN inherit CHAIN [G] export {ANY} remove, prune_all, prune undefine remove, prune_all, prune redefine append end UNBOUNDED [G] feature -- Resizing accommodate (new_count: INTEGER) deferred end feature -- Element change append (items: ITERABLE [G]) do if attached {FINITE [G]} items as finite then accommodate (count + finite.count) end Precursor (items) end

Available Now

This improved implementation of append is already available in class EL_CHAIN from Eiffel-Loop. The less useful implementation has been renamed to append_sequence.