Skip to content

List and Array operations

Michael edited this page May 15, 2013 · 8 revisions

This is an attempt to collect ideas and discussion on list/array operations.

Issues:

Resources:

Ideas:

  • Functional list paradigm: cons/car/cdr
  • List slicing (extract subset of a list)
    • Python notation: list[a:b] (a through b-1), list[a:] (a to end), list[:b] (start to b-1), list[:] (entire list)
  • Picking indices

list2 = list[2,4]; // returns [3,1] ```

  • Concatenation
    • Using comma as concatenation operator: newlist = lista, listb;
    • Use the concatenation operator from the D programming language: lista ~= listb; listc = lista ~ listb;
list = [];
r=10;
for (i=[0:10:360]) {
  list ~= [[cos(i)*r, sin(i)*r, 0]];
}
  • Use stack-like push / unshift

  • Declare lists in the same way as ranges

    • list = [2:0.5:4]; // Returns [2,2.5,3,3.5,4]
  • Support list comprehensions (map):

function double(list) = _ * 2;
list = [0,1,2,3];
list2 = double(list); // Returns [0,2,4,6]
  • Python-inspired syntax: [for i = [a:b] f(i)];
  • Advanced recursion example:
function a(b) = length(b) > 1 ? ( sin(b[0]) * 5, a(b[1:]) : sin(b[0]) * 5;
  • Random access: list = [0,1,2,3]; list[1] = 4; // Returns [0,4,2,3];

  • Iteration over a vector

   foreach (i=vector) {
      ...
   }
  • Various math functions would help, my max/min/sum from http://www.thingiverse.com/thing:70745 for example, it would have been much easier if the built-in functions supported vectors. Where the vector is of the right type of course.
Clone this wiki locally