scratch/output/Scratch/en/blog/Higher-order-function-in-zsh/index.html
Yann Esposito 70314df976 Recompiled
2012-05-02 17:43:56 +02:00

350 lines
No EOL
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="zsh, map, foldr, filter, functional, programming, higher order functions">
<link rel="shortcut icon" type="image/x-icon" href="/Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/Scratch/assets/css/main.css" />
<link rel="stylesheet" type="text/css" href="/Scratch/css/solarized.css" />
<link rel="stylesheet" type="text/css" href="/Scratch/css/idc.css" />
<link href='http://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://feeds.feedburner.com/yannespositocomen"/>
<link rel="alternate" lang="fr" xml:lang="fr" title="Fonctions d'ordre supérieur en zsh" type="text/html" hreflang="fr" href="/Scratch/fr/blog/Higher-order-function-in-zsh/" />
<link rel="alternate" lang="en" xml:lang="en" title="Higher order function in zsh" type="text/html" hreflang="en" href="/Scratch/en/blog/Higher-order-function-in-zsh/" />
<script type="text/javascript" src="/Scratch/js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="/Scratch/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/Scratch/js/index.js"></script>
<script type="text/javascript" src="/Scratch/js/highlight/highlight.pack.js"></script>
<script type="text/javascript" src="/Scratch/js/article.js"></script>
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<title>Higher order function in zsh</title>
</head>
<body lang="en" class="article">
<script type="text/javascript">// <![CDATA[
document.write('<div id="blackpage"><img src="/Scratch/img/loading.gif" alt="loading..."/></div>');
// ]]>
</script>
<div id="content">
<div id="choix">
<div class="return"><a href="#entete">&darr; Menu &darr;</a></div>
<div id="choixlang">
<a href="/Scratch/fr/blog/Higher-order-function-in-zsh/" onclick="setLanguage('fr')">en Français</a>
</div>
<div class="flush"></div>
</div>
<div id="titre">
<h1>
Higher order function in zsh
</h1>
</div>
<div class="flush"></div>
<div class="flush"></div>
<div id="afterheader">
<div class="corps">
<p><img alt="Title image" src="/Scratch/img/blog/Higher-order-function-in-zsh/main.jpg" /></p>
<div class="intro">
<p><span class="sc"><abbr title="Too long; didn't read">tl;dr</abbr>: </span> some simple implementation of higher order function for zsh.</p>
</div>
<p>Why is it important to have these functions?
Simply because, the more I programmed with zsh the more I tended to work using functional programming style.</p>
<p>The minimal to have better code are the functions <code>map</code>, <code>filter</code> and <code>fold</code>.</p>
<p>Lets compare.
First a program which convert all gif to png in many different directories of different projects.</p>
<p>Before ⇒</p>
<pre><code class="zsh"># for each directory in projects dir
for toProject in /path/to/projects/*(/N); do
# toProject is /path/to/projects/foo
# project become foo (:t for tail)
project=${toProject:t}
for toResource in $toProject/resources/*.gif(.N); do
convert $toResource ${toResource:r}.png &amp;&amp; \
\rm -f $toResource
done
done
</code></pre>
<ul>
<li>The <code>(/N)</code> means to select only directory and not to crash if there isnt any.</li>
<li>The <code>(.N)</code> means to select only files and not to crash if there isnt any.</li>
<li>The <code>:t</code> means tail; if <code>toto=/path/to/file.ext</code> then <code>${toto:t}=file.ext</code>.</li>
</ul>
<p>After ⇒</p>
<pre><code class="zsh">gif_to_png() { convert $1 ${1:r}.png &amp;&amp; \rm -f $1 }
handle_resources() { map gif_to_png $1/resources/*.gif(.N) }
map handle_resources /path/to/projects/*(/N)
</code></pre>
<p>No more bloc!
It might be a little bit harder to read if youre not used to functional programming notation.
But it is more concise and robusts.</p>
<p>Another example with some tests.</p>
<p>Find all files in project not containing an <code>s</code> which their name contains their project name:</p>
<p>Before ⇒</p>
<pre><code class="zsh">for toProject in Projects/*; do
project=$toProject:t
if print -- project | grep -v s &gt;/dev/null
then
print $project
for toResource in $toProject/*(.N); do
if print -- ${toResource:t} | grep $project &gt;/dev/null; then
print -- "X $toResource"
fi
done
fi
done
</code></pre>
<p>After ⇒</p>
<pre><code class="zsh">contain_no_s() { print $1 | grep -v s }
function verify_file_name {
local project=$1:t
contains_project_name() { print $1:t | grep $project }
map "print -- X" $(filter contains_project_name $1/*(.N))
}
map verify_file_name $( filter contain_no_s Projects/* )
</code></pre>
<p>Also, the first verstion is a bit easier to read.
But the second one is clearly far superior in architecture.
I dont want to argue why here.
Just believe me that the functional programming approach is superior.</p>
<p>Actually I lack the lambda operator.
If someone has an idea on how to create anonymous functions, just tell me, thanks.</p>
<p>Here is the source code:</p>
<div class="codefile"><a href="/Scratch/en/blog/Higher-order-function-in-zsh/code/functional.sh">&#x27A5; functional.sh</a></div>
<pre><code class="zsh">#!/usr/bin/env zsh
# Provide higer-order functions
# usage:
#
# $ foo(){print "x: $1"}
# $ map foo a b c d
# x: a
# x: b
# x: c
# x: d
function map {
local func_name=$1
shift
for elem in $@; print -- $(eval $func_name $elem)
}
# $ bar() { print $(($1 + $2)) }
# $ fold bar 0 1 2 3 4 5
# 15
# -- but also
# $ fold bar 0 $( seq 1 100 )
function fold {
if (($#&lt;2)) {
print -- "ERROR fold use at least 2 arguments" &gt;&amp;2
return 1
}
if (($#&lt;3)) {
print -- $2
return 0
} else {
local acc
local right
local func_name=$1
local init_value=$2
local first_value=$3
shift 3
right=$( fold $func_name $init_value $@ )
acc=$( eval "$func_name $first_value $right" )
print -- $acc
return 0
}
}
# usage:
#
# $ baz() { print $1 | grep baz }
# $ filter baz titi bazaar biz
# bazaar
function filter {
local predicate=$1
local result
typeset -a result
shift
for elem in $@; do
if eval $predicate $elem &gt;/dev/null; then
result=( $result $elem )
fi
done
print $result
}
</code></pre>
</div>
<div id="social">
<div class="left"> <a href="https://twitter.com/share" class="twitter-share-button" data-via="yogsototh">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
<div class="left"> <div class="g-plusone" data-size="medium" data-annotation="inline" data-width="106"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</div>
<div class="flush"></div>
</div>
<div id="choixrss">
<a id="rss" href="http://feeds.feedburner.com/yannespositocomen">
Subscribe
</a>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#comment').hide();
$('#clickcomment').click(showComments);
});
function showComments() {
$('#comment').show();
$('#clickcomment').fadeOut();
}
document.write('<div id="clickcomment">Comments &amp; Share</div>');
</script>
<div class="flush"></div>
<div class="corps" id="comment">
<h2 class="first">comments</h2>
<noscript>
You must enable javascript to comment.
</noscript>
<script type="text/javascript">
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
var idcomments_post_id = '/Scratch/en/blog/Higher-order-function-in-zsh/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/en/blog/Higher-order-function-in-zsh/';
</script>
<span id="IDCommentsPostTitle" style="display:none"></span>
<script type='text/javascript' src='/Scratch/js/genericCommentWrapperV2.js'></script>
</div>
<div id="entete" class="corps_spaced">
<div id="liens">
<ul><li><a href="/Scratch/en/">Home</a></li>
<li><a href="/Scratch/en/blog/">Blog</a></li>
<li><a href="/Scratch/en/softwares/">Softwares</a></li>
<li><a href="/Scratch/en/about/">About</a></li></ul>
</div>
<div class="flush"></div>
<hr/>
<div id="next_before_articles">
<div id="previous_articles">
previous entries
<div class="previous_article">
<a href="/Scratch/en/blog/programming-language-experience/"><span class="nicer">«</span>&nbsp;Programming Language Experience</a>
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/Learn-Vim-Progressively/"><span class="nicer">«</span>&nbsp;Learn Vim Progressively</a>
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/A-more-convenient-diff/"><span class="nicer">«</span>&nbsp;A more convenient diff</a>
</div>
</div>
<div id="next_articles">
next entries
<div class="next_article">
<a href="/Scratch/en/blog/Yesod-excellent-ideas/">Yesod excellent ideas&nbsp;<span class="nicer">»</span></a>
</div>
<div class="next_article">
<a href="/Scratch/en/blog/SVG-and-m4-fractals/">Increase the power of deficient languages.&nbsp;<span class="nicer">»</span></a>
</div>
<div class="next_article">
<a href="/Scratch/en/blog/Yesod-tutorial-for-newbies/">Haskell web programming&nbsp;<span class="nicer">»</span></a>
</div>
</div>
<div class="flush"></div>
</div>
</div>
<div id="bottom">
<div>
<a href="https://twitter.com/yogsototh">Follow @yogsototh</a>
</div>
<div>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Copyright ©, Yann Esposito</a>
</div>
<div id="lastmod">
Created: 09/28/2011
Modified: 04/26/2012
</div>
<div>
Entirely done with
<a href="http://www.vim.org">Vim</a>
and
<a href="http://nanoc.stoneship.org">nanoc</a>
</div>
</div>
<div class="clear"></div>
</div>
</body>
</html>