ArrayConcat Vs. ArrayMerge Vs ArrayAppend

100 views
Skip to first unread message

Peter J. Farrell

unread,
Jun 21, 2010, 2:54:54 PM6/21/10
to CFML Conventional Wisdom
This post is sparked by a tweet to this enhancement request for
ArrayMerge():

http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=83397

Now, OpenBD has ArrayConcat() already and Railo has ArrayMerge(). In
my opinion, ArrayConcat() is semantically better because ArrayMerge()
sounds like "duplicates" between the arrays are removed which is not
the behavior from what I understand.

I have passed on a message to the folks at Railo that ArrayMerge()
should really be ArrayConcat() via Todd Rafferty. Looks like Micha is
aware of the issue.

As for the Adobe CF, they can do what they want, but ArrayMerge() IMHO
is not the right name in the context of what it does.

Lastly, it has been suggested that ArrayAppend() should be overloaded
(possibly) with an option attribute to concat the two arrays. I think
this waters down what ArrayAppend() really does; which is append one
new element. It also possibly introduces a new point of failure in
which bugs can be introduced in an application.

Thoughts?

Henry Ho

unread,
Jun 21, 2010, 2:58:04 PM6/21/10
to CFML Conventional Wisdom
Looking at ListAppend at http://www.cfquickdocs.com/cf9/#listappend,
it actually Concatenates a list or element to a list. I think it's
not a bad idea to overload ArrayAppend() to concatenate an array as
well.

Thoughts?

Henry


On Jun 21, 11:54 am, "Peter J. Farrell" <pe...@mach-ii.com> wrote:
> This post is sparked by a tweet to this enhancement request for
> ArrayMerge():
>
> http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#...

Mark Jones

unread,
Jun 21, 2010, 3:09:06 PM6/21/10
to cfml-convent...@googlegroups.com
On Mon, Jun 21, 2010 at 1:58 PM, Henry Ho <henry...@gmail.com> wrote:
> Looking at ListAppend at http://www.cfquickdocs.com/cf9/#listappend,
> it actually Concatenates a list or element to a list.  I think it's
> not a bad idea to overload ArrayAppend() to concatenate an array as
> well.
>
> Thoughts?

The problem is this:

start = [1, 2];
add = [3,4];
end = ArrayAppend( start, end );

You SHOULD get [1,2, [3,4] ]. Otherwise you would not be able to add
an array as an element to another array (using ArrayAppend).

ArrayAppend should only be overloaded to act like ArrayConcat if
there's a third attribute to specify the behavior difference, at which
point you might as well have a different function name anyway. What's
better:

ArrayAppend( start, end, true );
or
ArrayConcat( start, end );

Lists are always going to be a corner case because they're not
actually their own data type. The List* functions are shorthand for
manipulating a specific format of string.

Matthew Woodward

unread,
Jun 21, 2010, 3:08:43 PM6/21/10
to cfml-convent...@googlegroups.com
On Mon, Jun 21, 2010 at 11:54 AM, Peter J. Farrell <pe...@mach-ii.com> wrote:
In
my opinion, ArrayConcat() is semantically better because ArrayMerge()
sounds like "duplicates" between the arrays are removed which is not
the behavior from what I understand.

I agree that when I think "merge" I'd kind of assume duplicates would be removed (which would be handy in some cases).

Maybe ArrayConcat() with an optional flag to remove duplicates?

I'm personally not opposed to overloading ArrayAppend() to allow the appending of arrays to existing arrays, though I understand the potential downside of this approach.

--
Matthew Woodward
ma...@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint, etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Henry Ho

unread,
Jun 21, 2010, 3:10:42 PM6/21/10
to CFML Conventional Wisdom
You're right. How could I have missed that.

Thx,
Henry


On Jun 21, 12:09 pm, Mark Jones <markjo...@visi.com> wrote:
> On Mon, Jun 21, 2010 at 1:58 PM, Henry Ho <henryho...@gmail.com> wrote:
> > Looking at ListAppend athttp://www.cfquickdocs.com/cf9/#listappend,

Peter J. Farrell

unread,
Jun 21, 2010, 3:15:44 PM6/21/10
to CFML Conventional Wisdom
I don't think it's a good idea as it's not the expected behavior to
append array2 on the end of an array with ArrayAppend(). I would
expected it to append a single element. There is a reason why the
collections framework in Java has add() and addAll() as methods. If
you wanted to go down this route, I would suggest ArrayAppendAll().

Lists are just string manipulators with some specific logic in them.
Consider this:

list1 = "alan,peter,matt"
list2 = "andy|jordan|vince"

newList = ListAppend(list1, list2)

There are two lists. The result would be:

"alan,peter,matt,andy|jordan|vince"

ListAppend doesn't convert the list delims for list2 so the output of
the element in position 4 would be "andy|jordan|vince" This allows
you to have a list within a list as long as you have unique delims.

ListAppend merely concats a string with another and prepending the
delim (if required). It doesn't care if value it's appending is 1, 2
or 3+ list items. So the logic behind ListAppend is:

if (NOT len(mylist)) {
myList = incomingElement;
} else {
my list = myList & delim & incomingElement;
}

Also, ArrayAppend() behavior would be wonky if you consider this:

ArrayAppend(myArray, someStruct, concat=true)

Would would happen? Well, you can't concat a struct to an array -- so
you just insert the struct as a normal array element. This is
confusing and inconsistent because the "concat" optional attribute
would only apply when the element to be appended is an *array*.

Matthew Woodward

unread,
Jun 21, 2010, 3:48:56 PM6/21/10
to cfml-convent...@googlegroups.com
On Mon, Jun 21, 2010 at 12:09 PM, Mark Jones <mark...@visi.com> wrote:
Otherwise you would not be able to add
an array as an element to another array (using ArrayAppend).

Yeah, good point. Probably better to use ArrayConcat since by the time you're having to add a bunch of flags to indicate behavior differences, it starts to make sense to have a separate function.

Peter Boughton

unread,
Jun 21, 2010, 6:05:06 PM6/21/10
to cfml-convent...@googlegroups.com
Why does there need to be a function for this?

For string concatenation, we do either "String1 & String2" or "String1
&= String2".

Are there any reasons not to use the same syntax for arrays?

Henry Ho

unread,
Jun 21, 2010, 6:13:29 PM6/21/10
to cfml-convent...@googlegroups.com
[1,2,3] & [4,5,6] == [1,2,3,4,5,6] or [1,2,3,[4,5,6]]?

Not very clear on what does "&" do.

Not a big fan of operator overloading.


Henry


Peter Boughton

unread,
Jun 21, 2010, 6:28:22 PM6/21/10
to cfml-convent...@googlegroups.com
Well & is concatenation, not appending, so it would do [1,2,3,4,5,6]

Hmm, though I guess there's still ambiguity with possibly doing
[1,2,3] & [4,5,6] and getting [14,25,36] instead.

Baz

unread,
Jun 21, 2010, 6:35:10 PM6/21/10
to cfml-convent...@googlegroups.com
If "123" & "321" == "123321"

Then for sure [1,2,3] & [3,2,1] == [1,2,3,3,2,1]

And for structs {one=1, two=2, three=3} & {three=4, two=5, one=6} == {three=4, two=5, one=6}

Baz

Henry Ho

unread,
Jun 21, 2010, 6:39:15 PM6/21/10
to cfml-convent...@googlegroups.com
[1,2,3] &= 4  ==> [1,2,3,4]?
[1,2,3] &= [4,5,6] ==> ??  [1,2,3,4,5,6] ??


Heny


--

Baz

unread,
Jun 21, 2010, 7:00:28 PM6/21/10
to cfml-convent...@googlegroups.com
[1,2,3] &= 4  ==> "Error you can only concatenate same types. Try [1,2,3] &= [4]"

If you are looking for  [1,2,3,[3,2,1]]

Then [1,2,3] &= [[3,2,1]]

Baz

Reply all
Reply to author
Forward
0 new messages