jonathanhogg / flitter

A functional programming language and declarative system for describing 2D and 3D visuals
https://flitter.readthedocs.io
BSD 2-Clause "Simplified" License
39 stars 1 forks source link

Should indexing wrap? #42

Closed jonathanhogg closed 7 months ago

jonathanhogg commented 7 months ago

There is an argument that indexing should wrap the same way that vector expansion in operators works, i.e., indices are modulo the length of the vector.

This would have a few benefits:

Question: should multi-binding let statements also wrap a short vector or should they match the behaviour of a multi-binding for loop? It would seem to be useful if they did wrap, as then one could write:

let width;height=SIZE

and have it do The Right Thing without having to do SIZE * (1;1).

This feels like it would be a substantive enough change that I should do it before 1.0.0 or not do it at all.

jonathanhogg commented 7 months ago

A possible down-side of this might be if I slice a vector anywhere and expect that a short vector will be honoured.

Seems like the best idea is to try this in a branch and test all of my existing code to see if any breaks…

jonathanhogg commented 7 months ago

Note that indexing null is still going to have to return null.

jonathanhogg commented 7 months ago

Initial tests with draft PR show that it seems to work fine.

jonathanhogg commented 7 months ago

Actually, on that "expanding out a vector note", you could force any vector to be a specific size with a range. So if radius needs to be 3 items long, then a simple radius[..3] would accomplish that. Most of the time this wouldn't be necessary, but it'd be handy when using functions like hypot(). So hypot(SIZE[..2]) would give the diagonal length of a SIZE rectangle even if SIZE is a single value (i.e., a square).