Fixed length records: Difference between revisions

(→‎{{header|TXR}}: Forth blocks task.)
Line 727:
=={{header|jq}}==
 
To read the raw input and write out raw strings, the command-line options -R (raw input), -sr ("slurp"raw output), and -r (raw output)j options are necessary:
 
jq -RrsRrj -f program.jq infile.dat
 
program.jq:
 
Currently gojq does not include the `_nwise/1` filter provided by jq, and in any case jq's implementation is suboptimal,
For the sake of generality, we define `cut` with an argument corresponding to the number of columns. To illustrate that an efficient recursive definition is possible, we define an inner helper function, `c`, with arity 0, as currently jq's tail-call optimization is restricted to zero-arity filters:
so we define `nwise` here so that it can be used for both array and string inputs. Notice that the inner helper function, `n`, has arity 0, allowing jq's tail-call optimization to work.
 
<syntaxhighlight lang="jq">def cut($n):
def nwise($n):
def cn: if length <==0 $n then empty. else .[0:$n] , (.[$n:] | cn) end;
cn;
cutnwise(80) | explode | reverse | implode;</syntaxhighlight>
</syntaxhighlight>
To show that this does indeed work, the output of `/bin/dd cbs=80 conv=unblock` is shown:
{{output}}
<pre>
8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
 
6 eniL
cut(80) | explode | reverse | implode;</syntaxhighlight>
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL
1+1 records in
1+1 records out
644 bytes transferred in 0.030319 secs (21241 bytes/sec)
</pre>
 
=={{header|Julia}}==
2,489

edits