Is there a language where collections can be used as objects without altering the behavior?
- by Dokkat
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"