From: Eduardo O. <edu...@gm...> - 2025-05-25 15:56:13
|
Hi list, I always had the impression that defstruct was just the core of something, and users were expected to write the rest... here is my most recent attempt to write that "rest". The code is just this, "@@4"(structname,o,method,Args) := apply(concat(structname,"__",method), append([o],Args)); "@@" (o,method) := buildq([o,method], lambda([[Args]], "@@4"(op(o),o,method,Args))); infix("@@",200,201); and here is a test: seqby(a,b,stp) := makelist(i, i,a,b,stp); /* convenience */ defstruct(poly(poly,var)); poly__hi (p) := hipow(p@poly, p@var); /* methods */ poly__coef (p,k) := ratcoef (p@poly, p@var, k); poly__coefs(p,ks) := makelist(p@@coef(k), k, ks); poly__L (p) := p@@coefs(seqby(p@@hi(),0,-1)); myp : new(poly(10*x^2 + 20*x + 30, x)); myp@@hi(); /* 2 */ myp@@coefs([3,2,1,0]); /* [0, 10, 20, 30] */ myp@@L(); /* [10, 20, 30] */ This will become a blog post (-ish) at some point, but that may take some days. Cheers, all comments and pointers welcome =), Eduardo Ochs http://anggtwu.net/eev-maxima.html |