Re: [q-lang-users] Vector module
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2007-10-02 16:24:00
|
Albert Graef wrote: > So then the entire thing would boil down to providing the creation and > type-checking convenience functions, as well as implementing the three > operations 'get', 'put' and 'putmap' (or whatever we call them) on > containers, and add some convenient syntactic sugar, which could easily > be done in an afternoon, I guess. All right, I have this implemented for reference tuples, lists and streams now. You can find the source here: http://q-lang.cvs.sourceforge.net/q-lang/q/stdlib/reftypes.q?view=log Here are a few examples to show how this all works: ==> import reftypes // needs to be imported explicitly ==> def Xs = reflist [1..10] // create a new reference list ==> get (Xs!5) // get a member 6 ==> Xs!!5 // syntactic sugar for the above 6 ==> put (Xs!5) 77 // change a member () ==> Xs!5 := 77 // syntactic sugar for the above () ==> Xs!!5 77 ==> get Xs // get all members [1,2,3,4,5,77,7,8,9,10] ==> putmap (*2) Xs // destructive map () ==> get Xs [2,4,6,8,10,154,14,16,18,20] ==> fill Xs 0 // fill with given value () ==> get Xs [0,0,0,0,0,0,0,0,0,0] ==> Xs := [1..10] // update all values at once () ==> get Xs [1,2,3,4,5,6,7,8,9,10] Tuples and streams work in an analogous fashion. (If you look at the script, you will notice that reference streams are always memoized, so that they properly remember their reference values.) Infinite streams work, too, provided that you don't try to apply the strict fill, put and putmap operations to the entire stream -- a finite subsection of the stream works, though: ==> def Xs = refstream {1..} ==> Xs!!5 6 ==> Xs!5 := 77 () ==> Xs!!5 77 ==> def Ys = take 10 Xs // apply putmap only to this finite subsequence ==> strict $ get Ys {1,2,3,4,5,77,7,8,9,10} ==> putmap (*2) Ys () ==> strict $ get Ys {2,4,6,8,10,154,14,16,18,20} ==> Xs!!5 154 Note that at this point only the first 10 members of the stream have actually been evaluated, the rest of the stream is still ``thunked'': ==> Xs {<<Ref>>,<<Ref>>,<<Ref>>,<<Ref>>,<<Ref>>,<<Ref>>,<<Ref>>,<<Ref>>, <<Ref>>,<<Ref>>,ref 11|lazy (map ref (iterate (+1) ((+1) 11)))} John, is that what you wanted (more or less)? Any comments? Ok, I'm ready to do a first release candidate now. Any last-minute bug reports? :) Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |