User defined pipe and redirection operators: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(18 intermediate revisions by 6 users not shown)
Line 18:
* define the procedures: input(cmd,stream), pipe(stream,cmd), output(stream, stream), whereis(array), append(stream)
 
For bonus Kudos: Implement the shell "&" concept as a dyadic operator in the specific language. e.g.: <langsyntaxhighlight lang="bash">( head x & tail x & wait ) | grep test</langsyntaxhighlight>
 
'''Sample shell script:''' ''¢ draft - pending a better (more interesting) suggestion ¢''
<langsyntaxhighlight lang="bash">aa="$(
(
head -4 < List_of_computer_scientists.lst;
Line 28:
) | sort | uniq | tee the_important_scientists.lst | grep aa
);
echo "Pioneer: $aa"</langsyntaxhighlight>
'''Input Records:'''
{|class="wikitable" style="text-align: center; margin: 1em auto 1em auto;"
Line 87:
=={{header|ALGOL 68}}==
See [[User defined pipe and redirection operators/ALGOL 68]]
{{works with|ALGOL 68|Revision 1; one minor extension - PRAGMA READ; one major extension - Algol68G's [[wp:Currying|Currying]].}}{{works with|ALGOL 68G|tested with release [http://sourceforge2.net/projects/algol68/files/algol68g/algol68g-18.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]3.win32}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''Currying.}}
'''File: Iterator_pipe_operators.a68'''
<langsyntaxhighlight lang="algol68">MODE
PAGEIN = PAGE,
PAGEAPPEND = REF PAGE,
Line 110:
>> = (GENLINE gen, PAGEAPPEND page)VOID: gen(APPEND page),
=: = (GENLINE gen, FILTER filter)GENLINE: filter(gen),
=: = (GENLINE gen, MANYTOONE cmd)GENLINE: cmd(gen);</langsyntaxhighlight>
'''File: Iterator_pipe_utilities.a68'''
* c.f. [[User defined pipe and redirection operators/ALGOL 68#Iterator pipe utilities]]
<langsyntaxhighlight lang="algol68"> # Sample ''utilities'' PROCedure declarations #
PROC cat = ([]GENLINE argv)GENLINE:~;
PROC tee = ([]YIELDLINE args filter)FILTER:~;
Line 120:
PROC sort = (GENLINE arg)GENLINE:~;
PROC head = (INT n, []GENLINE args)GENLINE:~;
PROC tail = (INT n, []GENLINE args)GENLINE:~;</langsyntaxhighlight>
 
'''File: Iterator_pipe_page.a68'''
* c.f. [[User defined pipe and redirection operators/ALGOL 68#Iterator pipe page]]
<langsyntaxhighlight lang="algol68"># Sample ''pipe I/O'' OPerator declarations #
OP READ = (PAGEIN page)GENLINE:~;
OP WRITE = (PAGEOUT page)YIELDLINE: ~;
OP APPEND = (PAGEAPPEND page)YIELDLINE:~;</langsyntaxhighlight>
'''File: test_Iterator_pipe_page.a68'''
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
# First define what kind of record (aka LINE) we are piping and filtering #
FORMAT line fmt = $xg$;
Line 177:
 
# Finally check the result: #
printfprint((
$"Pioneer: "$, line fmt, aa, $l$newline,
$"Number of Algol pioneers: "g(-0)$, whole( UPB algol pioneers list, $l$0 ), newline,
$"Number of scientists: "g(-0)$, whole( UPB the scientists list, $l$0 ), newline
))</lang>
</syntaxhighlight>
'''Output:'''
<pre>
Pioneer: Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
Number of Algol pioneers: 65
Number of scientists: 1513
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 424 ⟶ 425:
fmt.Println(name, "not found")
}
}</langsyntaxhighlight>
Output:
<pre>
Line 438 ⟶ 439:
Step 0: get the data. The task does not specify how to get the data, so here I use lynx, which is readily available on most unix-like systems, including cygwin. Note that lynx needs to be in the OS PATH when running j.
 
<langsyntaxhighlight lang="j">require 'task'
data=:<;._2 shell 'lynx -dump -nolist -width=999 http://en.wikipedia.org/wiki/List_of_computer_scientists'</langsyntaxhighlight>
 
Step 1: define task core algorithms:
 
<langsyntaxhighlight lang="j">grep=: +./@E.S:0 # ]</langsyntaxhighlight>
 
Step 2: select and display the required data:
 
<langsyntaxhighlight lang="j"> ;'aa' grep 'ALGOL' grep data
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
</syntaxhighlight>
</lang>
 
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.)
Line 455 ⟶ 456:
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:
 
<langsyntaxhighlight lang="j"> ;'aa' grep 'ALGOL' grep data,data
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
Line 461 ⟶ 462:
;'aa' grep ~. 'ALGOL' grep data,data
* Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL
</syntaxhighlight>
</lang>
 
That said, this implements most (perhaps all) of the required complexities:
 
<langsyntaxhighlight lang="j">declare=: erase@boxopen
tee=: 4 :0
if._1=nc boxopen x do.(x)=: '' end.
Line 505 ⟶ 506:
)
 
echo 'Pioneer:';aa</langsyntaxhighlight>
 
This produces the result:
 
<syntaxhighlight lang="text">Pioneer: * Adriaan van Wijngaarden - Dutch pioneer; ARRA, ALGOL</langsyntaxhighlight>
 
=={{header|Julia}}==
Uses Julia's Channels as asynchronous pipes. Note that the $( .. ) Unix command, which gathers stdin output into a single variable, is not used. Instead, a common output channel for various tasks is used to gather output. Sort() rather than sort() is used so as not to overload Base.sort().
<syntaxhighlight lang="julia">const LISTDATA = """
Wil van der Aalst business process management, process mining, Petri nets
Hal Abelson intersection of computing and teaching
Serge Abiteboul database theory
Samson Abramsky game semantics
Leonard Adleman RSA, DNA computing
Manindra Agrawal polynomial-time primality testing
Luis von Ahn human-based computation
Alfred Aho compilers book, the 'a' in AWK
Stephen R. Bourne Bourne shell, portable ALGOL 68C compiler
Kees Koster ALGOL 68
Lambert Meertens ALGOL 68, ABC (programming language)
Peter Naur BNF, ALGOL 60
Guido van Rossum Python (programming language)
Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
Dennis E. Wisnosky Integrated Computer-Aided Manufacturing (ICAM), IDEF
Stephen Wolfram Mathematica
William Wulf compilers
Edward Yourdon Structured Systems Analysis and Design Method
Lotfi Zadeh fuzzy logic
Arif Zaman Pseudo-random number generator
Albert Zomaya Australian pioneer of scheduling in parallel and distributed systems
Konrad Zuse German pioneer of hardware and software
"""
 
datafilename = "List_of_computer_scientists.lst"
stat(datafilename).size == 0 && (fd = open(datafilename, "a"); write(fd, LISTDATA); close(fd))
 
channelstream() = Channel{String}(200)
closewhenempty(c) = @async begin while !isempty(c) sleep(rand()) end; close(c); end
 
function head(filename, numlines, chan=channelstream())
fd = open(filename)
buffer = String[]
while !eof(fd)
push!(buffer, readline(fd, keep=true))
length(buffer) == numlines && break
end
close(fd)
@async begin
for line in buffer
put!(chan, line)
end
closewhenempty(chan)
end
return chan
end
 
function cat(filename::AbstractString, chan=channelstream())
@async begin
fd = open(filename)
while !eof(fd)
put!(chan, readline(fd, keep=true))
end
close(fd)
closewhenempty(chan)
end
return chan
end
 
function grep(inchan::Channel, target, outchan = channelstream())
@async begin
try
while isopen(inchan)
line = take!(inchan)
if occursin(target, line)
put!(outchan, line)
end
end
catch;
end
closewhenempty(outchan)
end
return outchan
end
grep(target) = (chan) -> grep(chan, target)
 
function tee(inchan::Channel, filename, outchan=channelstream())
fd = open(filename, "w")
@async begin
while isopen(inchan)
try
line = take!(inchan)
write(fd, line)
put!(outchan, line)
catch;
break
end
end
close(fd)
closewhenempty(outchan)
end
return outchan
end
tee(filename, outchan=channelstream()) = (inchan) -> tee(inchan, filename, outchan)
 
function tail(filename, numlines, chan=channelstream())
fd = open(filename)
buffer = String[]
while !eof(fd)
push!(buffer, readline(fd, keep=true))
length(buffer) > numlines && popfirst!(buffer)
end
@async begin
for line in buffer
put!(chan, line)
end
closewhenempty(chan)
end
return chan
end
 
function Sort(inchan, outchan = channelstream())
buffer = String[]
try
while isopen(inchan)
push!(buffer, take!(inchan))
end
catch;
end
@async begin
for line in sort!(buffer)
put!(outchan, line)
end
closewhenempty(outchan)
end
return outchan
end
Sort() = (chan) -> Sort(chan)
 
function uniq(inchan, outchan = channelstream())
alreadyseen = Set{String}()
@async begin
while isopen(inchan)
try
line = take!(inchan)
if !(line in alreadyseen)
push!(alreadyseen, line)
put!(outchan, line)
end
catch;
break
end
end
closewhenempty(outchan)
end
return outchan
end
uniq() = (chan) -> uniq(chan)
 
function print_lines(chan)
@async begin
while isopen(chan)
line = take!(chan)
print(line)
end
end
end
print_lines() = (chan) -> print_lines(chan)
 
const commonoutchan = channelstream()
 
head("List_of_computer_scientists.lst", 4, commonoutchan)
 
cat("List_of_computer_scientists.lst") |> grep("ALGOL") |> tee("Algol_pioneers.lst", commonoutchan)
 
tail("List_of_computer_scientists.lst", 4, commonoutchan)
 
Sort(commonoutchan) |> uniq() |> tee("the_important_scientists.lst") |>
grep("aa") |> print_lines()
</syntaxhighlight>{{out}}
<pre>
Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
</pre>
 
=={{header|Perl}}==
Implementing only stream chaining, cat, grep and tee. Oddly enough, I don't feel the urge to implement all of the more-or-less-the-same features asked for by the task.
<langsyntaxhighlight lang="perl">use strict;
use 5.10.0;
 
Line 616 ⟶ 794:
;
 
print while $_ = $chain->readline;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{trans|Go}}
{{libheader|Phix/Class}}
You could of course do things more character-by-character or line-by-line, and/or farm things out to separate
threads/tasks, but the latter would need some suspend/resume/scheduling, along with explicit eof markers.
The distributed version also has a couple of alternatives for pipe_head() and pipe_tail(), along with a
class-less version that is compatible with pwa/p2js.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Fake_Redirection.exw</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- class (see other version in distro)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- fake file system</span>
<span style="color: #008080;">class</span> <span style="color: #000000;">pipe</span> <span style="color: #000080;font-style:italic;">-- fake pipe</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">getch</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">getln</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">start</span><span style="color: #0000FF;">..</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">putch</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">data</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">putln</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">data</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">line</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">readall</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">no_empty</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rawdata</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">data</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">joinup</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">toName</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pipe</span> <span style="color: #000000;">src</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- role of &gt; operator</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">src</span><span style="color: #0000FF;">.</span><span style="color: #000000;">rawdata</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">fromName</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- role of &lt; operator</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">tee</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">.</span><span style="color: #000000;">rawdata</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">data</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">grep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">pat</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">pipe</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">.</span><span style="color: #000000;">getln</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pat</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">.</span><span style="color: #000000;">putln</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">multireader</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">pipes</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">pipe</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">pipe</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pipes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">.</span><span style="color: #000000;">putln</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">.</span><span style="color: #000000;">rawdata</span><span style="color: #0000FF;">())</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pipe_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">pipe</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lines</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">.</span><span style="color: #000000;">getln</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">.</span><span style="color: #000000;">putln</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #000080;font-style:italic;">-- or, nicer/neater but potentially much wasted effort:
-- return new(pipe,{joinup(head(pin.readall(),lines))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pipe_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- sequence ring = repeat("",lines)
-- integer rn = 0, full = false
-- while true do
-- object line = pin.getln()
-- if atom(line) then exit end if
-- rn += 1
-- if rn&gt;lines then {rn,full} = {1,true} end if
-- ring[rn] = line
-- end while
-- ring = iff(full?ring[rn+1..$]:{}) & ring[1..rn]
-- return new(pipe,{joinup(ring)})</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">joinup</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pin</span><span style="color: #0000FF;">.</span><span style="color: #000000;">readall</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sort_unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span> <span style="color: #000000;">pin</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pipe</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">joinup</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pin</span><span style="color: #0000FF;">.</span><span style="color: #000000;">readall</span><span style="color: #0000FF;">()))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">showCount</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">heading</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"not found"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">),</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">heading</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">lcs_txt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Wil van der Aalst business process management, process mining, Petri nets
Hal Abelson intersection of computing and teaching
Serge Abiteboul database theory
Samson Abramsky game semantics
Leonard Adleman RSA, DNA computing
Manindra Agrawal polynomial-time primality testing
Luis von Ahn human-based computation
Alfred Aho compilers book, the 'a' in AWK
Stephen R. Bourne Bourne shell, portable ALGOL 68C compiler
Kees Koster ALGOL 68
Lambert Meertens ALGOL 68, ABC (programming language)
Peter Naur BNF, ALGOL 60
Guido van Rossum Python (programming language)
Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
Dennis E. Wisnosky Integrated Computer-Aided Manufacturing (ICAM), IDEF
Stephen Wolfram Mathematica
William Wulf compilers
Edward Yourdon Structured Systems Analysis and Design Method
Lotfi Zadeh fuzzy logic
Arif Zaman Pseudo-random number generator
Albert Zomaya Australian pioneer of scheduling in parallel and distributed systems
Konrad Zuse German pioneer of hardware and software
"""</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">mainlist</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"List_of_computer_scientists.lst"</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mainlist</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lcs_txt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">toName</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aa"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">grep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tee</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sort_unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">multireader</span><span style="color: #0000FF;">({</span><span style="color: #000000;">pipe_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fromName</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mainlist</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">tee</span><span style="color: #0000FF;">(</span><span style="color: #000000;">grep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fromName</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mainlist</span><span style="color: #0000FF;">),</span> <span style="color: #008000;">"ALGOL"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ALGOL_pioneers.lst"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">pipe_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fromName</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mainlist</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">)})),</span>
<span style="color: #008000;">"the_important_scientists.lst"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"aa"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Pioneer: %s"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aa"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">showCount</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Number of ALGOL pioneers"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ALGOL_pioneers.lst"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">showCount</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Number of scientists"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"the_important_scientists.lst"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Pioneer: Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
Number of ALGOL pioneers: 5
Number of scientists: 13
</pre>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 752 ⟶ 1,099:
 
(require 'sample)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Implementing cat, redirect(<), head, tail and tee. I share the same general lack of enthusiasm as the Perl example author to implement the other shell ops. Many of the ops already exist in some form in Raku, though they may have slightly different syntax and precedence as the shell ops. I didn't bother implementing pipe (|) as Raku pretty much has chaining semantics built in.
 
The less than '<' operator is very low level in Raku and would be troublesome to override, so is implemented as 'redirect'. The sort and unique directives don't really have any effect on the final result string, it would be the same without them, but it is following the task example.
 
This assumes that the data is in a tab separated file "List_of_computer_scientists.lst" in the current directory.
 
<syntaxhighlight lang="raku" line>sub cat ($fname) { lazy $fname.IO.lines };
sub head ($count, @positional) { @positional.head($count) }
sub redirect ($fname) { cat($fname) }
sub tail (Int $count, Str $fname) { $fname.IO.lines.tail($count) }
sub infix:<tee> ($stream, $fname) {
# need to reify the lazy list to write to a file
my @values = @$stream[^Inf].grep: *.defined;
# meh. not really going to write the files
# $fname.IO.put @values.join("\n")
# just pass the reified values along
@values.List
}
 
my $aa = ((
(head 4, redirect 'List_of_computer_scientists.lst'),
cat('List_of_computer_scientists.lst') .grep( /'ALGOL'/ ) tee 'ALGOL_pioneers.lst',
tail 4, 'List_of_computer_scientists.lst'
).flat .sort .unique tee 'the_important_scientists.lst') .grep: /'aa'/;
 
say "Pioneer: $aa";</syntaxhighlight>
{{out}}
<pre>Pioneer: Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL</pre>
 
=={{header|Tcl}}==
The syntax of redirections is slightly out, as they're inserted as explicit pipeline elements, and standard Tcl syntax is used to pull in results from sub-pipelines (because it is vastly simpler):
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# Helpers
Line 887 ⟶ 1,265:
}
close $f
}</langsyntaxhighlight>
Sample pipeline:
<langsyntaxhighlight lang="tcl">set file "List_of_computer_scientists.lst"
set aa [pipeline \
<< [pipeline < $file | head 4] [pipeline < $file | grep ALGOL | tee "ALGOL_pioneers.txt"] [pipeline < $file | tail 4] \
| sort | uniq | tee "the_important_scientists.lst" | grep aa]
puts "Pioneer: $aa"</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Phix}}
{{libheader|Wren-seq}}
Although Wren supports operator overloading, there are a number of restrictions which would make simulating the Unix shell operators awkward or even imposible. As in the Phix (and Go) examples, I've therefore used named methods instead.
<syntaxhighlight lang="wren">import "./seq" for Lst
 
var FS = {} // fake file system
 
var JoinUp = Fn.new { |lines| lines.join("\n") + "\n" }
 
class Pipe { // fake pipe
static fromName(name) { Pipe.new(FS[name]) } // role of < operator
 
static multireader(pipes) {
var res = Pipe.new("")
for (i in 0...pipes.count) {
var p = pipes[i]
res.putln(p.rawData)
}
return res
}
 
construct new(data) {
_data = data
_idx = -1
}
 
getln() {
if (_idx < _data.count-1) {
var start = _idx + 1
_idx = _data.indexOf("\n", start)
return (_idx >= 0) ? _data[start.._idx] : ""
}
return ""
}
 
putln(line) { _data = _data + line }
 
readAll() { _data.split("\n").where { |s| s != "" }.toList }
 
rawData { _data }
 
toName(name) { FS[name] = _data } // role of > operator
 
tee(name) { Pipe.new(FS[name] = _data) }
 
grep(pat) {
var res = Pipe.new("")
while (true) {
var line = getln()
if (line == "") break
if (line.indexOf(pat) >= 0) res.putln(line)
}
return res
}
 
head(lines) { Pipe.new(JoinUp.call(readAll().take(lines).toList)) }
 
tail(lines) {
var t = readAll()
if (t.count >= lines) t = t[-lines..-1]
return Pipe.new(JoinUp.call(t))
}
 
sortUnique { Pipe.new(JoinUp.call(Lst.distinct(readAll()))) }
}
 
var showCount = Fn.new { |heading, name|
if (!FS[name]) Fiber.abort("not found")
var n = FS[name].split("\n").count { |s| s != "" }
System.print("%(heading): %(n)")
}
 
var lcsTxt = """
Wil van der Aalst business process management, process mining, Petri nets
Hal Abelson intersection of computing and teaching
Serge Abiteboul database theory
Samson Abramsky game semantics
Leonard Adleman RSA, DNA computing
Manindra Agrawal polynomial-time primality testing
Luis von Ahn human-based computation
Alfred Aho compilers book, the 'a' in AWK
Stephen R. Bourne Bourne shell, portable ALGOL 68C compiler
Kees Koster ALGOL 68
Lambert Meertens ALGOL 68, ABC (programming language)
Peter Naur BNF, ALGOL 60
Guido van Rossum Python (programming language)
Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
Dennis E. Wisnosky Integrated Computer-Aided Manufacturing (ICAM), IDEF
Stephen Wolfram Mathematica
William Wulf compilers
Edward Yourdon Structured Systems Analysis and Design Method
Lotfi Zadeh fuzzy logic
Arif Zaman Pseudo-random number generator
Albert Zomaya Australian pioneer of scheduling in parallel and distributed systems
Konrad Zuse German pioneer of hardware and software
"""
 
var mainList = "List_of_computer_scientists.lst"
FS[mainList] = lcsTxt
var p = Pipe.fromName(mainList)
var pipes = [p.head(4), p.grep("ALGOL").tee("ALGOL_pioneers.lst"), p.tail(4)]
var p2 = Pipe.multireader(pipes).sortUnique.tee("the_important_scientists.lst").grep("aa").toName("aa")
System.write("Pioneer: %(FS["aa"])")
showCount.call("Number of ALGOL pioneers", "ALGOL_pioneers.lst")
showCount.call("Number of scientists", "the_important_scientists.lst")</syntaxhighlight>
 
{{out}}
<pre>
Pioneer: Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
Number of ALGOL pioneers: 5
Number of scientists: 13
</pre>
9,476

edits