Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Last active December 20, 2015 00:49
Show Gist options
  • Save jaytaph/6044303 to your computer and use it in GitHub Desktop.
Save jaytaph/6044303 to your computer and use it in GitHub Desktop.
saffire datastructure subscriptions

Datastructures and subscriptions

A basic datastructure is defined as:

<datastructure>[<elementlist>]
  • The datastructure can be a data-structure class (something that implements the interface datastructure, or subscription or something?).
  • The element-list is a comma separated list of 0 or more elements.
  • The element itself is a colon separated list of 0 or more (primary)expressions called items.
  • Depending on the actual data-structure used, each element can have multiple items.

Lists

Lists are simple structures that only have values. However, there ARE keys available, but these keys are maintained internally. You can set directly on key, but if you set outside the current index, the element will not be added. When the key is inside the current index, the value will be replaced. Deleting a key will mean that all the elements will move up one position.

foo (on index 0)
bar (on index 1)
baz (on index 2)
qux (on index 3)

delete key 2

foo (on index 0)
bar (on index 1)
qux (on index 2)

Adding values to a list:

list[] = 1;   // Add value 1 to the end of the list
list.push(1);  // Add value 1 to the end of the list
list.unshift(1);  // Add value 1 to the begin of the list
list.pop(1);  // Fetch (and remove) the last value from the list
list[0]         // fetch the first value
list[1]        // etch the second value
list[-1]        // fetch the last value
list[-2]        // fetch the second-last vale
list[11111]     // throws outofboundsException when this index is not found
list[anythingbutanumerical]    // throws incorrectIndexException

list.length()        // Returns size of the list
list.rand()          // return random element from the list
list.shuffle()       // shuffles around elements in list
list.split(index)    // tuple with 2 lists, split on the given index
list.map(callable)   // calls map function on each element

Questions

  • Should we allow anything on key values? Should we not just delete on key and use push and pop?
  • SHould we allow list[] to add values?
  • is foreach(list as k,v) allowed? If not, how do we fetch the meta, as k is mandatory in order to fetch meta (foreach (list as k,v,m))

Hash

This structure is the same as a dictionary, hashtable or associative array. Any type of object can be used for keys and values:

hash["foo":"bar", 1:"baz", true:false, 1:5];

Iteration will return the key in __key, and the value in __current.

Adding a new value:

hash["foo"] = val;

Removing:

hash["foo"] = null;    // This means we cannot set null's? NOt a good idea
hash.delete("foo");    // Makes sense
unset(hash["foo"])     // Can't, we don't allow functions
unset hash["foo"];     // unset will be a keyword. Don't want to go there
hash["foo"] = void;    // New datastructure that points to (void *)0; It's not the
                       // same a NULL, which is an actual object, but void is
                       // literally nothing. you cannot use void as an object.
void a;                // Another new keyword. Could be used for unsetting
                       // everything. But same issue as for unset.

void

The "void" keyword, is a keyword, not an object. It could be used on places to "eat up" certain values, or to "remove" variables. (a.unset() might actually assign void to itself).

There are some issues with this:

  • null and void might be seen as similar concepts, and thus confusing for programmers
a = null;
io.print(a);  // Outputs "null";

a = void;     // We assign to unassign (!?)
io.print(a);  // exception: undefined variable a

void a;       // It makes more sense programmatically.
io.print(a);  // eception: undeifned variable a

Slicing

It should be possible to slice data-structure. This is done with the to keyword inside the subscription:

l = mylist[0 to 6];
h = myhash["key" to "anotherkey"];

Catches

  • there will be a new hash generated with the values starting at the first index, and ends (including!) at the second index. * If the first index does not exist, the result will be empty.
  • If the second index does not exist, the result will run form the first index all the way to the end

Questions

  • Should we throw an outofbounds exception when we hit the second index before the first one?
  • Should we throw an outofbounds exception when the first index isn't found?
  • Should we throw an outofbounds exception when the second index isn't found?

Multidimensional datastructures

As datastructure can hold any other object, including datastructures, it's possible to create multidimensional structures:

l = list[list[a,b,c],list[d,e,f],list[g,h,i]];

l[0][0] // a
l[1][0] // d
l[2][2] // i

also, hashes work the same way:

h = hash["foo":"bar", "baz":"qux"];
h["tmp"] = list[1,2,3];

i = h["tmp"][0];  // Fetch first index from list inside h["tmp"]

questions

  • Does foreach() automatically loop over multidimensional arrays?
  • Should the iterator take care of this? (ie: recursiveIterator?)
  • Should we have a .__iterable() inside the baseclass that tells if an object is iterable?
  • Should strings be iterable by default (on each character?). This is nice, but breaks the .__iterable() thing, as we most likely don't want to iterate strings during foreach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment