Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,5 +1,6 @@
This task is about arrays. For hashes or associative arrays, please
see [[Creating an Associative Array]].
This task is about arrays.
For hashes or associative arrays, please see [[Creating an Associative Array]].
For a definition and in-depth discussion of what an array is, see [[Array]].
In this task, the goal is to show basic array syntax in your
language. Basically, create an array, assign a value to it, and

View file

@ -1,2 +1,4 @@
---
category:
- Simple
note: Basic language learning

View file

@ -0,0 +1 @@
[val 1 2 3]

View file

@ -0,0 +1 @@
[ptr 1 2 3]

View file

@ -0,0 +1 @@
[val 1 2 3] 1 th

View file

@ -0,0 +1 @@
[val 1 2 3] 7 1 paste

View file

@ -0,0 +1 @@
[ptr 1 2 3] [ptr 7] 1 paste

View file

@ -0,0 +1 @@
[ptr 'foo' 'bar' 'baz' 'bop'] 1 3 slice

View file

@ -0,0 +1,4 @@
[ptr 1 2 3] dup
<- arlen 1 + newin dup dup ->
0 paste
[ptr 4] 3 paste

View file

@ -0,0 +1,2 @@
[ptr 1 2 3]
ar2ls (4) unshift bons

View file

@ -0,0 +1,8 @@
myArray as (int) = (1, 2, 3) // Size based on initialization
fixedArray as (int) = array(int, 1) // Given size(1 in this case)
myArray[0] = 10
myArray = myArray + fixedArray // Append arrays
print myArray[0]

View file

@ -0,0 +1,11 @@
( tbl$(mytable,100)
& 5:?(30$mytable)
& 9:?(31$mytable)
& out$(!(30$mytable))
& out$(!(-169$mytable)) { -169 mod 100 == 31 }
& out$!mytable { still index 31 }
& tbl$(mytable,0)
& (!mytable & out$"mytable still exists"
| out$"mytable is gone"
)
);

View file

@ -0,0 +1,44 @@
#include <array>
#include <vector>
// These headers are only needed for the demonstration
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
// This is a template function that works for any array-like object
template <typename Array>
void demonstrate(Array& array)
{
// Array element access
array[2] = "Three"; // Fast, but unsafe - if the index is out of bounds you
// get undefined behaviour
array.at(1) = "Two"; // *Slightly* less fast, but safe - if the index is out
// of bounds, an exception is thrown
// Arrays can be used with standard algorithms
std::reverse(begin(array), end(array));
std::for_each(begin(array), end(array),
[](typename Array::value_type const& element) // in C++14, you can just use auto
{
std::cout << element << ' ';
});
std::cout << '\n';
}
int main()
{
// Compile-time sized fixed-size array
auto fixed_size_array = std::array<std::string, 3>{ "One", "Four", "Eight" };
// If you do not supply enough elements, the remainder are default-initialized
// Dynamic array
auto dynamic_array = std::vector<std::string>{ "One", "Four" };
dynamic_array.push_back("Eight"); // Dynamically grows to accept new element
// All types of arrays can be used more or less interchangeably
demonstrate(fixed_size_array);
demonstrate(dynamic_array);
}

View file

@ -0,0 +1,36 @@
MODULE Arrays;
IMPORT
Out;
PROCEDURE Static;
VAR
x: ARRAY 5 OF LONGINT;
BEGIN
x[0] := 10;
x[1] := 11;
x[2] := 12;
x[3] := 13;
x[4] := x[0];
Out.String("Static at 4: ");Out.LongInt(x[4],0);Out.Ln;
END Static;
PROCEDURE Dynamic;
VAR
x: POINTER TO ARRAY OF LONGINT;
BEGIN
NEW(x,5);
x[0] := 10;
x[1] := 11;
x[2] := 12;
x[3] := 13;
x[4] := x[0];
Out.String("Dynamic at 4: ");Out.LongInt(x[4],0);Out.Ln;
END Dynamic;
BEGIN
Static;
Dynamic
END Arrays.

View file

@ -1,7 +1,7 @@
/*REXX program demonstrates a simple array usage. */
a.='not found' /*value for all a.xxx (so far).*/
do j=1 to 100 /*start at 1, define 100 elements*/
a.j=-j*100 /*define as negative J thousand. */
a.j=-j*1000 /*define as negative J thousand. */
end /*j*/ /*the above defines 100 elements.*/
say 'element 50 is:' a.50

View file

@ -1,5 +1,5 @@
/*REXX program demonstrates array usage with mimicry. */
a. = 'not found' /*value for all a.xxx (so far). */
a. = 00 /*value for all a.xxx (so far). */
do j=1 to 100 /*start at 1, define 100 elements*/
a.j = -j * 100 /*define element as -J hundred. */
end /*j*/ /*the above defines 100 elements.*/

View file

@ -2,7 +2,7 @@
array. = 'out of range' /*define ALL elements to this. */
do j=-3000 to 3000 /*start at -3k, going up to +3k.*/
array.j=j**2 /*define element as it's square. */
array.j=j**2 /*define element as its square. */
end /*j*/ /* [↑] defines 6,001 elements. */
g=-7
say g "squared is:" array.g

View file

@ -0,0 +1,19 @@
" Creating a dynamic array with some initial values
let array = [3, 4]
" Retrieving an element
let four = array[1]
" Modifying an element
let array[0] = 2
" Appending a new element
call add(array, 5)
" Prepending a new element
call insert(array, 1)
" Inserting a new element before another element
call insert(array, 3, 2)
echo array

View file

@ -0,0 +1,31 @@
'Example of array of 10 int types:
Dim numbers As Integer() = New Integer(0) {}
'Example of array of 4 string types:
Dim words As String() = {"hello", "world", "from", "mars"}
'You can also declare the size of the array and initialize the values at the same time:
Dim more_numbers As Integer() = New Integer(2) {21, 14, 63}
'For Multi-Dimensional arrays you declare them the same except for a comma in the type declaration.
'The following creates a 3x2 int matrix
Dim number_matrix As Integer(,) = New Integer(2, 1) {}
'As with the previous examples you can also initialize the values of the array, the only difference being each row in the matrix must be enclosed in its own braces.
Dim string_matrix As String(,) = {{"I", "swam"}, {"in", "the"}, {"freezing", "water"}}
'or
Dim funny_matrix As String(,) = New String(1, 1) {{"clowns", "are"}, {"not", "funny"}}
Dim array As Integer() = New Integer(9) {}
array(0) = 1
array(1) = 3
Console.WriteLine(array(0))
'Dynamic
Imports System
Imports System.Collections.Generic
Dim list As New List(Of Integer)()
list.Add(1)
list.Add(3)
list(0) = 2
Console.WriteLine(list(0))