views:

122

answers:

5

Possible Duplicate:
Access array element from function call in php

Sorry if this has been asked before, but due to the symbols I can't search Google or Stack Overflow for it. If it's been asked before, feel free to close it as a dupe.

In some languages, if a function returns an array, then as opposed to storing the array in a variable then retrieving a single element, like this:

var someValues = getSomeValues();
var firstElement = someValues[0];

You can use the array index notation directly on the function call to retrieve the element of the returned array instead, like this:

var firstElement = getSomeValues()[0];

What is this construct known as? Does it have a special name at all?

+1  A: 

I use the term Lookup.

ChaosPandion
+3  A: 

You can use the array index notation directly on the function call to retrieve the element

var firstElement = getSomeValues()[0];

What you are doing is accessing the indexed element of the array/collection returned by the function. You are not directly accessing an index in a function.

I would call this "chaining" an indexer to the return value.

Oded
Yes, I did mean that. I should've been clearer in my question.
BoltClock
@BoltClock - So if I am understanding this correctly my answer is not what you are looking for?
ChaosPandion
@ChaosPandion: I wouldn't be sure myself since I'm asking, but if you understand it to be that way, so be it.
BoltClock
@BoltClock - Sorry, I'm a bit slow in the morning. Ah who am I kidding I am always slow. :)
ChaosPandion
@ChaosPandion: and for the record, that upvote on your answer is actually mine :)
BoltClock
A: 

Eh.. Dangerous...??

If there are no elements in the returned array.

It's a bad habit to get into when it comes to dealing with arrays, javascript aside as per comment.

Other than that the idea is known as 'method chaining'

Adrian Regan
Please elaborate.
ChaosPandion
Ok, but just to be clear this would not be dangerous in a language like JavaScript. Unless of course you try and invoke the `undefined` return value as a function.
ChaosPandion
+1  A: 

I believe it doesn't have any special name. You're just accessing the first element of an array returned. In many languages there's nothing special in a function that returns an array.

In your case, you're not applying [] (indexing) operator to a function call. Instead, you apply it to the returned array. With precedence, it looks like this.

var firstElement = ( getSomeValues() )[0];

So there's nothing special in it.


However, there's another pattern of getting the first element, related to functional programming. For example:

($first_value, @rest_of_list) = getSomeValues();  # Perl
let first::rest = getSomeValues () in ...         (* OCaml *)

This saves the first value of a list (technically, not of an array) into $first_value, and the rest are stored in the array variable. That array variable may be omitted, and then you get the first value only.

($first_value) = getSomeValues();        # Perl
let first::_ = getSomeValues () in ...   (* OCaml *)

The more generic concept, of which what shown above is just a special case, is pattern matching (more on pattern matching). In some functional languages (none of the shown above, but in Haskell, for example) it is different: it indeed only computes first element, and doesn't make the rest of list be evaluated.

However, there's nothing that could qualify as pattern matching at all in the procedural code you showed.

Pavel Shved
Thanks for the thorough explanation, *and* the functional programming example. +1
BoltClock
`sequence.First()` would be the equivalent .NET call?
ChaosPandion
+1  A: 

Array dereferencing

Eton B.