Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
90
Task/Arrays/8051-Assembly/arrays.8051
Normal file
90
Task/Arrays/8051-Assembly/arrays.8051
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
; constant array (elements are unchangeable) - the array is stored in the CODE segment
|
||||
myarray db 'Array' ; db = define bytes - initializes 5 bytes with values 41, 72, 72, etc. (the ascii characters A,r,r,a,y)
|
||||
myarray2 dw 'A','r','r','a','y' ; dw = define words - initializes 5 words (1 word = 2 bytes) with values 41 00 , 72 00, 72 00, etc.
|
||||
; how to read index a of the array
|
||||
push acc
|
||||
push dph
|
||||
push dpl
|
||||
mov dpl,#low(myarray) ; location of array
|
||||
mov dph,#high(myarray)
|
||||
movc a,@a+dptr ; a = element a
|
||||
mov r0, a ; r0 = element a
|
||||
pop dpl
|
||||
pop dph
|
||||
pop acc ; a = original index again
|
||||
|
||||
; array stored in internal RAM (A_START is the first register of the array, A_END is the last)
|
||||
; initalise array data (with 0's)
|
||||
push 0
|
||||
mov r0, #A_START
|
||||
clear:
|
||||
mov @r0, #0
|
||||
inc r0
|
||||
cjne r0, #A_END, clear
|
||||
pop 0
|
||||
; how to read index r1 of array
|
||||
push psw
|
||||
mov a, #A_START
|
||||
add a, r1 ; a = memory location of element r1
|
||||
push 0
|
||||
mov r0, a
|
||||
mov a, @r0 ; a = element r1
|
||||
pop 0
|
||||
pop psw
|
||||
; how to write value of acc into index r1 of array
|
||||
push psw
|
||||
push 0
|
||||
push acc
|
||||
mov a, #A_START
|
||||
add a, r1
|
||||
mov r0, a
|
||||
pop acc
|
||||
mov @r0, a ; element r1 = a
|
||||
pop 0
|
||||
pop psw
|
||||
|
||||
; array stored in external RAM (A_START is the first memory location of the array, LEN is the length)
|
||||
; initalise array data (with 0's)
|
||||
push dph
|
||||
push dpl
|
||||
push acc
|
||||
push 0
|
||||
mov dptr, #A_START
|
||||
clr a
|
||||
mov r0, #LEN
|
||||
clear:
|
||||
movx @dptr, a
|
||||
inc dptr
|
||||
djnz r0, clear
|
||||
pop 0
|
||||
pop acc
|
||||
pop dpl
|
||||
pop dph
|
||||
; how to read index r1 of array
|
||||
push dph
|
||||
push dpl
|
||||
push 0
|
||||
mov dptr, #A_START-1
|
||||
mov r0, r1
|
||||
inc r0
|
||||
loop:
|
||||
inc dptr
|
||||
djnz r0, loop
|
||||
movx a, @dptr ; a = element r1
|
||||
pop 0
|
||||
pop dpl
|
||||
pop dph
|
||||
; how to write value of acc into index r1 of array
|
||||
push dph
|
||||
push dpl
|
||||
push 0
|
||||
mov dptr, #A_START-1
|
||||
mov r0, r1
|
||||
inc r0
|
||||
loop:
|
||||
inc dptr
|
||||
djnz r0, loop
|
||||
movx @dptr, a ; element r1 = a
|
||||
pop 0
|
||||
pop dpl
|
||||
pop dph
|
||||
14
Task/Arrays/8th/arrays.8th
Normal file
14
Task/Arrays/8th/arrays.8th
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[ 1 , 2 ,3 ] \ an array holding three numbers
|
||||
1 a:@ \ this will be '2', the element at index 1
|
||||
drop
|
||||
1 123 a:@ \ this will store the value '123' at index 1, so now
|
||||
. \ will print [1,123,3]
|
||||
|
||||
[1,2,3] 45 a:push
|
||||
\ gives us [1,2,3,45]
|
||||
\ and empty spots are filled with null:
|
||||
[1,2,3] 5 15 a:!
|
||||
\ gives [1,2,3,null,15]
|
||||
|
||||
\ arrays don't have to be homogenous:
|
||||
[1,"one", 2, "two"]
|
||||
13
Task/Arrays/AntLang/arrays.antlang
Normal file
13
Task/Arrays/AntLang/arrays.antlang
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/ Create an immutable sequence (array)
|
||||
arr: <1;2;3>
|
||||
|
||||
/ Get the head an tail part
|
||||
h: head[arr]
|
||||
t: tail[arr]
|
||||
|
||||
/ Get everything except the last element and the last element
|
||||
nl: first[arr]
|
||||
l: last[arr]
|
||||
|
||||
/ Get the nth element (index origin = 0)
|
||||
nth:arr[n]
|
||||
3
Task/Arrays/Apex/arrays-1.apex
Normal file
3
Task/Arrays/Apex/arrays-1.apex
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Integer[] array = new Integer[10]; // optionally, append a braced list of Integers like "{1, 2, 3}"
|
||||
array[0] = 42;
|
||||
System.debug(array[0]); // Prints 42
|
||||
4
Task/Arrays/Apex/arrays-2.apex
Normal file
4
Task/Arrays/Apex/arrays-2.apex
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
List <Integer> aList = new List <Integer>(); // optionally add an initial size as an argument
|
||||
aList.add(5);// appends to the end of the list
|
||||
aList.add(1, 6);// assigns the element at index 1
|
||||
System.debug(list[0]); // Prints 5, alternatively you can use list.get(0)
|
||||
8
Task/Arrays/Axe/arrays.axe
Normal file
8
Task/Arrays/Axe/arrays.axe
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1→{L₁}
|
||||
2→{L₁+1}
|
||||
3→{L₁+2}
|
||||
4→{L₁+3}
|
||||
Disp {L₁}►Dec,i
|
||||
Disp {L₁+1}►Dec,i
|
||||
Disp {L₁+2}►Dec,i
|
||||
Disp {L₁+3}►Dec,i
|
||||
11
Task/Arrays/BML/arrays.bml
Normal file
11
Task/Arrays/BML/arrays.bml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
% Define an array(containing the numbers 1-3) named arr in the group $
|
||||
in $ let arr hold 1 2 3
|
||||
|
||||
% Replace the value at index 0 in array to "Index 0"
|
||||
set $arr index 0 to "Index 0"
|
||||
|
||||
% Will display "Index 0"
|
||||
display $arr index 0
|
||||
|
||||
% There is no automatic garbage collection
|
||||
delete $arr
|
||||
20
Task/Arrays/Ceylon/arrays.ceylon
Normal file
20
Task/Arrays/Ceylon/arrays.ceylon
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import ceylon.collection {
|
||||
|
||||
ArrayList
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
// you can get an array from the Array.ofSize named constructor
|
||||
value array = Array.ofSize(10, "hello");
|
||||
value a = array[3];
|
||||
print(a);
|
||||
array[4] = "goodbye";
|
||||
print(array);
|
||||
|
||||
// for a dynamic list import ceylon.collection in your module.ceylon file
|
||||
value list = ArrayList<String>();
|
||||
list.push("hello");
|
||||
list.push("hello again");
|
||||
print(list);
|
||||
}
|
||||
9
Task/Arrays/ChucK/arrays.chuck
Normal file
9
Task/Arrays/ChucK/arrays.chuck
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
int array[0]; // instantiate int array
|
||||
array << 1; // append item
|
||||
array << 2 << 3; // append items
|
||||
4 => array[3]; // assign element(4) to index(3)
|
||||
5 => array.size; // resize
|
||||
array.clear(); // clear elements
|
||||
<<<array.size()>>>; // print in cosole array size
|
||||
[1,2,3,4,5,6,7] @=> array;
|
||||
array.popBack(); // Pop last element
|
||||
31
Task/Arrays/Computer-zero-Assembly/arrays-1.computer
Normal file
31
Task/Arrays/Computer-zero-Assembly/arrays-1.computer
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
load: LDA ary
|
||||
ADD sum
|
||||
STA sum
|
||||
|
||||
LDA load
|
||||
ADD one
|
||||
STA load
|
||||
|
||||
SUB end
|
||||
BRZ done
|
||||
|
||||
JMP load
|
||||
|
||||
done: LDA sum
|
||||
STP
|
||||
|
||||
one: 1
|
||||
end: LDA ary+10
|
||||
|
||||
sum: 0
|
||||
|
||||
ary: 1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
30
Task/Arrays/Computer-zero-Assembly/arrays-2.computer
Normal file
30
Task/Arrays/Computer-zero-Assembly/arrays-2.computer
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
load: LDA ary
|
||||
BRZ done
|
||||
|
||||
ADD sum
|
||||
STA sum
|
||||
|
||||
LDA load
|
||||
ADD one
|
||||
STA load
|
||||
|
||||
JMP load
|
||||
|
||||
done: LDA sum
|
||||
STP
|
||||
|
||||
one: 1
|
||||
|
||||
sum: 0
|
||||
|
||||
ary: 1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
0
|
||||
82
Task/Arrays/FreeBASIC/arrays.freebasic
Normal file
82
Task/Arrays/FreeBASIC/arrays.freebasic
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
' compile with: FBC -s console.
|
||||
' compile with: FBC -s console -exx to have boundary checks.
|
||||
|
||||
Dim As Integer a(5) ' from s(0) to s(5)
|
||||
Dim As Integer num = 1
|
||||
Dim As String s(-num To num) ' s1(-1), s1(0) and s1(1)
|
||||
|
||||
Static As UByte c(5) ' create a array with 6 elements (0 to 5)
|
||||
|
||||
'dimension array and initializing it with Data
|
||||
Dim d(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}
|
||||
Print " The first dimension has a lower bound of"; LBound(d);_
|
||||
" and a upper bound of"; UBound(d)
|
||||
Print " The second dimension has a lower bound of"; LBound(d,2);_
|
||||
" and a upper bound of"; UBound(d,2)
|
||||
Print : Print
|
||||
|
||||
Dim Shared As UByte u(0 To 3) ' make a shared array of UByte with 4 elements
|
||||
|
||||
Dim As UInteger pow() ' make a variable length array
|
||||
' you must Dim the array before you can use ReDim
|
||||
ReDim pow(num) ' pow now has 1 element
|
||||
pow(num) = 10 ' lets fill it with 10 and print it
|
||||
Print " The value of pow(num) = "; pow(num)
|
||||
|
||||
ReDim pow(10) ' make pow a 10 element array
|
||||
Print
|
||||
Print " Pow now has"; UBound(pow) - LBound(pow) +1; " elements"
|
||||
' the value of pow(num) is gone now
|
||||
Print " The value of pow(num) = "; pow(num); ", should be 0"
|
||||
|
||||
Print
|
||||
For i As Integer = LBound(pow) To UBound(pow)
|
||||
pow(i) = i * i
|
||||
Print pow(i),
|
||||
Next
|
||||
Print:Print
|
||||
|
||||
ReDim Preserve pow(3 To 7)
|
||||
' the first five elements will be preserved, not elements 3 to 7
|
||||
Print
|
||||
Print " The lower bound is now"; LBound(pow);_
|
||||
" and the upper bound is"; UBound(pow)
|
||||
Print " Pow now has"; UBound(pow) - LBound(pow) +1; " elements"
|
||||
Print
|
||||
For i As Integer = LBound(pow) To UBound(pow)
|
||||
Print pow(i),
|
||||
Next
|
||||
Print : Print
|
||||
|
||||
'erase the variable length array
|
||||
Erase pow
|
||||
Print " The lower bound is now"; LBound(pow);_
|
||||
" and the upper bound is "; UBound(pow)
|
||||
Print " If the lower bound is 0 and the upper bound is -1 it means,"
|
||||
Print " that the array has no elements, it's completely removed"
|
||||
Print : Print
|
||||
|
||||
'erase the fixed length array
|
||||
Print " Display the contents of the array d"
|
||||
For i As Integer = 1 To 2 : For j As Integer = 1 To 5
|
||||
Print d(i,j);" ";
|
||||
Next : Next : Print : Print
|
||||
|
||||
Erase d
|
||||
Print " We have erased array d"
|
||||
Print " The first dimension has a lower bound of"; LBound(d);_
|
||||
" and a upper bound of"; UBound(d)
|
||||
Print " The second dimension has a lower bound of"; LBound(d,2);_
|
||||
" and a upper bound of"; UBound(d,2)
|
||||
Print
|
||||
For i As Integer = 1 To 2 : For j As Integer = 1 To 5
|
||||
Print d(i,j);" ";
|
||||
Next : Next
|
||||
Print
|
||||
Print " The elements self are left untouched but there content is set to 0"
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
1
Task/Arrays/Futhark/arrays-1.futhark
Normal file
1
Task/Arrays/Futhark/arrays-1.futhark
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3]
|
||||
2
Task/Arrays/Futhark/arrays-2.futhark
Normal file
2
Task/Arrays/Futhark/arrays-2.futhark
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
replicate 5 3 == [3,3,3,3,3]
|
||||
iota 5 = [0,1,2,3,4]
|
||||
3
Task/Arrays/Futhark/arrays-3.futhark
Normal file
3
Task/Arrays/Futhark/arrays-3.futhark
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fun update(as: *[]int, i: int, x: int): []int =
|
||||
let as[i] = x
|
||||
in x
|
||||
11
Task/Arrays/Harbour/arrays-1.harbour
Normal file
11
Task/Arrays/Harbour/arrays-1.harbour
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Declare and initialize two-dimensional array
|
||||
local arr1 := { { "NITEM", "N", 10, 0 }, { "CONTENT", "C", 60, 0 } }
|
||||
// Create an empty array
|
||||
local arr2 := {}
|
||||
// Declare three-dimensional array
|
||||
local arr3[ 2, 100, 3 ]
|
||||
// Create an array
|
||||
local arr4 := Array( 50 )
|
||||
|
||||
// Array can be dynamically resized:
|
||||
arr4 := ASize( arr4, 80 )
|
||||
6
Task/Arrays/Harbour/arrays-2.harbour
Normal file
6
Task/Arrays/Harbour/arrays-2.harbour
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Adding new item to array, its size is incremented
|
||||
AAdd( arr1, { "LBASE", "L", 1, 0 } )
|
||||
// Delete the first item of arr3, The size of arr3 remains the same, all items are shifted to one position, the last item is replaced by Nil:
|
||||
ADel( arr1, 1 )
|
||||
// Assigning a value to array item
|
||||
arr3[ 1, 1, 1 ] := 11.4
|
||||
2
Task/Arrays/Harbour/arrays-3.harbour
Normal file
2
Task/Arrays/Harbour/arrays-3.harbour
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
x := arr3[ 1, 10, 2 ]
|
||||
// The retrieved item can be nested array, in this case it isn't copied, the pointer to it is assigned
|
||||
7
Task/Arrays/Harbour/arrays-4.harbour
Normal file
7
Task/Arrays/Harbour/arrays-4.harbour
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// Fill the 20 items of array with 0, starting from 5-th item:
|
||||
AFill( arr4, 0, 5, 20 )
|
||||
// Copy 10 items from arr4 to arr3[ 2 ], starting from the first position:
|
||||
ACopy( arr4, arr3[ 2 ], 1, 10 )
|
||||
// Duplicate the whole or nested array:
|
||||
arr5 := AClone( arr1 )
|
||||
arr6 := AClone( arr1[ 3 ] )
|
||||
9
Task/Arrays/I/arrays-1.i
Normal file
9
Task/Arrays/I/arrays-1.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
software {
|
||||
var a = []
|
||||
a += 2
|
||||
print(a[0]) //Outputs 2
|
||||
|
||||
a[0] = 4
|
||||
|
||||
print(a[0]) //Outputs 4
|
||||
}
|
||||
98
Task/Arrays/I/arrays-2.i
Normal file
98
Task/Arrays/I/arrays-2.i
Normal 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
|
||||
3
Task/Arrays/I/arrays-3.i
Normal file
3
Task/Arrays/I/arrays-3.i
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Unicon provides a number of extensions
|
||||
# insert and delete work on lists allowing changes in the middle
|
||||
# possibly others
|
||||
51
Task/Arrays/LFE/arrays.lfe
Normal file
51
Task/Arrays/LFE/arrays.lfe
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
; Create a fixed-size array with entries 0-9 set to 'undefined'
|
||||
> (set a0 (: array new 10))
|
||||
#(array 10 0 undefined 10)
|
||||
> (: array size a0)
|
||||
10
|
||||
|
||||
; Create an extendible array and set entry 17 to 'true',
|
||||
; causing the array to grow automatically
|
||||
> (set a1 (: array set 17 'true (: array new)))
|
||||
#(array
|
||||
18
|
||||
...
|
||||
(: array size a1)
|
||||
18
|
||||
|
||||
; Read back a stored value
|
||||
> (: array get 17 a1)
|
||||
true
|
||||
|
||||
; Accessing an unset entry returns the default value
|
||||
> (: array get 3 a1)
|
||||
undefined
|
||||
|
||||
; Accessing an entry beyond the last set entry also returns the
|
||||
; default value, if the array does not have fixed size
|
||||
> (: array get 18 a1)
|
||||
undefined
|
||||
|
||||
; "sparse" functions ignore default-valued entries
|
||||
> (set a2 (: array set 4 'false a1))
|
||||
#(array
|
||||
18
|
||||
...
|
||||
> (: array sparse_to_orddict a2)
|
||||
(#(4 false) #(17 true))
|
||||
|
||||
; An extendible array can be made fixed-size later
|
||||
> (set a3 (: array fix a2))
|
||||
#(array
|
||||
18
|
||||
...
|
||||
|
||||
; A fixed-size array does not grow automatically and does not
|
||||
; allow accesses beyond the last set entry
|
||||
> (: array set 18 'true a3)
|
||||
exception error: badarg
|
||||
in (array set 3)
|
||||
|
||||
> (: array get 18 a3)
|
||||
exception error: badarg
|
||||
in (array get 2)
|
||||
33
Task/Arrays/Lasso/arrays-1.lasso
Normal file
33
Task/Arrays/Lasso/arrays-1.lasso
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// Create a new empty array
|
||||
local(array1) = array
|
||||
|
||||
// Create an array with 2 members (#myarray->size is 2)
|
||||
local(array1) = array('ItemA','ItemB')
|
||||
|
||||
// Assign a value to member [2]
|
||||
#array1->get(2) = 5
|
||||
|
||||
// Retrieve a value from an array
|
||||
#array1->get(2) + #array1->size // 8
|
||||
|
||||
// Merge arrays
|
||||
local(
|
||||
array1 = array('a','b','c'),
|
||||
array2 = array('a','b','c')
|
||||
)
|
||||
#array1->merge(#array2) // a, b, c, a, b, c
|
||||
|
||||
// Sort an array
|
||||
#array1->sort // a, a, b, b, c, c
|
||||
|
||||
// Remove value by index
|
||||
#array1->remove(2) // a, b, b, c, c
|
||||
|
||||
// Remove matching items
|
||||
#array1->removeall('b') // a, c, c
|
||||
|
||||
// Insert item
|
||||
#array1->insert('z') // a, c, c, z
|
||||
|
||||
// Insert item at specific position
|
||||
#array1->insert('0',1) // 0, a, c, c, z
|
||||
11
Task/Arrays/Lasso/arrays-2.lasso
Normal file
11
Task/Arrays/Lasso/arrays-2.lasso
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Create a staticarray containing 5 items
|
||||
local(mystaticArray) = staticarray('a','b','c','d','e')
|
||||
|
||||
// Retreive an item
|
||||
#mystaticArray->get(3) // c
|
||||
|
||||
// Set an item
|
||||
#mystaticArray->get(3) = 'changed' // a, b, changed, d, e
|
||||
|
||||
// Create an empty static array with a length of 32
|
||||
local(mystaticArray) = staticarray_join(32,void)
|
||||
15
Task/Arrays/Lingo/arrays-1.lingo
Normal file
15
Task/Arrays/Lingo/arrays-1.lingo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
a = [1,2] -- or: a = list(1,2)
|
||||
put a[2] -- or: put a.getAt(2)
|
||||
-- 2
|
||||
a.append(3)
|
||||
put a
|
||||
-- [1, 2, 3]
|
||||
a.deleteAt(2)
|
||||
put a
|
||||
-- [1, 3]
|
||||
a[1] = 5 -- or: a.setAt(1, 5)
|
||||
put a
|
||||
-- [5, 3]
|
||||
a.sort()
|
||||
put a
|
||||
-- [3, 5]
|
||||
11
Task/Arrays/Lingo/arrays-2.lingo
Normal file
11
Task/Arrays/Lingo/arrays-2.lingo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
ba = bytearray(2, 255) -- initialized with size 2 and filled with 0xff
|
||||
put ba
|
||||
-- <ByteArrayObject length = 2 ByteArray = 0xff, 0xff >
|
||||
ba[1] = 1
|
||||
ba[2] = 2
|
||||
ba[ba.length+1] = 3 -- dynamically increases size
|
||||
put ba
|
||||
-- <ByteArrayObject length = 3 ByteArray = 0x1, 0x2, 0x3 >
|
||||
ba[1] = 5
|
||||
put ba
|
||||
-- <ByteArrayObject length = 3 ByteArray = 0x5, 0x2, 0x3 >
|
||||
1
Task/Arrays/Monte/arrays-1.monte
Normal file
1
Task/Arrays/Monte/arrays-1.monte
Normal file
|
|
@ -0,0 +1 @@
|
|||
var myArray := ['a', 'b', 'c','d']
|
||||
1
Task/Arrays/Monte/arrays-2.monte
Normal file
1
Task/Arrays/Monte/arrays-2.monte
Normal file
|
|
@ -0,0 +1 @@
|
|||
traceln(myArray[0])
|
||||
1
Task/Arrays/Monte/arrays-3.monte
Normal file
1
Task/Arrays/Monte/arrays-3.monte
Normal file
|
|
@ -0,0 +1 @@
|
|||
myArray := myArray.with(3, 'z')
|
||||
22
Task/Arrays/Nim/arrays.nim
Normal file
22
Task/Arrays/Nim/arrays.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
var # fixed size arrays
|
||||
x = [1,2,3,4,5,6,7,8,9,10] # type and size automatically inferred
|
||||
y: array[1..5, int] = [1,2,3,4,5] # starts at 1 instead of 0
|
||||
z: array['a'..'z', int] # indexed using characters
|
||||
|
||||
x[0] = x[1] + 1
|
||||
echo x[0]
|
||||
echo z['d']
|
||||
|
||||
x[7..9] = y[3..5] # copy part of array
|
||||
|
||||
var # variable size sequences
|
||||
a = @[1,2,3,4,5,6,7,8,9,10]
|
||||
b: seq[int] = @[1,2,3,4,5]
|
||||
|
||||
a[0] = a[1] + 1
|
||||
echo a[0]
|
||||
|
||||
a.add(b) # append another sequence
|
||||
a.add(200) # append another element
|
||||
echo a.pop() # pop last item, removing and returning it
|
||||
echo a
|
||||
3
Task/Arrays/Oforth/arrays.oforth
Normal file
3
Task/Arrays/Oforth/arrays.oforth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[ "abd", "def", "ghi" ] at(3) println
|
||||
|
||||
ListBuffer new dup addAll([1, 2, 3]) dup put(2, 8.1) println
|
||||
74
Task/Arrays/Phix/arrays.phix
Normal file
74
Task/Arrays/Phix/arrays.phix
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
-- simple one-dimensional arrays:
|
||||
sequence s1 = {0.5, 1, 4.7, 9}, -- length(s1) is now 4
|
||||
s2 = repeat(0,6), -- s2 is {0,0,0,0,0,0}
|
||||
s3 = tagset(5) -- s3 is {1,2,3,4,5}
|
||||
|
||||
?s1[3] -- displays 4.7 (nb 1-based indexing)
|
||||
s1[3] = 0 -- replace that 4.7
|
||||
s1 &= {5,6} -- length(s1) is now 6 ({0.5,1,0,9,5,6})
|
||||
s1 = s1[2..5] -- length(s1) is now 4 ({1,0,9,5})
|
||||
s1[2..3] = {2,3,4} -- length(s1) is now 5 ({1,2,3,4,5})
|
||||
s1 = append(s1,6) -- length(s1) is now 6 ({1,2,3,4,5,6})
|
||||
s1 = prepend(s1,0) -- length(s1) is now 7 ({0,1,2,3,4,5,6})
|
||||
|
||||
-- negative subscripts can also be used, counting from the other end, eg
|
||||
s2[-2..-1] = {-2,-1} -- s2 is now {0,0,0,0,-2,-1}
|
||||
|
||||
-- multi dimensional arrays:
|
||||
sequence y = {{{1,1},{3,3},{5,5}},
|
||||
{{0,0},{0,1},{9,1}},
|
||||
{{1,7},{1,1},{2,2}}}
|
||||
-- y[2][3][1] is 9
|
||||
|
||||
y = repeat(repeat(repeat(0,2),3),3)
|
||||
-- same structure, but all 0s
|
||||
|
||||
-- Array of strings:
|
||||
sequence s = {"Hello", "World", "Phix", "", "Last One"}
|
||||
-- s[3] is "Phix"
|
||||
-- s[3][2] is 'h'
|
||||
|
||||
-- A Structure:
|
||||
sequence employee = {{"John","Smith"},
|
||||
45000,
|
||||
27,
|
||||
185.5}
|
||||
|
||||
-- To simplify access to elements within a structure it is good programming style to define constants that name the various fields, eg:
|
||||
constant SALARY = 2
|
||||
|
||||
-- Array of structures:
|
||||
sequence employees = {
|
||||
{{"Jane","Adams"}, 47000, 34, 135.5}, -- a[1]
|
||||
{{"Bill","Jones"}, 57000, 48, 177.2}, -- a[2]
|
||||
-- .... etc.
|
||||
}
|
||||
-- employees[2][SALARY] is 57000
|
||||
|
||||
-- A tree can be represented easily, for example after adding "b","c","a" to it you might have:
|
||||
sequence tree = {{"b",3,2},
|
||||
{"c",0,0},
|
||||
{"a",0,0}}
|
||||
|
||||
-- ie assuming
|
||||
constant ROOT=1, VALUE=1, LEFT=2, RIGHT=3 -- then
|
||||
-- tree[ROOT][VALUE] is "b"
|
||||
-- tree[ROOT][LEFT] is 3, and tree[3] is the "a"
|
||||
-- tree[ROOT][RIGHT] is 2, and tree[2] is the "c"
|
||||
|
||||
-- The operations you might use to build such a tree (tests/loops/etc omitted) could be:
|
||||
tree = {}
|
||||
tree = append(tree,{"b",0,0})
|
||||
tree = append(tree,{"c",0,0})
|
||||
tree[1][RIGHT] = length(tree)
|
||||
tree = append(tree,{"a",0,0})
|
||||
tree[1][LEFT] = length(tree)
|
||||
|
||||
-- Finally, some tests (recall that we have already output a 4.7):
|
||||
?s[3]
|
||||
?tree
|
||||
?tree[ROOT][VALUE]
|
||||
employees = append(employees, employee)
|
||||
?employees[3][SALARY]
|
||||
?s1
|
||||
?s2
|
||||
13
Task/Arrays/Pony/arrays.pony
Normal file
13
Task/Arrays/Pony/arrays.pony
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
let x = numbers(0) // fetch the first element of array. index starts at 0
|
||||
Fact(x == 10) // try block is needed, because both lines inside it can throw exception
|
||||
end
|
||||
|
||||
var other: Array[U64] = [10, 20, 30] // array literal
|
||||
let s = other.size() // return the number of elements in array
|
||||
try
|
||||
Fact(s == 3) // size of array 'other' is 3
|
||||
other(1) = 40 // 'other' now is [10, 40, 30]
|
||||
end
|
||||
11
Task/Arrays/Ring/arrays.ring
Normal file
11
Task/Arrays/Ring/arrays.ring
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# create an array with one string in it
|
||||
a = ['foo']
|
||||
|
||||
# add items
|
||||
a + 1 # ["foo", 1]
|
||||
|
||||
# set the value at a specific index in the array
|
||||
a[1] = 2 # [2, 1]
|
||||
|
||||
# retrieve an element
|
||||
see a[1]
|
||||
27
Task/Arrays/SSEM/arrays.ssem
Normal file
27
Task/Arrays/SSEM/arrays.ssem
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
10101000000000100000000000000000 0. -21 to c
|
||||
11101000000000010000000000000000 1. Sub. 23
|
||||
00101000000001100000000000000000 2. c to 20
|
||||
00101000000000100000000000000000 3. -20 to c
|
||||
10101000000001100000000000000000 4. c to 21
|
||||
10000000000000100000000000000000 5. -1 to c
|
||||
01001000000000010000000000000000 6. Sub. 18
|
||||
00101000000001100000000000000000 7. c to 20
|
||||
00101000000000100000000000000000 8. -20 to c
|
||||
10000000000001100000000000000000 9. c to 1
|
||||
01101000000000010000000000000000 10. Sub. 22
|
||||
00000000000000110000000000000000 11. Test
|
||||
01001000000001000000000000000000 12. Add 18 to CI
|
||||
11001000000000000000000000000000 13. 19 to CI
|
||||
10101000000000100000000000000000 14. -21 to c
|
||||
00101000000001100000000000000000 15. c to 20
|
||||
00101000000000100000000000000000 16. -20 to c
|
||||
00000000000001110000000000000000 17. Stop
|
||||
10000000000000000000000000000000 18. 1
|
||||
11111111111111111111111111111111 19. -1
|
||||
00000000000000000000000000000000 20. 0
|
||||
00000000000000000000000000000000 21. 0
|
||||
11011000000000010000000000000000 22. Sub. 27
|
||||
10000000000000000000000000000000 23. 1
|
||||
01000000000000000000000000000000 24. 2
|
||||
11000000000000000000000000000000 25. 3
|
||||
00100000000000000000000000000000 26. 4
|
||||
29
Task/Arrays/Sidef/arrays.sidef
Normal file
29
Task/Arrays/Sidef/arrays.sidef
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# create an empty array
|
||||
var arr = [];
|
||||
|
||||
# push objects into the array
|
||||
arr << "a"; #: ['a']
|
||||
arr.append(1,2,3); #: ['a', 1, 2, 3]
|
||||
|
||||
# change an element inside the array
|
||||
arr[2] = "b"; #: ['a', 1, 'b', 3]
|
||||
|
||||
# set the value at a specific index in the array (with autovivification)
|
||||
arr[5] = "end"; #: ['a', 1, 'b', 3, nil, 'end']
|
||||
|
||||
# resize the array
|
||||
arr.resize_to(-1); #: []
|
||||
|
||||
# slice assignment
|
||||
arr[0..2] = @|('a'..'c'); #: ['a', 'b', 'c']
|
||||
|
||||
# indices as arrays
|
||||
var indices = [0, -1];
|
||||
arr[indices] = ("foo", "baz"); #: ['foo', 'b', 'baz']
|
||||
|
||||
# retrieve multiple elements
|
||||
var *elems = arr[0, -1]
|
||||
say elems #=> ['foo', 'baz']
|
||||
|
||||
# retrieve an element
|
||||
say arr[-1]; #=> 'baz'
|
||||
6
Task/Arrays/Swift/arrays.swift
Normal file
6
Task/Arrays/Swift/arrays.swift
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Arrays are typed in Swift, however, using the Any object we can add any type. Swift does not support fixed length arrays
|
||||
var anyArray = [Any]()
|
||||
anyArray.append("foo") // Adding to an Array
|
||||
anyArray.append(1) // ["foo", 1]
|
||||
anyArray.removeAtIndex(1) // Remove object
|
||||
anyArray[0] = "bar" // ["bar"]]
|
||||
14
Task/Arrays/Wren/arrays.wren
Normal file
14
Task/Arrays/Wren/arrays.wren
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var arr = []
|
||||
arr.add(1)
|
||||
arr.add(2)
|
||||
arr.count // 2
|
||||
arr.clear()
|
||||
|
||||
arr.add(0)
|
||||
arr.add(arr[0])
|
||||
arr.add(1)
|
||||
arr.add(arr[-1]) // [0, 0, 1, 1]
|
||||
|
||||
arr[-1] = 0
|
||||
arr.insert(-1, 0) // [0, 0, 1, 0, 0]
|
||||
arr.removeAt(2) // [0, 0, 0, 0]
|
||||
24
Task/Arrays/XLISP/arrays.xlisp
Normal file
24
Task/Arrays/XLISP/arrays.xlisp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
[1] (define a (make-vector 10)) ; vector of 10 elements initialized to the empty list
|
||||
|
||||
A
|
||||
[2] (define b (make-vector 10 5)) ; vector of 10 elements initialized to 5
|
||||
|
||||
B
|
||||
[3] (define c #(1 2 3 4 5 6 7 8 9 10)) ; vector literal
|
||||
|
||||
C
|
||||
[4] (vector-ref c 3) ; retrieve a value -- NB. indexed from 0
|
||||
|
||||
4
|
||||
[5] (vector-set! a 5 1) ; set a_5 to 1
|
||||
|
||||
1
|
||||
[6] (define d (make-array 5 6 7)) ; 3-dimensional array of size 5 by 6 by 7
|
||||
|
||||
D
|
||||
[7] (array-set! d 1 2 3 10) ; set d_1,2,3 to 10 -- NB. still indexed from 0
|
||||
|
||||
10
|
||||
[8] (array-ref d 1 2 3) ; and get the value of d_1,2,3
|
||||
|
||||
10
|
||||
36
Task/Arrays/jq/arrays.jq
Normal file
36
Task/Arrays/jq/arrays.jq
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# Create a new array with length 0
|
||||
[]
|
||||
|
||||
# Create a new array of 5 nulls
|
||||
[][4] = null # setting the element at offset 4 expands the array
|
||||
|
||||
# Create an array having the elements 1 and 2 in that order
|
||||
[1,2]
|
||||
|
||||
# Create an array of integers from 0 to 10 inclusive
|
||||
[ range(0; 11) ]
|
||||
|
||||
# If a is an array (of any length), update it so that a[2] is 5
|
||||
a[2] = 5;
|
||||
|
||||
# Append arrays a and b
|
||||
a + b
|
||||
|
||||
# Append an element, e, to an array a
|
||||
a + [e]
|
||||
|
||||
##################################################
|
||||
# In the following, a is assumed to be [0,1,2,3,4]
|
||||
|
||||
# It is not an error to use an out-of-range index:
|
||||
a[10] # => null
|
||||
|
||||
# Negative indices count backwards from after the last element:
|
||||
a[-1] # => 4
|
||||
|
||||
# jq supports simple slice operations but
|
||||
# only in the forward direction:
|
||||
a[:1] # => [0]
|
||||
a[1:] # => [1,2,3,4]
|
||||
a[2:4] # => [2,3]
|
||||
a[4:2] # null
|
||||
Loading…
Add table
Add a link
Reference in a new issue