Pylogram array syntax
Aug. 2nd, 2012 12:39 pmThis isn't actually part of pylogram, it works on any arrays, but is useful for expressing things in Pylogram. Assuming that Point is an object with .x and .y members and Array acts like a list of Points, it lets you say:
A few caveats:
import pylogram # Graph of y=2x+1 pts = Array(10,Point) each(pts).y = 100-each(pts).x * 2 + 1 each(pts).x = prev(pts).x + 1 every(pts).col = rgb(0,0,0) display(pts)
A few caveats:
- I'm not sure if this is cool or horrible
- In this example Point and Array are pylogram objects, so you can specify y in terms of x before specifying x.
- It also works if pts was a normal list of structs, except that it's normal assignemnt so you have to assign x before using x to calculate y
- However, there's a bug in the "each(pts).x = prev(pts).x" case where prev(pts).x returns a copy of the list rather than the actual values, so after you update pts[1] in terms of pts[0], you don't get that value used to update pts[2]
- If you want to assign an element of the array, instead of a member of an element, you need the "each(int_arr).val = each(int_arr).val+1" syntax
- Colours don't actually work yet, I just wanted an example of setting every element to the same value.
- I was originally going to make this a property of array, which means you might be able to use slice notation: "arr[each].y = arr[each].x" and you wouldn't have to worry about assigning to elements of the array. But having it in separate functions means they work on any iterable container too.