Arrays are Awesome

presidentbeef edited this page Feb 26, 2011 · 1 revision

Currently, arrays in Brat have more methods than any other core object. Why? Because arrays are fun. They get invited to all the parties and always have something svelte to wear. Besides just looking good, arrays are pretty useful, too.

Hey by the way

There are lots of examples below. You should try them out right in your browsing machine.

Basically

Arrays are lists of things. They can grow and shrink. They might have a million items inside, or none at all. But arrays are considerate. They will always keep their items in the order you want.

Making them

By the way, there are many ways to make arrays!

[]                 #This is an empty, lonely array
[1 :a object.new]  #This is a heterogeneous array (we don't judge!)
1.to 10            #This makes an array like: [1 2 3 4 5 6 7 8 9 10]
3.of "ha"          #This makes an array like: ["ha" "ha" "ha"]

Querying them

Then we can ask the arrays some deep questions:

my_array = [1 2 3]
my_array.empty?   #Haha, nope
my_array.all? { item | item < 4 }  #Yep
my_array.any? { item | item % 2 == 0 }  #Yep!
my_array.include? :batteries  #No, that's extra
my_array.array?  #Of course it is!
my_array.length  #It's three long

Getting what you want

That is all fine and good fun, but arrays are not just for storage. Sometimes you take them down from the attic and look through them for some barely recalled memories.

my_array = [1 2 3 4]
my_array.first      #Returns 1. Because it's the first thing in the array
my_array.last       #Bet you know what this does!
my_array.rest       #Everything except Mr. First-in-line
my_array[1]         #Returns 2. Did you expect 1? Some silly people start counting from zero! How weird
my_array[-1]        #If 0 is first, what is -1? Last, I guess
my_array[1, 2]      #Returns a new array: [2, 3]
my_array[1, -1]     #Okay, kind of weird. But it returns [2, 3, 4]
my_array[-1, -3]    #Double negatives?! Maybe this one needs to be run through try.brat-lang.org

#Modifying them

Once we have an array, that doesn't mean it's frozen like a popsicle or stuck in a stuffy suit! We can still add and remove items from it:

my_array = [:a :b :c]     #Make an array
my_array << :d            #Add an item to the end
my_array.pop              #Remove an item from the end
my_array.insert 1 :hello  #Puts a little "hello" inside the array to get [:a :hello :b :c]
my_array[1] = :goodbye    #Now it's leaving. Farewell, old friend

Making more

That was pretty cool. But there is lots more arrays can do. In particular, arrays are good at generating new arrays. All of the methods below return new arrays. The original desserts are left unharmed.

desserts = [:cake :pie "ice cream" :cookies]
desserts.copy       #Copy the menu. Take home and place on refrigerator
desserts.shuffle    #Just like Vegas. But I can't talk about that now
desserts.reverse    #The same desserts, but reversed!
desserts.sort       #Now they are sorted out
desserts.reject { dessert | dessert.length > 4 }  #Just cake and pie left
desserts.select { dessert | dessert.length > 4 }  #Now it's just ice cream and cookies until noon
desserts.map { dessert | dessert.reverse }        #The desserts are really reversed now

Mappin' and reducin'

Oh, arrays can totally do that.

animals = [:horse :elephant :mongoose :dog :chicken]
lengths = animals.map { animal | animal.length }
shortest = lengths.reduce { length, shortest | true? length < shortest, length, shortest } 

Mangling them

One is not always required to make new arrays to do interesting things. Some things can be be done "in place". That is, directly modifying the array itself instead of making a new one.

more_numbers = [9 6 8 7 7 10 11]
more_numbers.sort!     #They are now in ascending order
more_numbers.reverse!  #They are now in descending order
more_numbers.shuffle!  #Who knows where they are now
more_numbers.unique!   #You know that extra 7 bothered you. Now it is gone
more_numbers.map! { n | n * 2 }  #Double your pleasure, double your fun
more_numbers.reject! { n | n % 2 == 1 } #The odd are often unloved
more_numbers.select! { n | n > 18 }    #Need to be over 18
more_numbers.delete_first 20           #Is anything even left in the array?
more_numbers.clear                     #Certainly isn't now!

Just lists of numbers

There are even some number-specific methods for arrays:

numbers = [1 2 99 1000]
numbers.max       #1000
numbers.min       #1
numbers.sum       #2002

Rummaging around

If you aren't sure where something is in an array or even what's in it really, there are some ways to look around:

mine = [:copper :gold :saltpeter :silver :diamond]
mine.each { mineral | p mineral }   #Prints out each mineral
mine.reverse_each { mineral | p mineral }  #Prints them out, but the other way
mine.each_with_index { mineral, index | p "#{index}. #{mineral}" }  #Print out a numbered list
mine.find { mineral | mineral[0] == "g" }  #Returns the first mineral starting with "g"
mine.index_of :silver  #Returns the index of the first mineral equal to "silver"
mine.rindex_of :silver #Returns the index of the last mineral equal to "silver" (the same, in this case)
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.