User defined pipe and redirection operators: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Phix}}: comment -> use new requires() builtin)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 4 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 90:
{{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 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 182:
"Number of scientists: ", whole( UPB the scientists list, 0 ), newline
))
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 191:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 425:
fmt.Println(name, "not found")
}
}</langsyntaxhighlight>
Output:
<pre>
Line 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 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 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 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 617 ⟶ 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.
<lang Phix>-- demo\rosetta\Fake_Redirection.exw
<!--<syntaxhighlight lang="phix">(phixonline)-->
requires("0.8.2")
<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>
constant fs = new_dict() -- fake file system
 
class pipe -- fake pipe
string data = ""
integer idx = 0
function getch()
if idx<length(data) then
idx += 1
return data[idx]
end if
return -1
end function
function getln()
if idx<length(data) then
integer start = idx+1
idx = find('\n',data,start)
return data[start..idx]
end if
return -1
end function
procedure putch(integer ch)
data &= ch
end procedure
procedure putln(string line)
data &= line
end procedure
function readall()
return split(data,'\n',no_empty:=true)
end function
function rawdata()
return data
end function
end class
 
function joinup(sequence lines)
return join(lines,"\n")&"\n"
end function
 
procedure toName(string name, pipe src) -- role of > operator
setd(name,src.rawdata(),fs)
end procedure
<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>
function fromName(string name) -- role of < operator
return new(pipe,{getd(name,fs)})
end function
<span style="color: #008080;">class</span> <span style="color: #000000;">pipe</span> <span style="color: #000080;font-style:italic;">-- fake pipe</span>
function tee(pipe in, string name)
<span style="color: #004080;">string</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
string data = in.rawdata()
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
setd(name,data,fs)
<span style="color: #008080;">function</span> <span style="color: #000000;">getch</span><span style="color: #0000FF;">()</span>
return new(pipe,{data})
<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>
end function
<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>
function grep(pipe in, string pat)
<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>
pipe res = new()
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
while true do
object line = in.getln()
if atom(line) then exit end if
if match(pat,line) then
res.putln(line)
end if
end while
return res
end function
 
function multireader(sequence pipes)
pipe res = new()
for i=1 to length(pipes) do
pipe p = pipes[i]
res.putln(p.rawdata())
end for
return res
end function
 
function pipe_head(pipe in, integer lines)
return new(pipe,{joinup(head(in.readall(),lines))})
end function
 
function pipe_tail(pipe in, integer lines)
return new(pipe,{joinup(tail(in.readall(),lines))})
end function
 
function sort_unique(pipe in)
return new(pipe,{joinup(unique(in.readall()))})
end function
<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>
procedure showCount(string heading, name)
<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>
if getd_index(name,fs)=NULL then crash("not found") end if
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
integer n = length(split(getd(name,fs), "\n",no_empty:=true))
printf(1,"%s: %v\n", {heading, n})
<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>
end procedure
<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>
constant lcs_txt = """
Wil van der Aalst business process management, process mining, Petri nets
<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>
Hal Abelson intersection of computing and teaching
<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>
Serge Abiteboul database theory
<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>
Samson Abramsky game semantics
<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>
Leonard Adleman RSA, DNA computing
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Manindra Agrawal polynomial-time primality testing
Luis von Ahn human-based computation
<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>
Alfred Aho compilers book, the 'a' in AWK
<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>
Stephen R. Bourne Bourne shell, portable ALGOL 68C compiler
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
Kees Koster ALGOL 68
<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>
Lambert Meertens ALGOL 68, ABC (programming language)
<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>
Peter Naur BNF, ALGOL 60
<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>
Guido van Rossum Python (programming language)
<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>
Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
Dennis E. Wisnosky Integrated Computer-Aided Manufacturing (ICAM), IDEF
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
Stephen Wolfram Mathematica
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
William Wulf compilers
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Edward Yourdon Structured Systems Analysis and Design Method
Lotfi Zadeh fuzzy logic
<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>
Arif Zaman Pseudo-random number generator
<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>
Albert Zomaya Australian pioneer of scheduling in parallel and distributed systems
<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>
Konrad Zuse German pioneer of hardware and software
<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>
mainlist = "List_of_computer_scientists.lst"
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
setd(mainlist,lcs_txt,fs)
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
toName("aa", grep(tee(sort_unique(multireader({pipe_head(fromName(mainlist), 4),
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
tee(grep(fromName(mainlist), "ALGOL"),
"ALGOL_pioneers.lst"),
<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>
pipe_tail(fromName(mainlist), 4)})),
<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>
"the_important_scientists.lst"),
<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>
"aa"))
<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>
printf(1,"Pioneer: %s", getd("aa",fs))
<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>
showCount("Number of ALGOL pioneers", "ALGOL_pioneers.lst")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
showCount("Number of scientists", "the_important_scientists.lst")</lang>
<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>
Line 763 ⟶ 967:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 895 ⟶ 1,099:
 
(require 'sample)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 905 ⟶ 1,109:
This assumes that the data is in a tab separated file "List_of_computer_scientists.lst" in the current directory.
 
<syntaxhighlight lang="raku" perl6line>sub cat ($fname) { lazy $fname.IO.lines };
sub head ($count, @positional) { @positional.head($count) }
sub redirect ($fname) { cat($fname) }
Line 924 ⟶ 1,128:
).flat .sort .unique tee 'the_important_scientists.lst') .grep: /'aa'/;
 
say "Pioneer: $aa";</langsyntaxhighlight>
{{out}}
<pre>Pioneer: Adriaan van Wijngaarden Dutch pioneer; ARRA, ALGOL</pre>
Line 930 ⟶ 1,134:
=={{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 1,061 ⟶ 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