Is there a language where collections can be used as objects without altering the behavior?
Posted
by
Dokkat
on Programmers
See other posts from Programmers
or by Dokkat
Published on 2012-06-03T21:33:12Z
Indexed on
2012/06/03
22:46 UTC
Read the original article
Hit count: 454
Is there a language where collections can be used as objects without altering the behavior?
As an example, first, imagine those functions work:
function capitalize(str)
//suppose this *modifies* a string object capitalizing it
function greet(person):
print("Hello, " + person)
capitalize("pedro")
>> "Pedro"
greet("Pedro")
>> "Hello, Pedro"
Now, suppose we define a standard collection with some strings:
people = ["ed","steve","john"]
Then, this will call toUpper() on each object on that list
people.toUpper()
>> ["Ed","Steve","John"]
And this will call greet once for EACH people on the list, instead of sending the list as argument
greet(people)
>> "Hello, Ed"
>> "Hello, Steve"
>> "Hello, John"
© Programmers or respective owner