Multi-dimensional array: Difference between revisions

m
(Added Quackery.)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by one other user not shown)
Line 2,307:
=={{header|Quackery}}==
 
Quackery does not come supplied with multi-dimensional arrays. ThisPresented ishere anare two idiomatic implementationimplementations, the first using nests of nests, and the second using row major.
 
===Utility words common to both implementations===
Row or column major implementations are also possible. Quackery is not based on a contiguous static memory model.
 
When an <code>array</code> is created, each element of the array is preloaded with a datum. It could be initialised with something appropriate, such as e.g. zeros or empty strings, or <code>null</code> to indicate no value has been assigned yet.
<syntaxhighlight lang="Quackery"> [ this ] is null ( --> [ )
 
When an <code>array</code> is created, its layout needs to be specified as a nest of numbers. For the array used in this task the layout is <code>[ 5 4 3 2 ]</code>.
[ dip nested
 
Individual items in an array are also referred to with a nest of numbers, a "specifier". For the array used in this task the specifier will be four numbers, in the ranges <code>0...4</code>, <code>0...3</code>, <code>0...2</code>, and <code>0...1</code>, i.e. <code>[ 0 0 0 0 ] ... [ 4 3 2 1 ]</code>.
 
<code>[a]-></code>takes a specifier and a layout, and returns a number equal to the position of the item if the array were in row major.
 
<code>->[a]</code> is the converse operation; it takes a number and a layout, and returns a specifier.
 
To determine the number of items in an array from its layout, use <code>1 swap witheach *</code>
 
Note that in the nest-of-nests version arrays do not carry their layouts with them, so you would need to keep a note of the layout of an array somewhere if it will be required (i.e for the following example use.)
 
An example use for these would be using a <code>times</code> loop to iterate through every item of a multidimensional array without requiring nested <code>times</code> loops.
 
So <code>' [ 0 1 1 0 ] ' [ 5 4 3 2 ] [a]-></code> returns <code>8</code>, and <code>8 ' [ 5 4 3 2 ] ->[a]</code> returns <code>[ 0 1 1 0 ]</code>.
 
<syntaxhighlight lang="Quackery"> [ this ] is null is null ( --> [ )
 
[ 0 unrot
witheach
[ dip nested
[ behead rot ]
* + swap ]
drop ] is [a]-> ( [ [ --> n )
[ [] unrot
reverse
witheach
[ /mod rot
join swap ]
drop ] is ->[a] ( n [ --> [ )
</syntaxhighlight>
 
===Nest of nests===
 
<code>array</code> takes a datum with with to initialise each item in the array, and a layout, as described above, and returns an array.
 
<code>[peek]</code> takes a specifier as described above, and an array, and returns a datum.
 
<code>[poke]</code> takes a datum, an array, and a specifier, and returns an updated array, with the new datum in the specified location.
 
<syntaxhighlight lang="Quackery"> [ dip nested
reverse
witheach
Line 2,328 ⟶ 2,369:
witheach
[ dip swap poke ] ] is [poke] ( x a [ --> a )
</syntaxhighlight>
 
===Row major===
null ' [ 5 4 3 2 ] array
 
<code>array</code>, <code>[peek]</code>, and <code>[poke]</code> have the same behaviours as the nest-of-nests versions above.
 
<code>[layout]</code> takes an array and returns its layout.
 
<code>[size]</code> takes an array and returns the number of items in the array.
 
<syntaxhighlight lang="Quackery"> [ swap nested
over 1 swap
witheach *
of
2 pack ] is array ( x [ --> [ )
 
[ 0 peek ] is [layout] ( [ --> [ )
 
[ 1 peek size ] is [size] ( [ --> n )
 
[ swap unpack
unrot [a]-> peek ] is [peek] ( [ a --> x )
 
[ swap unpack
dip [ tuck [a]-> ]
rot dip
[ swap poke ]
swap 2 pack ] is [poke] ( x a [ --> a )</syntaxhighlight>
 
===Task===
 
The task code and output are the same for both versions.
 
<syntaxhighlight lang="Quackery"> null ' [ 5 4 3 2 ] array
say "[ 5 4 3 2 ] array created, initialised with nulls."
cr
Line 2,363 ⟶ 2,436:
null copied from [ 0 0 0 0 ].
2 copied from [ 3 2 1 0 ].
8 copied from [ 4 3 2 1 ].</pre>
 
</pre>
 
=={{header|Racket}}==
Line 2,683 ⟶ 2,754:
 
Otherwise what was said in the Kotlin preamble applies more or less to Wren.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
// create a 4 dimensional list of the required size and initialize successive elements to the values 1 to 120
9,476

edits