June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,29 +1,35 @@
procedure TForm1.Button1Click(Sender: TObject);
var
StaticArray: array[0..9] of Integer;
DynamicArray: array of Integer;
StaticArray: array[1..10] of Integer; // static arrays can start at any index
DynamicArray: array of Integer; // dynamic arrays always start at 0
StaticArrayText,
DynamicArrayText: string;
lcv: Integer;
ixS, ixD: Integer;
begin
// Setting the length of the dynamic array the same as the static one
SetLength(DynamicArray, Length(StaticArray));
// Asking random numbers storing into the static array
for lcv := 0 to Pred(Length(StaticArray)) do
for ixS := Low(StaticArray) to High(StaticArray) do
begin
StaticArray[lcv] := StrToInt(
StaticArray[ixS] := StrToInt(
InputBox('Random number',
'Enter a random number for position',
IntToStr(Succ(lcv))));
IntToStr(ixS)));
end;
// Storing entered numbers of the static array in reverse order into the dynamic
for lcv := 0 to Pred(Length(StaticArray)) do
DynamicArray[Pred(Length(DynamicArray)) - lcv] := StaticArray[lcv];
// Concatenating the static and dynamic array into a single string variable
for lcv := 0 to Pred(Length(StaticArray)) do
ixD := High(DynamicArray);
for ixS := Low(StaticArray) to High(StaticArray) do
begin
StaticArrayText := StaticArrayText + IntToStr(StaticArray[lcv]);
DynamicArrayText := DynamicArrayText + IntToStr(DynamicArray[lcv]);
DynamicArray[ixD] := StaticArray[ixS];
Dec(ixD);
end;
// Concatenating the static and dynamic array into a single string variable
StaticArrayText := '';
for ixS := Low(StaticArray) to High(StaticArray) do
StaticArrayText := StaticArrayText + IntToStr(StaticArray[ixS]);
DynamicArrayText := '';
for ixD := Low(DynamicArray) to High(DynamicArray) do
DynamicArrayText := DynamicArrayText + IntToStr(DynamicArray[ixD]);
end;
// Displaying both arrays (#13#10 = Carriage Return/Line Feed)
ShowMessage(StaticArrayText + #13#10 + DynamicArrayText);

View file

@ -1,4 +1,4 @@
int[] aStackAllocatedArray(3).
V<int,3> aStackAllocatedArray.
aStackAllocatedArray[0] := 1.
aStackAllocatedArray[1] := 2.
aStackAllocatedArray[2] := 3.

View file

@ -1,6 +1,6 @@
var aDynamicArray := ArrayList new.
aDynamicArray += 1.
aDynamicArray += 2.
aDynamicArray += 4.
aDynamicArray append:1.
aDynamicArray append:2.
aDynamicArray append:4.
aDynamicArray[2] := 3.

View file

@ -0,0 +1,8 @@
// Create an array of fixed size
U8 array[10] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
// The first element of a HolyC array is indexed at 0. To set a value:
array[0] = 123;
// Access an element
Print("%d\n", array[0]);

View file

@ -0,0 +1,98 @@
record aThing(a, b, c) # arbitrary object (record or class) for illustration
procedure main()
A0 := [] # empty list
A0 := list() # empty list (default size 0)
A0 := list(0) # empty list (literal size 0)
A1 := list(10) # 10 elements, default initializer &null
A2 := list(10, 1) # 10 elements, initialized to 1
# literal array construction - arbitrary dynamically typed members
A3 := [1, 2, 3, ["foo", "bar", "baz"], aThing(1, 2, 3), "the end"]
# left-end workers
# NOTE: get() is a synonym for pop() which allows nicely-worded use of put() and get() to implement queues
#
Q := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x := pop(A0) # x is 1
x := get(A0) # x is 2
push(Q,0)
# Q is now [0,3, 4, 5, 6, 7, 8, 9, 10]
# right-end workers
x := pull(Q) # x is 10
put(Q, 100) # Q is now [0, 3, 4, 5, 6, 7, 8, 9, 100]
# push and put return the list they are building
# they also can have multiple arguments which work like repeated calls
Q2 := put([],1,2,3) # Q2 is [1,2,3]
Q3 := push([],1,2,3) # Q3 is [3,2,1]
Q4 := push(put(Q2),4),0] # Q4 is [0,1,2,3,4] and so is Q2
# array access follows with A as the sample array
A := [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# get element indexed from left
x := A[1] # x is 10
x := A[2] # x is 20
x := A[10] # x is 100
# get element indexed from right
x := A[-1] # x is 100
x := A[-2] # x is 90
x := A[-10] # x is 10
# copy array to show assignment to elements
B := copy(A)
# assign element indexed from left
B[1] := 11
B[2] := 21
B[10] := 101
# B is now [11, 21, 30, 50, 60, 60, 70, 80, 90, 101]
# assign element indexed from right - see below
B[-1] := 102
B[-2] := 92
B[-10] := 12
# B is now [12, 21, 30, 50, 60, 60, 70, 80, 92, 102]
# list slicing
# the unusual nature of the slice - returning 1 less element than might be expected
# in many languages - is best understood if you imagine indexes as pointing to BEFORE
# the item of interest. When a slice is made, the elements between the two points are
# collected. eg in the A[3 : 6] sample, it will get the elements between the [ ] marks
#
# sample list: 10 20 [30 40 50] 60 70 80 90 100
# positive indexes: 1 2 3 4 5 6 7 8 9 10 11
# non-positive indexes: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
#
# I have deliberately drawn the indexes between the positions of the values.
# The nature of this indexing brings simplicity to string operations
#
# list slicing can also use non-positive indexes to access values from the right.
# The final index of 0 shown above shows how the end of the list can be nominated
# without having to know it's length
#
# NOTE: list slices are distinct lists, so assigning to the slice
# or a member of the slice does not change the values in A
#
# Another key fact to understand: once the non-positive indexes and length-offsets are
# resolved to a simple positive index, the index pair (if two are given) are swapped
# if necessary to yield the elements between the two.
#
S := A[3 : 6] # S is [30, 40, 50]
S := A[6 : 3] # S is [30, 40, 50] not illegal or erroneous
S := A[-5 : -8] # S is [30, 40, 50]
S := A[-8 : -5] # S is [30, 40, 50] also legal and meaningful
# list slicing with length request
S := A[3 +: 3] # S is [30, 40, 50]
S := A[6 -: 3] # S is [30, 40, 50]
S := A[-8 +: 3] # S is [30, 40, 50]
S := A[-5 -: 3] # S is [30, 40, 50]
S := A[-8 -: -3] # S is [30, 40, 50]
S := A[-5 +: -3] # S is [30, 40, 50]
end

View file

@ -0,0 +1,3 @@
# Unicon provides a number of extensions
# insert and delete work on lists allowing changes in the middle
# possibly others

View file

@ -1,3 +1,3 @@
[ "abd", "def", "ghi" ] at(3) println
[ "abd", "def", "ghi" ] at( 3 ) .
ListBuffer new dup addAll([1, 2, 3]) dup put(2, 8.1) println
Array new dup addAll( [1, 2, 3] ) dup put( 2, 8.1 ) .

View file

@ -1,3 +1,7 @@
use "assert" // due to the use of Fact
- - -
var numbers = Array[I32](16) // creating array of 32-bit ints with initial allocation for 16 elements
numbers.push(10) // add value 10 to the end of array, extending the underlying memory if needed
try

View file

@ -0,0 +1,14 @@
arr1: [] ;create empty array
arr2: ["apple" "orange" 1 2 3] ;create an array with data
>> insert arr1 "blue"
>> arr1
== ["blue"]
append append arr1 "black" "green"
>> arr1
== ["blue" "black" "green"]
>> arr1/2
== "black"
>> second arr1
== "black"
>> pick arr1 2
== "black"

View file

@ -0,0 +1,16 @@
>> vec1: make vector! [ 20 30 70]
== make vector! [20 30 70]
>> vec1/2
== 30
>> second vec1
== 30
>> append vec1 90
== make vector! [20 30 70 90]
>> append vec1 "string"
*** Script Error: invalid argument: "string"
*** Where: append
*** Stack:
>> append vec1 3.0
*** Script Error: invalid argument: 3.0
*** Where: append
*** Stack: