Page 1 of 1

Better array support?

Posted: Mon Dec 03, 2007 2:44 am
by itistoday
Is this planned for the future? It would be nice if more of the functions that operated on lists would also operate on arrays, for example (sort). As a big fan of newLISP I would be glad to help implement this myself, it's just that school won't give me a minute's rest. :\

Posted: Mon Dec 03, 2007 9:08 am
by cormullion
Sounds like a good idea - for all types to be equal!

You can always sort this way:

Code: Select all

> (set 'arry (array 3 3 (randomize (sequence 1 100))))
((83 61 84) (33 68 42) (30 63 46))
> (sort (array-list arry) (fn (x y) (< (x 2) (y 2))))
((33 68 42) (30 63 46) (83 61 84))
But that will probably be slightly slower than an array-optimized sort, and presumably you're using arrays longer than 200 elements or so for speed reasons anyway.

Posted: Mon Dec 03, 2007 12:56 pm
by Jeff
Arrays don't work the same as lists underneath. I think that newLISP arrays are meant to be mathematical arrays, not like PHP arrays.

Posted: Mon Dec 03, 2007 11:13 pm
by itistoday
cormullion wrote:Sounds like a good idea - for all types to be equal!
You can sort a list that contains mixed-type elements, so I see no reason why you couldn't do the same for an array. I wrote a C vector implementation a while ago that let you do this, you can use it just like a list, except insertions/removal from anywhere other than the back take longer. You just have an array of pointers to "DataHolder" unions that can be anything. It would then follow the same rules that sort uses for lists.

Posted: Thu Dec 13, 2007 12:24 pm
by cormullion
nice one Lutz!

Code: Select all

newLISP v.9.2.9 on OSX UTF-8, execute 'newlisp -h' for more info.

> (set 'a (array 2 2 (randomize (sequence 1 10))))
((8 7) (2 10))
> (sort a)
((2 10) (8 7))

Posted: Thu Dec 13, 2007 2:57 pm
by itistoday
cormullion wrote:nice one Lutz!

Code: Select all

newLISP v.9.2.9 on OSX UTF-8, execute 'newlisp -h' for more info.

> (set 'a (array 2 2 (randomize (sequence 1 10))))
((8 7) (2 10))
> (sort a)
((2 10) (8 7))
Awesome! Thanks Lutz! :D

Posted: Thu Dec 13, 2007 3:25 pm
by Lutz
Awesome! Thanks Lutz! :D
You are welcome. Always read the release/changes notes. Many of the user suggestions make it into a new version shortly, others are noted and may show up later.

Lutz