Commits

patrickl  committed f215540

[svn r537] ok

  • Participants
  • Parent commits 878bcdb

Comments (0)

Files changed (1)

File bashkell.html

+Content-type: text/html
+
+<HTML><HEAD><TITLE>Manpage of bashkell</TITLE>
+</HEAD><BODY>
+<H1>bashkell</H1>
+Section: User Commands  (1)<BR>Updated: March 5 2010<BR><A HREF="#index">Index</A>
+<A HREF="http://localhost/cgi-bin/man/man2html">Return to Main Contents</A><HR>
+
+
+<P>
+<P>
+<A NAME="lbAB">&nbsp;</A>
+<H2>NAME</H2>
+
+bashkell - A bash function library inspired by Haskell.
+<P>
+<P>
+<A NAME="lbAC">&nbsp;</A>
+<H2>SYNOPSIS</H2>
+
+<BR>&nbsp;&nbsp;source&nbsp;bashkell&nbsp;[-l]
+<P>
+<P>
+<A NAME="lbAD">&nbsp;</A>
+<H2>DESCRIPTION</H2>
+
+<B>baskell</B>
+
+is a bash function library inspired by Haskell. It provides implementations of the following 
+standard Haskell higher-order functions: 
+<I>map</I>, <I>filter</I>, <I>foldl</I>, <I>foldr</I>, <I>all</I> and <I>any</I>. Implementations for the 
+following standard Haskell functions are also provided:
+<I>sum</I>, <I>product</I>, <I>max</I>, <I>min</I>, <I>and</I>, <I>or</I>
+and <I>show</I>.
+
+<B>baskell</B>
+
+also supports lambda expressions that can be used with higher-order functions or to define 
+short (one line) bash functions.
+<P>
+<P>
+<A NAME="lbAE">&nbsp;</A>
+<H2>OPTIONS</H2>
+
+<DL COMPACT>
+<DT><B>-l</B>
+
+<DD>
+When the 
+<B>-l </B>
+
+options is used, <B>bashkell</B> defines a <I>let</I> functionn that is an alias for <I>define</I> (see below
+for more about <I>define</I>). This allows function definition in a style more similar to Haskell.
+<P>
+Note: As a side effect of defining a <I>let</I> function, the bash <I>let</I> keyword 
+will be masked. Do not use this option if you intend to use the bash <I>let</I>
+keyword to create variables.
+<P>
+<P>
+</DL>
+<A NAME="lbAF">&nbsp;</A>
+<H2>INPUT</H2>
+
+Most <B>bashkell</B> functions (with the exception of <I>show</I>) operate on a list
+as input. By default, a function's input is assumed to be provided via stdin. However, it is also
+possible to provide the function's input on the command line, by using a <I>$</I> followed by the actual
+input supplied as parameters. For example, the 2 following ways of implementing the identity function 
+over a list using <I>map</I> are equivalent:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;-e&nbsp;&quot;1\n2\n3&quot;&nbsp;|&nbsp;map&nbsp;echo
+<BR>&nbsp;&nbsp;1
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;3
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;echo&nbsp;$&nbsp;1&nbsp;2&nbsp;3
+<BR>&nbsp;&nbsp;1&nbsp;
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;3
+<P>
+<P>
+<A NAME="lbAG">&nbsp;</A>
+<H2>OUTPUT</H2>
+
+The output for all <B>bashkell</B> functions is stdout. If a function returns a list, the output will
+be (possibly) multiple lines to stdout. If a function returns a scalar value, the output will be one line to 
+stdout.
+<P>
+The output for bash expressions manipulated by <B>bashkell</B> functions is also considered as being,
+by default, stdout. For example, if you apply 'echo' to a list using <I>map</I>, the resulting list is
+a concatenation of all the stdout output for each call to 'echo'. However, in some case we are more 
+interested in the return code of an expression rather than in it's stdout output. In order to have <B>bashkell</B> 
+higher-order functions operate on the return code of an expression instead if on it's output, use <B>-r</B>
+option of that function. In that case, the stdout output of the expression will be discarded.
+<P>
+Here are some examples:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;echo&nbsp;$&nbsp;1&nbsp;2
+<BR>&nbsp;&nbsp;1
+<BR>&nbsp;&nbsp;2
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;-r&nbsp;echo&nbsp;$&nbsp;1&nbsp;2
+<BR>&nbsp;&nbsp;0
+<BR>&nbsp;&nbsp;0
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;eval&nbsp;$&nbsp;/bin/true&nbsp;/bin/false&nbsp;
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;-r&nbsp;eval&nbsp;$&nbsp;/bin/true&nbsp;/bin/false
+<BR>&nbsp;&nbsp;0
+<BR>&nbsp;&nbsp;1
+<P>
+In a way using the return code can be viewed as evaluating the bash expression in a boolean context.
+<P>
+<P>
+<A NAME="lbAH">&nbsp;</A>
+<H2>LAMBDA EXPRESSIONS</H2>
+
+<B>bashkell</B> has support for Haskell-style lambda expressions. These can be used with the higher-order 
+<B>bashkell</B> function or with the <I>define/let</I> function. The syntax is really similar to Haskell:
+<P>
+<BR>&nbsp;&nbsp;\ARG1&nbsp;...&nbsp;ARGN&nbsp;-&gt;&nbsp;BASH_EXPRESSION
+<P>
+For example, you could rewrite the identity function from the above example as such:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;'\x&nbsp;-&gt;&nbsp;echo&nbsp;$x'&nbsp;$&nbsp;1&nbsp;2&nbsp;3
+<BR>&nbsp;&nbsp;1
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;3
+<P>
+You could also have given a name to your lambda expression using <I>define</I>:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;define&nbsp;id&nbsp;=&nbsp;'\x&nbsp;-&gt;&nbsp;echo&nbsp;$x'
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;id&nbsp;$&nbsp;1&nbsp;2&nbsp;3
+<BR>&nbsp;&nbsp;1
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;3
+<P>
+<P>
+<A NAME="lbAI">&nbsp;</A>
+<H2>HIGHER-ORDER FUNCTIONS</H2>
+
+<P>
+<A NAME="lbAJ">&nbsp;</A>
+<H3>map [-r] EXPRESSION [$ INPUT...]</H3>
+
+Maps <I>EXPRESSION </I> over the input. <I>EXPRESSION</I> can be either a normal bash expression or a lambda expression.
+In the case of a normal bash expression, the argument will be appended to the end of the expression. 
+<P>
+For example, to get the basename of a list of files, each of the following example would work:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;basename&nbsp;$&nbsp;/tmp/file1&nbsp;/tmp/file2&nbsp;/tmp/file3
+<BR>&nbsp;&nbsp;file1
+<BR>&nbsp;&nbsp;file2
+<BR>&nbsp;&nbsp;file3
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;'\f&nbsp;-&gt;&nbsp;basename&nbsp;$f'&nbsp;$&nbsp;/tmp/file1&nbsp;/tmp/file2&nbsp;/tmp/file3
+<BR>&nbsp;&nbsp;file1
+<BR>&nbsp;&nbsp;file2
+<BR>&nbsp;&nbsp;file3
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;/tmp/file1&nbsp;&gt;&nbsp;/tmp/files
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;/tmp/file2&nbsp;&gt;&gt;&nbsp;/tmp/files
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;/tmp/file3&nbsp;&gt;&gt;&nbsp;/tmp/files
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;basename&nbsp;&lt;/tmp/files
+<BR>&nbsp;&nbsp;file1
+<BR>&nbsp;&nbsp;file2
+<BR>&nbsp;&nbsp;file3
+<P>
+Note: Strictly speaking, this implementation of <I>map</I> is more like <I>fmap</I>, because
+all of the outputs are concatenated together.
+<P>
+<A NAME="lbAK">&nbsp;</A>
+<H3>filter EXPRESSION [$ INPUT...]</H3>
+
+Returns elements of <I>INPUT</I> for which <I>EXPRESSION</I> is true. Note that here it is the return code
+of <I>EXPRESSION</I> that is used to determine truth, all output is discarded.
+<P>
+For example, to verify which files exist from a list of file:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;filter&nbsp;'test&nbsp;-e'&nbsp;$&nbsp;/etc/passwd&nbsp;/etc/foo&nbsp;/etc/bar
+<BR>&nbsp;&nbsp;/etc/passwd
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;filter&nbsp;'\f&nbsp;-&gt;&nbsp;[[&nbsp;-e&nbsp;&quot;$f&quot;&nbsp;]]'&nbsp;$&nbsp;/etc/passwd&nbsp;/etc/foo&nbsp;/etc/bar
+<BR>&nbsp;&nbsp;/etc/passwd
+<P>
+Select even numbers from a list:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;filter&nbsp;'\n&nbsp;-&gt;&nbsp;!((n&nbsp;%&nbsp;2))'&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;4
+<P>
+<A NAME="lbAL">&nbsp;</A>
+<H3>foldl [-r] EXPRESSION ACC [$ INPUT...]</H3>
+
+Performs a left fold on the input, using <I>EXPRESSION</I> and <I>ACC</I> as the starting value. 
+<P>
+For example:
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;define&nbsp;minus&nbsp;=&nbsp;'\a&nbsp;b&nbsp;-&gt;&nbsp;echo&nbsp;$((a&nbsp;-&nbsp;b))'
+<BR>&nbsp;&nbsp;$&nbsp;foldl&nbsp;minus&nbsp;0&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5
+<BR>&nbsp;&nbsp;-15
+<P>
+Note: <I>foldl</I> cannot be used to build a lists, i.e. <I>ACC</I> must ba a scalar value.
+<P>
+<A NAME="lbAM">&nbsp;</A>
+<H3>foldr [-r] EXPRESSION ACC [$ INPUT...]</H3>
+
+Performs a right fold on the input, using <I>EXPRESSION</I> and <I>ACC</I> as the starting value. 
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;define&nbsp;minus&nbsp;=&nbsp;'\a&nbsp;b&nbsp;-&gt;&nbsp;echo&nbsp;$((a&nbsp;-&nbsp;b))'
+<BR>&nbsp;&nbsp;$&nbsp;foldr&nbsp;minus&nbsp;0&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5
+<BR>&nbsp;&nbsp;3
+<P>
+Note: <I>foldl</I> cannot be used to build a lists, i.e. <I>ACC</I> must ba a scalar value.
+<P>
+<A NAME="lbAN">&nbsp;</A>
+<H3>all[_] EXPRESSION [$ INPUT...]</H3>
+
+Prints 0 if <I>EXPRESSION</I> is true for each input element, prints 1 otherwise. The return code
+of <I>all</I> will also follow the same rules. If you wish to discard the output of the <I>all</I>
+function, use <I>all_</I> instead.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;all&nbsp;'\n&nbsp;-&gt;&nbsp;((n&nbsp;&gt;&nbsp;0))'&nbsp;$&nbsp;1&nbsp;2
+<BR>&nbsp;&nbsp;0
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;all_&nbsp;'\n&nbsp;-&gt;&nbsp;((n&nbsp;&gt;&nbsp;0))'&nbsp;$&nbsp;1&nbsp;0
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;$?
+<BR>&nbsp;&nbsp;1
+<P>
+Note: <I>all</I> evaluates <I>EXPRESSION</I> for ALL input elements. It will not stop after the first
+element for which <I>EXPRESSION</I> is false.
+<P>
+<A NAME="lbAO">&nbsp;</A>
+<H3>any[_] EXPRESSION [$ INPUT...]</H3>
+
+Prints 0 if <I>EXPRESSION</I> is true for at least 1 input element, prints 1 otherwise. The return code
+of <I>any</I> will also follow the same rules. If you wish to discard the output of the <I>any</I>
+function, use <I>any_</I> instead.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;any&nbsp;'\n&nbsp;-&gt;&nbsp;((n&nbsp;&gt;&nbsp;0))'&nbsp;$&nbsp;1&nbsp;2
+<BR>&nbsp;&nbsp;0
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;any_&nbsp;'\n&nbsp;-&gt;&nbsp;((n&nbsp;&gt;&nbsp;0))'&nbsp;$&nbsp;0&nbsp;0
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;$?
+<BR>&nbsp;&nbsp;1
+<P>
+Note: <I>any</I> evaluates <I>EXPRESSION</I> for ALL input elements. It will not stop after the first
+element for which <I>EXPRESSION</I> is true.
+<P>
+<A NAME="lbAP">&nbsp;</A>
+<H3>define NAME = LAMBDA_EXPRESSION</H3>
+
+<A NAME="lbAQ">&nbsp;</A>
+<H3>define NAME [ARG...] = BASH_EXPRESSION</H3>
+
+<I>define</I> is use to give names to lambda expressions or bash expressions. The expression is wrapped
+in a bash function of the given name. If the <B>-l</B> option is used when <B>bashkell</B> is sourced,
+the <I>let function can be used for the same effect.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;define&nbsp;add&nbsp;a&nbsp;b&nbsp;=&nbsp;'echo&nbsp;$((a&nbsp;+&nbsp;b))'
+<BR>&nbsp;&nbsp;$&nbsp;add&nbsp;1&nbsp;2
+<BR>&nbsp;&nbsp;3
+<P>
+<BR>&nbsp;&nbsp;#&nbsp;source&nbsp;bashkell&nbsp;-l
+<BR>&nbsp;&nbsp;$&nbsp;let&nbsp;add&nbsp;=&nbsp;'\a&nbsp;b&nbsp;-&gt;&nbsp;echo&nbsp;$((a&nbsp;+&nbsp;b))'
+<BR>&nbsp;&nbsp;$&nbsp;let&nbsp;inc&nbsp;n&nbsp;=&nbsp;'add&nbsp;1&nbsp;n'
+<BR>&nbsp;&nbsp;$&nbsp;map&nbsp;inc&nbsp;$&nbsp;1&nbsp;2&nbsp;3
+<BR>&nbsp;&nbsp;2
+<BR>&nbsp;&nbsp;3
+<BR>&nbsp;&nbsp;4
+<P>
+<P>
+</I><A NAME="lbAR">&nbsp;</A>
+<H2>OTHER FUNCTIONS</H2>
+
+<P>
+<A NAME="lbAS">&nbsp;</A>
+<H3>and[_] [$ INPUT...]</H3>
+
+Performs a logical 'and' of all input elements. Note that here, as with bash return codes,
+0 is true and any other number is false. Use <I>and_</I> to discard output.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;and&nbsp;$&nbsp;0&nbsp;0
+<BR>&nbsp;&nbsp;0
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;and_&nbsp;$&nbsp;0&nbsp;1&nbsp;
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;$?
+<BR>&nbsp;&nbsp;1
+<P>
+<A NAME="lbAT">&nbsp;</A>
+<H3>or[_] [$ INPUT...]</H3>
+
+Performs a logical 'or' of all input elements. Note that here, as with bash return codes,
+0 is true and any other number is false. Use <I>and_</I> to discard output.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;or&nbsp;$&nbsp;0&nbsp;0
+<BR>&nbsp;&nbsp;0
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;or_&nbsp;$&nbsp;0&nbsp;1&nbsp;
+<BR>&nbsp;&nbsp;$&nbsp;echo&nbsp;$?
+<BR>&nbsp;&nbsp;0
+<P>
+<A NAME="lbAU">&nbsp;</A>
+<H3>take [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;take&nbsp;2&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4
+<BR>&nbsp;&nbsp;1
+<BR>&nbsp;&nbsp;2
+<P>
+<A NAME="lbAV">&nbsp;</A>
+<H3>drop [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;drop&nbsp;2&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4
+<BR>&nbsp;&nbsp;3
+<BR>&nbsp;&nbsp;4
+<P>
+<A NAME="lbAW">&nbsp;</A>
+<H3>sum [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;sum&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4
+<BR>&nbsp;&nbsp;10
+<BR>&nbsp;&nbsp;
+<A NAME="lbAX">&nbsp;</A>
+<H3>product [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;product&nbsp;$&nbsp;1&nbsp;2&nbsp;3&nbsp;4
+<BR>&nbsp;&nbsp;24
+<P>
+<A NAME="lbAY">&nbsp;</A>
+<H3>maximum [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;maximum&nbsp;$&nbsp;5&nbsp;4&nbsp;6
+<BR>&nbsp;&nbsp;6
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;maximum&nbsp;$&nbsp;
+<P>
+<A NAME="lbAZ">&nbsp;</A>
+<H3>minimum [$ INPUT...]</H3>
+
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;minimum&nbsp;$&nbsp;5&nbsp;4&nbsp;6
+<BR>&nbsp;&nbsp;4
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;minimum&nbsp;$&nbsp;
+<P>
+<A NAME="lbBA">&nbsp;</A>
+<H3>show INPUT... </H3>
+
+<I>show</I> is just an alias for echo.
+<P>
+<BR>&nbsp;&nbsp;$&nbsp;show&nbsp;a&nbsp;b&nbsp;c
+<BR>&nbsp;&nbsp;a&nbsp;b&nbsp;c
+<P>
+<P>
+<A NAME="lbBB">&nbsp;</A>
+<H2>AUTHOR</H2>
+
+Written by Patrick LeBoutillier &lt;<A HREF="mailto:patl@cpan.org">patl@cpan.org</A>&gt;.
+<P>
+<P>
+
+<HR>
+<A NAME="index">&nbsp;</A><H2>Index</H2>
+<DL>
+<DT><A HREF="#lbAB">NAME</A><DD>
+<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
+<DT><A HREF="#lbAD">DESCRIPTION</A><DD>
+<DT><A HREF="#lbAE">OPTIONS</A><DD>
+<DT><A HREF="#lbAF">INPUT</A><DD>
+<DT><A HREF="#lbAG">OUTPUT</A><DD>
+<DT><A HREF="#lbAH">LAMBDA EXPRESSIONS</A><DD>
+<DT><A HREF="#lbAI">HIGHER-ORDER FUNCTIONS</A><DD>
+<DL>
+<DT><A HREF="#lbAJ">map [-r] EXPRESSION [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAK">filter EXPRESSION [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAL">foldl [-r] EXPRESSION ACC [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAM">foldr [-r] EXPRESSION ACC [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAN">all[_] EXPRESSION [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAO">any[_] EXPRESSION [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAP">define NAME = LAMBDA_EXPRESSION</A><DD>
+<DT><A HREF="#lbAQ">define NAME [ARG...] = BASH_EXPRESSION</A><DD>
+</DL>
+<DT><A HREF="#lbAR">OTHER FUNCTIONS</A><DD>
+<DL>
+<DT><A HREF="#lbAS">and[_] [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAT">or[_] [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAU">take [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAV">drop [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAW">sum [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAX">product [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAY">maximum [$ INPUT...]</A><DD>
+<DT><A HREF="#lbAZ">minimum [$ INPUT...]</A><DD>
+<DT><A HREF="#lbBA">show INPUT... </A><DD>
+</DL>
+<DT><A HREF="#lbBB">AUTHOR</A><DD>
+</DL>
+<HR>
+This document was created by
+<A HREF="http://localhost/cgi-bin/man/man2html">man2html</A>,
+using the manual pages.<BR>
+Time: 22:49:15 GMT, March 31, 2010
+</BODY>
+</HTML>