Tuesday, April 22, 2014

Comparison of Arrays, briefly

So. Everything is an object. That was js, right? An object can be an association between a key (name) and a value. Which makes an object sort of an array, right? Because an array is a collection of key:value pairs. If key is a name, that's an object, if it's an index value, that's an array. Key:value pairs. Yep.

Examples from things I have some familiarity with:

JavaScript array creation:
var myArray = ["element1", "element2", "bunnies"];
console.log(myArray[2]);  
//prints "bunnies"


jQuery object (more than just an array):
var divs = $("div");
console.log(divs);  
//prints "[0: div.container, 1: div.logo, 2: div.content...]"


PHP:
$animals = array("dragon", "giraffe", "hedgehog");
echo $animals[2];
//displays "giraffe"


Python Lists (mutable, homogenous):
>>> colors = [['blue', 'red'], ['sage', 'vermillion']]
>>> colors[-1][0]
'sage'


And Python Dictionaries (essentially objects/associative arrays):
>>>dict = {'nonfiction': ['Pollan', 'Berners-Lee'], 'fiction': ['Tolkien', 'Asimov']}
>>> dict['nonfiction'][1]
'Berners-Lee'


And Python Tuples (immutable, heterogenous):
>>>tup = ('a', 3, -0.2)
>>>tup[1]
3


Java (fixed length):
String[] rabbits = {"fuzzy", "hoppy", "ears"};
System.out.println(rabbits[1]);
//prints "hoppy"


C (Closest to Java. Info from Learn C the Hard Way. I hadn't touched C before today, forgive me.):
main(){
char name[] = "Petunia"; 
//creates array of chars {'p', 'e', 't'...etc}
printf("name is %s.\n", name);
printf("or also %c %c %c.", name[0], name[1], name[2]);
}
//prints
name is Petunia.
or also P e t.

2 comments:

  1. Hi Elli,

    Really, both arrays and associative arrays are just special cases of functions, whose values have been precomputed and stored for fast lookup. Allow me to explain.

    Imagine you have two sets of values X and Y and a projection function that maps each value in X to a single value in Y. Now if X contains integer values, this matches the semantics of an array. If X contains non-integer values, this matches the semantics of an associative array.

    To take the metaphor a bit further, imagine the values from your X and Y sets arranged along X and Y axes on a 2D plot. Mark the (X,Y) points corresponding to the entries in your array or associative array, and once again we have a clean mathematical representation of our data structures.

    And what do both of these mathematical descriptions of arrays and associative arrays have in common? Yep, both of these show that those data structures could be represented perfectly well with deterministic functions taking one input and returning one output.

    Here's an example of an array-like function in the Clojure programming language (http://clojure.org):

    (defn my-array-function [index]
    (case index
    0 "first value"
    1 "second value"
    2 "last value"
    (throw (Exception. "Index out of bounds."))))

    And here's an example of an associative array-like function:

    (defn my-associative-array-function [key]
    (case key
    "foo" "first value"
    "bar" "second value"
    "baz" "last value"
    nil))

    Easy peasy, right?

    So why would we want to use an array or associative array in our programs rather than just implementing the same effect with functions? Well...first, using a data structure provides a more declarative, readable representation of our information. Second, array and associative array lookup is quite fast, while function evaluation might be slow, depending on how the result is calculated.

    So there you go. Some nuggets of wisdom to chew on while sipping tea in the evening.

    Happy Hacking!
    ~Gary

    ReplyDelete
    Replies
    1. Gary- that's a neat way of looking at it. Thank you for the nuggets!

      Delete