User defined pipe and redirection operators: Difference between revisions

Content added Content deleted
(J: implement most (perhaps all) of the required complexities)
Line 56: Line 56:
As for the concept of a pipe that presents data one record at a time to a downstream function, that corresponds to the J operator <code>@</code> and we could achieve the "left to right" syntax mechanism by explicitly ordering its arguments <code>2 :'v@u'</code> but it's not clear how to demonstrate that usefully, in this task. (And, I could write a lot of code, to accomplish what's being accomplished here with the two successive greps, but I find that concept distasteful and tedious.)
As for the concept of a pipe that presents data one record at a time to a downstream function, that corresponds to the J operator <code>@</code> and we could achieve the "left to right" syntax mechanism by explicitly ordering its arguments <code>2 :'v@u'</code> but it's not clear how to demonstrate that usefully, in this task. (And, I could write a lot of code, to accomplish what's being accomplished here with the two successive greps, but I find that concept distasteful and tedious.)


That said, note also that J's sort (<code>/:~</code>) and uniq (<code>~.</code>) operations would work just fine on this kind of data. For example:
However, note also that J's sort (<code>/:~</code>) and uniq (<code>~.</code>) operations would work just fine on this kind of data. For example:


<lang j> ;'aa' grep 'ALGOL' grep data,data
<lang j> ;'aa' grep 'ALGOL' grep data,data
Line 65: Line 65:
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
</lang>
</lang>

That said, this implements most (perhaps all) of the required complexities:

<lang j>declare=: erase@boxopen
tee=: 4 :0
if._1=nc boxopen x do.(x)=: '' end.
(x)=: (do x),y
y
)
grep=: 4 :'x (+./@E.S:0 # ]) y'
pipe=:2 :'v@(u"0)' NB. small pipe -- spoon feed one record at a time
PIPE=:2 :0 NB. big pipe -- feed everything all together
v u y
:
v (,x)"_ y NB. syntactic sugar, beware of tooth decay
)
head=: {.
tail=: -@[ {. ]
sort=: /:~
uniq=: ~.
cat=: ]
echo=: smoutput@;

declare;:'ALGOL_pioneers the_important_scientists'
aa=: ;do TXT=:0 :0 -.LF
(
(
4 head data
),(
cat pipe
('ALGOL'&grep) pipe
('ALGOL_pioneers'&tee)
data
),(
4 tail data
)) ''PIPE
sort PIPE
uniq PIPE
('the_important_scientists'&tee) PIPE
('aa'&grep)
''
)

echo 'Pioneer:';aa</lang>

This produces the result:

<lang>Pioneer: * Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL</lang>


=={{header|Perl}}==
=={{header|Perl}}==