Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
126
Task/Arrays/Ecstasy/arrays.ecstasy
Normal file
126
Task/Arrays/Ecstasy/arrays.ecstasy
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
module test {
|
||||
void show(Object o=Null) {
|
||||
@Inject Console console;
|
||||
console.print(o);
|
||||
}
|
||||
|
||||
void run() {
|
||||
// an array literal has Constant mutability; it is **not** mutable
|
||||
immutable Int[] literalArray = [1,2,3];
|
||||
show($"{literalArray=}, {&literalArray.actualType=}");
|
||||
|
||||
// obtaining the size or any element of an array is easy
|
||||
show($"{literalArray.size=}, {literalArray[2]=}");
|
||||
|
||||
// modifications to a Constant array result in a new Constant array;
|
||||
// in Computer Science, this is called a persistent data structure
|
||||
immutable Int[] biggerArray = literalArray + 4;
|
||||
show($"{biggerArray=}, {&biggerArray.actualType=}");
|
||||
|
||||
immutable Int[] biggestArray = biggerArray + biggerArray;
|
||||
show($"{biggestArray=}, {&biggestArray.actualType=}");
|
||||
|
||||
// arrays can be accessed using the bracket operators
|
||||
show($"element at {biggestArray[2]=}");
|
||||
|
||||
// attempts to modify an immutable array "in place" will result in an
|
||||
// exception at runtime
|
||||
try {
|
||||
biggestArray[2] = 99;
|
||||
} catch (ReadOnly e) {
|
||||
show($"immutable array not modified: {biggestArray=}");
|
||||
}
|
||||
|
||||
// fixed-size arrays are like C/Java/C# arrays; their elements are
|
||||
// all set to the default value of the array Element type
|
||||
Int[] fixedLengthArray = new Int[10];
|
||||
show($"element at {fixedLengthArray[2]=}");
|
||||
|
||||
// you can also initialize all the elements to a specific value
|
||||
Int[] negOnes = new Int[3](-1);
|
||||
show($"{negOnes=}");
|
||||
|
||||
// ... or using a lambda
|
||||
Int[] counting = new Int[5](i -> i);
|
||||
show($"{counting=}");
|
||||
|
||||
// attempts to modify a fixed-size array "in place" will succeed
|
||||
counting[1] = 99;
|
||||
show($"replaced [1]=99: {counting=}");
|
||||
|
||||
// attempts to add or delete elements from a fixed-size array will
|
||||
// raise an exception
|
||||
try {
|
||||
counting += 101;
|
||||
} catch (ReadOnly e) {
|
||||
show($"Fixed mutability array not appendable: {counting=}");
|
||||
}
|
||||
|
||||
// you can ask an array for its mutability
|
||||
show($"{literalArray.mutability=}, {fixedLengthArray.mutability=}");
|
||||
|
||||
// you can convert an array from one mutability to another; the
|
||||
// Persistent mutability is just like the Constant mutability,
|
||||
// except that the array doesn't have to be immutable, so the
|
||||
// array can hold elements that are mutable, but no elements can
|
||||
// be added, removed, or replaced
|
||||
Int[] constantToMutable = biggestArray.toArray(Mutable);
|
||||
show($|{constantToMutable=}, {&constantToMutable.actualType=},\
|
||||
| {constantToMutable.mutability=}
|
||||
);
|
||||
Int[] constantToFixed = biggestArray.toArray(Fixed);
|
||||
show($|{constantToFixed=}, {&constantToFixed.actualType=},\
|
||||
| {constantToFixed.mutability=}
|
||||
);
|
||||
Int[] fixedToPersistent = counting.toArray(Persistent);
|
||||
show($|{fixedToPersistent=}, {&fixedToPersistent.actualType=},\
|
||||
| {fixedToPersistent.mutability=}
|
||||
);
|
||||
Int[] fixedToConstant = counting.toArray(Constant);
|
||||
show($|{fixedToConstant=}, {&fixedToConstant.actualType=},\
|
||||
| {fixedToConstant.mutability=}
|
||||
);
|
||||
|
||||
// a slice of an array is an array; this is very handy
|
||||
Int[] slice = constantToMutable[1..2];
|
||||
show($"{slice=}");
|
||||
|
||||
// slices may rely on the array that they are sliced from; to ensure that
|
||||
// changes to the original array don't appear in the slice, the slice
|
||||
// must be reified
|
||||
constantToMutable[1] = 17; // this will appear in the slice
|
||||
slice = slice.reify();
|
||||
constantToMutable[2] = 18; // this will NOT appear in the slice
|
||||
show($"{constantToMutable=}, {slice=}");
|
||||
|
||||
// slices can be inclusive or exclusive
|
||||
show($"{constantToMutable[1..2]=}");
|
||||
show($"{constantToMutable[1..<2]=}");
|
||||
show($"{constantToMutable[1>..2]=}");
|
||||
show($"{constantToMutable[1>..<2]=}");
|
||||
|
||||
// creating a new Mutable array uses the simplest form of the constructor;
|
||||
// a Mutable array
|
||||
Int[] variableArray = new Int[];
|
||||
show($"new {variableArray=}");
|
||||
|
||||
// you can specify an estimated capacity for a new Mutable array, but the
|
||||
// capacity is just an optimization hint!
|
||||
Int[] willBeGiantArray = new Int[](999);
|
||||
show($"new {willBeGiantArray=}, {willBeGiantArray.capacity=}");
|
||||
|
||||
// you can easily add and remove data from a Mutable array
|
||||
for (Int i : 10..1) {
|
||||
variableArray.add(i);
|
||||
}
|
||||
show($"NASA count-down: {variableArray=}");
|
||||
|
||||
// remove unlucky numbers in Japanese
|
||||
variableArray.remove(4);
|
||||
show($"lucky count-down: {variableArray=}");
|
||||
|
||||
// delete by index works as well
|
||||
variableArray.delete(variableArray.size-1);
|
||||
show($"aborted count-down: {variableArray=}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue