# [chuck-users] Function to remove duplicates from an array

David Powers cyborgk at gmail.com
Fri Apr 21 15:10:28 EDT 2006

```Hello everyone,

I am writing some functions that will be made into a class, for doing basic
serial operations on cells (i.e. arrays of floats). The operations I have so
far are: transpose, reverse, invert, permute, shuffle.

The next function to finish is multiply, which does Boulez-style cell
multiplication. For the function to work correctly, it should remove any
duplicates from the array. Unfortunately, my brain's a little rusty right
now. I'm sure I did this in the only computer class I ever took (C++). But
I'm too sleepy to figure out the correct solution offhand, I think the ways
I'm coming up with are inefficient. Can anyone help?

I've gotten really spoiled, by usually coding these sorts of things in
Python, where its amazingly quick to do list manipulations of any kind.

Here's my code so far, which works except multiply is a dummy function:

//cellFun.ck

public class Argh
{
fun static void print(float cell[])
{
for (0 => int i; i<cell.cap(); i++)
{
<<< cell[i] >>> ;
}
}

fun static void remove (float a[], int index)
{
a.cap()-1 => int newLength;
float temp[newLength];
for (0 => int i; i < newLength; i++)
{
if (i<index)
{
a[i] => temp[i];
}
else
{
a[i+1] => temp[i];
}
}
float a[newLength];
for (0 => int i; i < newLength; i++ )
{
temp[i] => a[i];
}
}

//match
fun static int match(float cell[], float element)
{
cell.cap() => int length;
-1 => int position;
for (0 => int i; i<length; i++)
{
if (cell[i] == element)
{
i => position;
break;
}
}
return position;
}

}

fun float[] copyCell(float cell[])
{
cell.cap() => int length;
float newCell[length];
for (0 => int i; i<length; i++)
{
cell[i] => newCell[i];
}
return (newCell);
}

//transpose
fun float[] tranCell(float cell[], float amount)
{
cell.cap() => int length;
float newCell[length];
for (0 => int i; i<length; i++)
{
cell[i] + amount => newCell[i];
}
return(newCell);
}

//reverse
fun float[] revCell(float cell[])
{
cell.cap() => int length;
float newCell[length];
length - 1 => int counter;
for (0 => int i; i<length; i++)
{
cell[i]=> newCell[counter];
counter--;
}
return(newCell);
}

//invert()

fun float[] invCell(float cell[])
{
cell.cap() => int length;
float newCell[length];
cell[0] => newCell[0];
for (1 => int i; i<length; i++)
{
(2 * cell[0]) - (cell[i]) => newCell[i];
}
return(newCell);
}

//shufCell():

fun float[] shufCell(float cell[])
{
cell.cap() => int length;
copyCell(cell) @=> float newCell[];
//--- Shuffle elements by randomly exchanging each with one other.
for (0 => int i; i<length; i++)
{
std.rand2(0,length-1) => int r;  // generate a random position
newCell[i] => float temp;
newCell[r] => newCell[i];
temp => newCell[r];
}
return(newCell);
}

//permCell(float cell[], int amount):

fun float[] permCell (float cell[], int amount)
{
cell.cap() => int length;
float newCell[length];
for (0=>int i; i<length; i++)
{
cell[(amount+i)%length] => newCell[i];
}
return(newCell);
}

//multiply(float cell[], float multCell[])
//unfinished
//look for a match
//store the position in a new array

fun float[] multCell (float cell[], float mult[])
{
<<<"unfinished">>>;
return [0.0];
}

//tests!!!

fun void testShiz()
{
[0.0,2.0,4.0,6.0,8.0,2.0,10.0] @=> float testCell[];
copyCell(testCell) @=> float theCopy[];
[0.0,1.0,2.0] @=> float multi[];

<<<"print cell">>>;
Argh.print(testCell);

<<<"match (return position of first occurrance of 2.0 in cell)">>>;
<<<Argh.match(testCell,2.0)>>>;

<<<"transpose cell by 2.0 (returns a copy)">>>;
Argh.print(tranCell(testCell, 2.0));

<<<"reverse cell (returns a copy)">>>;
Argh.print(revCell(testCell));

<<<"invert cell (returns a copy)">>>;
Argh.print(invCell(testCell));

<<<"shuffle cell (returns a copy)">>>;
Argh.print(shufCell(testCell));

<<<"permCell(permute by 2, returns a copy)">>>;
Argh.print(permCell(testCell, 2));

<<<"multCell (multiply by [0.0,1.0,2.0], returns a copy">>>;
Argh.print(multCell(testCell,multi));

<<<"print the original">>>;
Argh.print(testCell);

<<<"permute the original by 2 and print">>>;
permCell(testCell, 2) @=> testCell;
Argh.print(testCell);

<<<"print the copy">>>;
Argh.print(theCopy);
}

testShiz();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20060421/3c2b6021/attachment.htm
```