Array: Difference between revisions

3,111 bytes added ,  9 years ago
jq
m (→‎{{header|REXX}}: Removed "header" template - not a task page)
(jq)
Line 59:
</lang>
 
===REXX={{header|jq}}==
jq's data types are the same as JSON's data types, and thus jq's arrays can hold any combination of JSON entities. jq arrays are indexed beginning with 0, so ["hello"][0] evaluates to "hello".
<lang rexx>/*REXX program snipetts to show a variety (types) of "array" indexing. */
 
====Immutability====
jq offers a comprehensive collection of operators and functions for array processing, but to understand them it must be appreciated that all data values in JSON are immutable. In particular, if a is an array, a jq expression such as "a[0] = 1" may give the appearance of updating the array, but in fact it simply returns an array identical to <tt>a</tt> except for the first element.
 
In fact, an expression such as "a[m] = 1" (where m is some non-negative integer) does not require that the length of the array, a, be at least (m+1). The expression should instead be interpreted to mean: produce an array derived from a such that a[m] == 1. If necessary, jq will add "null" elements to achieve this requirement. Thus, one way to create an array of m nulls is to write:<lang jq>[][m]</lang>
 
It might seem that jq must be horrendously inefficient because of immutability, but in fact, jq is fast because of compiler optimizations.
====Basic Array Operations====
Assuming a and b are arrays, and if m and n are non-negative integers:
<lang jq>
[][n] # => an array with n nulls
[range(0;n)] # => [0,1, ... (n-1)]
a | length # => the length of a
a + b # => the concatenation of a followed by b
a[n] # => the element at offset n (or null)
a[-1] # => the last element (or null)
a[m:n] # => the subarray [a[m], ..., a[n-1]]
</lang>
====Streams and Arrays====
jq provides support for streams of JSON entities, and thus the syntax for converting between an array and the stream of entities that an array contains is fundamental to jq. In brief:
<lang jq>
# If a is an array, then the expression
a[]
# will produce the stream of a's entities.
 
# Conversely, if S is a stream of entities,
# then they can be gathered together using the syntax:
[ S ]
</lang>
For example, if S is the stream (1,2,3), then [1,2,3] is the corresponding array. That is, [(1,2,3)] == [1,2,3].
 
This highlights an important point about "," in jq: the comma is not merely a syntactic marker but an operator.
 
====map/reduce====
jq's "map" filter is quite conventional, e.g. <lang jq>[1,2,3] | map(-) # => [-1, -2, -3]</lang>The <tt>reduce</tt> filter, however, has a special syntax that makes it very powerful. In brief, the syntax is:<lang jq>reduce S as $var (init; update)
</lang>where S is an expression producing a stream of values, and init and update are jq expressions. The reduction takes place by setting a hidden variable to the value of "init" initially, and then performing the update on the hidden variable as specified by "update" for each value, $var, in the stream. The result of the reduction is the final value of the hidden variable.
 
For example, here is a toy example followed by an efficient computation of n! (factorial n):<lang jq>reduce [1,2,3][] as $i (0; . + $i) # => 6
 
reduce range(2;n+1) as $i (1; . * $i) # => n!</lang>
 
=={{header|REXX}}==
<lang rexx>/*REXX program snipettssnippets to show a variety (types) of "array" indexing. */
 
/*REXX arrays aren't true arrays, but can be used (and thought) as such.*/
2,511

edits