[chuck-users] Are array types really classes ?

Michael Heuer heuermh at gmail.com
Fri Sep 4 11:22:39 EDT 2009


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


More information about the chuck-users mailing list