> By default, I try to write code using basic Scheme idioms. But if I'm
> rewriting code from languages like C, C++, Ruby, etc, which have nice
> shorthand syntax for array element access, structure access, and method
> calling, it really makes me wanna cook something up on the Scheme side
> to achieve similar concision.
>
> One approach I've messed around with and have liked is as follows.
>
> Here's an 'is-vector' macro:
I have a library '(agave geometry pt)' for 2d vectors. I've messed
around with an 'is-pt' macro for that record type as well. But what
about a vector ot pt instances? Here's 'is-vector-of-pt-with-index' :-)
http://gist.github.com/242742
Define a vector of pt instances:
> (define v
(vector (pt 0 0)
(pt 1 1)
(pt 2 2)))
And an index:
> (define i 0)
Declare 'v' and 'i':
> (is-vector-of-pt-with-index v i)
Access the elements of 'v' via 'i':
> (v.i)
#[pt 0 0]
> (set! i 1)
> (v.i)
#[pt 1 1]
> (set! i 2)
> (v.i)
#[pt 2 2]
> (set! i 0)
Access the components of a particular pt:
> (v.i.x)
0
> (v.i.y)
0
Change the components:
> (v.i.x! 5)
> (v.i.y! 6)
> (v.i.x)
5
> (v.i.y)
6
> v
#(#[pt 5 6] #[pt 1 1] #[pt 2 2])
>
It might also be good to support multiple index variables (e.g. 'i',
'j', etc).
On a related note, it would be nice to declare the types of record
fields:
(define-record-type ball
(fields (pos is pt)
(vel is pt)))
So I can do:
(define b (make-ball ...))
(is-ball b)
(b.pos.x)
etc...
And perhaps instead of:
(define b (make-ball ...))
(is-ball b)
have:
(define-ball b ...)
Again, I don't always have a need for such shorthand syntax, but when
dealing with math heavy C/C++ code involving vectors, matrices, etc,
this kind of syntax helps tremendously, at least in the porting phase.
Ed