Hi list, I am currently learning Chuck, so I this may sound like a silly question. The language specification states that arrays are objects, but I can't find the proper syntax to declare a class that extends an array type. Since, as far as I understood, float[] is a reference to an array of floats, I was not really surprised that Chuck does not accept "class foo extends float[]", but what would the proper syntax be, if that is possible at all ? Thanks
Cyrille.Damez@laposte.net wrote:
I am currently learning Chuck, so I this may sound like a silly question. The language specification states that arrays are objects, but I can't find the proper syntax to declare a class that extends an array type. Since, as far as I understood, float[] is a reference to an array of floats, I was not really surprised that Chuck does not accept "class foo extends float[]", but what would the proper syntax be, if that is possible at all ?
Seems more likely that foo has-a float[] rather than foo is-a float[]. I'm curious, what would you want to extend a float[] for? If you are interested in collections-like stuff, LiCK has List, ArrayList, FloatArrayList, etc. http://github.com/heuermh/lick Contributions welcome. michael
Seems more likely that foo has-a float[] rather than foo is-a float[]. I'm curious, what would you want to extend a float[] for?
Probably stuff like sorting, removing duplicates, etc. We can't BTW. Arrays are indeed objects (you can for example assign them) but they might not really be classes as such. I'm really not sure there, it seems like arrays are more or less a construct on their own that gets applied to a given class. The "concept of array" doesn't seem to have a name beyond being hinted at by using square brackets in ChucK. I'm not sure how it should be extended, syntactically, but I could see uses to that. Here be dragons, let's first sort out the issues of the type system (particularly as it applies to arrays), then look onwards. Yours, Kas.
Hey Michael,
I'm not really sure where to start making a dent in your LiCK classes.
What do the classes do exactly, and do you have any examples of LiCK
in use? I'm seeing lots of classes like Atan that just take
Math.atan(x), and I'm wondering how that helps. Also, I'm assuming it
takes 1.2.2 since it uses array operators like << and .size and all
(that's the connection to our current thread). Thanks,
Andrew
2009/9/2 Kassen
Seems more likely that foo has-a float[] rather than foo is-a float[]. I'm curious, what would you want to extend a float[] for?
Probably stuff like sorting, removing duplicates, etc.
We can't BTW. Arrays are indeed objects (you can for example assign them) but they might not really be classes as such. I'm really not sure there, it seems like arrays are more or less a construct on their own that gets applied to a given class. The "concept of array" doesn't seem to have a name beyond being hinted at by using square brackets in ChucK. I'm not sure how it should be extended, syntactically, but I could see uses to that.
Here be dragons, let's first sort out the issues of the type system (particularly as it applies to arrays), then look onwards.
Yours, Kas.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Andrew C. Smith wrote:
I'm not really sure where to start making a dent in your LiCK classes. What do the classes do exactly, and do you have any examples of LiCK in use? I'm seeing lots of classes like Atan that just take Math.atan(x), and I'm wondering how that helps. Also, I'm assuming it takes 1.2.2 since it uses array operators like << and .size and all (that's the connection to our current thread).
The wiki helps a tiny bit
http://wiki.github.com/heuermh/lick
The unit tests show what the major classes do, e.g.
http://github.com/heuermh/lick/blob/b30c8ab28abe6e1580e43819fe586af247cfd298...
All the Atan, etc. functions are a workaround for ChucK's lack of
function pointers. For a float list
FloatArrayList list;
list.add(Math.PI); or Math.PI => list.add;
Atan atan;
list.transform(atan);
<<
On Thursday 03 September 2009 01:54:07 Michael Heuer wrote:
I'm curious, what would you want to extend a float[] for?
Very simple things like (algebraic) vectors, list of probabilities, etc. Of course I can always make classes that contain just a vector and duplicate every needed member functions and operators of float[], but that feels a bit silly . Moreover, unless there is some way of writing an implicit reference cast operator from foo to float[] (again, excuse this possibly trivial question, I am learning), I won't be able to use existing functions that take array references as parameters directly on foo (I will have to pass the member foo.content or trivially overload all of them). Yes I am lazy :)
Cyrille; Very simple things like (algebraic) vectors, list of probabilities, etc. Of
course I can always make classes that contain just a vector and duplicate every needed member functions and operators of float[], but that feels a bit silly .
Wouldn't writing a Vector class, then creating a array of those come down to this? Vector my-vectors[8]; Would do the trick, assuming you wrote the "Vector" class. That will get you everything that float[] has, I think, at least everything that "float[]" has that "float" lacks.
Moreover, unless there is some way of writing an implicit reference cast operator from foo to float[] (again, excuse this possibly trivial question, I am learning), I won't be able to use existing functions that take array references as parameters directly on foo (I will have to pass the member foo.content or trivially overload all of them).
We don't have operator overloading right now. At least you can't overload them yourself; of course the ChucK operator is quite overloaded. I'd still like to have overloading of the ChucK operator for classes we write ourselves (probably cast as well). Right now they can't be chucked to other objects to yield values or form connections, etc. This seems a bit unnatural to me; basically everything but the stuff we create can be chucked to things while -hopefully- the things we just created are very important to the domain we are reasoning about.
Yes I am lazy :)
A comendable property for a programmer, I hear :-). Good questions. Yours, Kas.
On Thursday 03 September 2009 22:17:04 Kassen wrote:
Wouldn't writing a Vector class, then creating a array of those come down to this?
Not quite, because I can't add member functions that would perform "horizontal" operations (e.g. norm() for vectors, or draw() for probability arrays).
We don't have operator overloading right now. At least you can't overload them yourself; of course the ChucK operator is quite overloaded.
Indeed. That must have been why I assumed we could. Thanks for the answers!
Hi Cyrille, On Sep 3, 2009, at 5:51 PM, Cyrille.Damez@laposte.net wrote:
On Thursday 03 September 2009 22:17:04 Kassen wrote:
Wouldn't writing a Vector class, then creating a array of those come down to this?
Not quite, because I can't add member functions that would perform "horizontal" operations (e.g. norm() for vectors, or draw() for probability arrays).
Sometimes I create public classes just as namespaces to encapsulate functionality. You could have public class Probabilities that contains a bunch of static members and then create a Vector class that has-a float[] and whatever else it needs and wraps Probabilities.norm () in member function Vector.norm(). Would that be a compromise? Or am I missing something? -mike
We don't have operator overloading right now. At least you can't overload them yourself; of course the ChucK operator is quite overloaded.
Indeed. That must have been why I assumed we could.
Thanks for the answers! _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Mike;
Not quite, because I can't add member functions that would perform "horizontal" operations (e.g. norm() for vectors, or draw() for probability arrays).
Sometimes I create public classes just as namespaces to encapsulate functionality. You could have public class Probabilities that contains a bunch of static members and then create a Vector class that has-a float[] and whatever else it needs and wraps Probabilities.norm() in member function Vector.norm().
Would that be a compromise? Or am I missing something?
I agree, particularly as the normalisation operation would most likely be applied to the vector (probably to yield a unity length) and it wouldn't be the array itself that would be normalised. So; if it would apply to the vector it could/should belong to the vector's properties/namespace I'd say. A separate question might be whether we would need some syntactic sugar to apply a single operation to all array entries in a given array (for example to normalise a set of a thousand vectors or to compute the absolute value of all floats in a given array). This might make sense but a simple "for" loop and a single line of code will do the job as well. For multi-dimensional arrays this quickly becomes so hard to generalise that I think for loops are perfectly fine, especially as they are so clear. In practice much of this would depend on the exact issue we are trying to solve and how. We could look into some concrete examples if Cyrille has some and see where the issues arise. I myself would certainly start here with defining a class named "vector", that would hold a array (of the vector's components) as well as member functions for normalising it, multiplying it by floats and possibly others, depending on the situation. It takes a bit of work and thought but once done you can re-use it forever. Yours, Kas.
Kassen wrote:
Not quite, because I can't add member functions that would perform "horizontal" operations (e.g. norm() for vectors, or draw() for probability arrays).
Sometimes I create public classes just as namespaces to encapsulate functionality. You could have public class Probabilities that contains a bunch of static members and then create a Vector class that has-a float[] and whatever else it needs and wraps Probabilities.norm() in member function Vector.norm().
Would that be a compromise? Or am I missing something?
I agree, particularly as the normalisation operation would most likely be applied to the vector (probably to yield a unity length) and it wouldn't be the array itself that would be normalised. So; if it would apply to the vector it could/should belong to the vector's properties/namespace I'd say.
A separate question might be whether we would need some syntactic sugar to apply a single operation to all array entries in a given array (for example to normalise a set of a thousand vectors or to compute the absolute value of all floats in a given array). This might make sense but a simple "for" loop and a single line of code will do the job as well. For multi-dimensional arrays this quickly becomes so hard to generalise that I think for loops are perfectly fine, especially as they are so clear.
Did you see the line list.transform(atan); in my example above? There's also void assign(float) void forEach(FloatProcedure procedure) void forEach(FloatPredicate predicate, FloatProcedure procedure)
In practice much of this would depend on the exact issue we are trying to solve and how. We could look into some concrete examples if Cyrille has some and see where the issues arise. I myself would certainly start here with defining a class named "vector", that would hold a array (of the vector's components) as well as member functions for normalising it, multiplying it by floats and possibly others, depending on the situation.
FloatArrayList has float sample() which I assume to be the same as draw(). How might norm() be defined? The definition I'm familiar with normalizes all the float values in the array to the range [0.0 - 1.0] based on the minimum and maximum float values.
It takes a bit of work and thought but once done you can re-use it forever.
Or use FloatArrayList, which has already been written. :) michael
The definition I'm familiar with normalizes all the float values in the array to the range [0.0 - 1.0] based on the minimum and maximum float values.
What I'm familiar with is that we would have a vector, say a 3 component one representing a line-segment in 3d space. Normalising would mean multiplying all components by the same number so that it would represent a
Michael How might norm() be defined? line segment of length "one", in the same direction of the original vector that might have a arbitrary length. This will make the vector indicate a direction in a way that has lots of uses, for example in quaternions for 3d rotations. Your definition might make sense as well, for example in setting EQ bands or mixer channels or some such application. I'm not sure what solution is needed here or why but if you want to you can, which is the important bit.
Or use FloatArrayList, which has already been written. :)
Of course! :-) Kas.
participants (5)
-
Andrew C. Smith
-
Cyrille.Damez@laposte.net
-
gelfmuse@gmail.com
-
Kassen
-
Michael Heuer