2011-09-27 15:11:40 +00:00
<?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" / >
2011-10-26 08:49:00 +00:00
< meta name = "keywords" content = "zsh, map, foldr, filter, functional, programming, higher order functions" >
2011-09-27 15:11:40 +00:00
< 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" / >
2012-04-02 21:43:39 +00:00
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/solarized.css" / >
2011-09-27 15:11:40 +00:00
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/idc.css" / >
2012-05-02 15:43:56 +00:00
< link href = 'http://fonts.googleapis.com/css?family=Inconsolata' rel = 'stylesheet' type = 'text/css' >
2011-09-27 15:11:40 +00:00
< link rel = "alternate" type = "application/rss+xml" title = "RSS" href = "http://feeds.feedburner.com/yannespositocomen" / >
2011-09-30 08:16:57 +00:00
< 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/" / >
2011-09-27 15:11:40 +00:00
< 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 >
2012-05-02 15:43:56 +00:00
< script type = "text/javascript" src = "/Scratch/js/highlight/highlight.pack.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/article.js" > < / script >
2011-09-27 15:11:40 +00:00
<!-- [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 >
2011-10-18 22:30:00 +00:00
< body lang = "en" class = "article" >
2011-09-27 15:11:40 +00:00
< script type = "text/javascript" > / / < ! [ C D A T A [
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" > ↓ Menu ↓ < / a > < / div >
< div id = "choixlang" >
< a href = "/Scratch/fr/blog/Higher-order-function-in-zsh/" onclick = "setLanguage('fr')" > en Français< / a >
< / div >
2011-09-28 15:45:28 +00:00
< div class = "flush" > < / div >
2011-09-27 15:11:40 +00:00
< / 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" >
2011-09-28 15:45:28 +00:00
< p > < img alt = "Title image" src = "/Scratch/img/blog/Higher-order-function-in-zsh/main.jpg" / > < / p >
2011-09-27 15:11:40 +00:00
< 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 >
2011-09-28 15:47:13 +00:00
< 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 >
2012-05-03 09:21:34 +00:00
< p > Let’ s compare.
2011-09-28 15:47:13 +00:00
First a program which convert all gif to png in many different directories of different projects.< / p >
< p > Before ⇒< / p >
2012-05-02 15:43:56 +00:00
< 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 & & \
\rm -f $toResource
done
done
< / code > < / pre >
2011-09-27 15:11:40 +00:00
2011-09-29 10:34:44 +00:00
< ul >
2012-05-03 09:21:34 +00:00
< li > The < code > (/N)< / code > means to select only directory and not to crash if there isn’ t any.< / li >
< li > The < code > (.N)< / code > means to select only files and not to crash if there isn’ t any.< / li >
2011-09-29 10:34:44 +00:00
< li > The < code > :t< / code > means tail; if < code > toto=/path/to/file.ext< / code > then < code > ${toto:t}=file.ext< / code > .< / li >
< / ul >
2011-09-28 15:47:13 +00:00
< p > After ⇒< / p >
2012-05-02 15:43:56 +00:00
< pre > < code class = "zsh" > gif_to_png() { convert $1 ${1:r}.png & & \rm -f $1 }
2011-09-28 15:47:13 +00:00
2012-05-02 15:43:56 +00:00
handle_resources() { map gif_to_png $1/resources/*.gif(.N) }
2011-09-28 15:47:13 +00:00
2011-09-27 15:11:40 +00:00
map handle_resources /path/to/projects/*(/N)
2012-05-02 15:43:56 +00:00
< / code > < / pre >
2011-09-27 15:11:40 +00:00
2011-09-28 15:47:13 +00:00
< p > No more bloc!
2012-05-03 09:21:34 +00:00
It might be a little bit harder to read if you’ re not used to functional programming notation.
2011-09-28 15:47:13 +00:00
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 >
2011-09-28 15:45:28 +00:00
< p > Before ⇒< / p >
2012-05-02 15:43:56 +00:00
< pre > < code class = "zsh" > for toProject in Projects/*; do
project=$toProject:t
if print -- project | grep -v s > /dev/null
then
print $project
for toResource in $toProject/*(.N); do
if print -- ${toResource:t} | grep $project > /dev/null; then
print -- "X $toResource"
fi
done
fi
done
< / code > < / pre >
2011-09-27 15:11:40 +00:00
2011-09-28 15:45:28 +00:00
< p > After ⇒< / p >
2011-09-27 15:11:40 +00:00
2012-05-02 15:43:56 +00:00
< pre > < code class = "zsh" > contain_no_s() { print $1 | grep -v s }
2011-09-28 15:45:28 +00:00
2011-09-27 15:11:40 +00:00
function verify_file_name {
2012-05-02 15:43:56 +00:00
local project=$1:t
contains_project_name() { print $1:t | grep $project }
map "print -- X" $(filter contains_project_name $1/*(.N))
2011-09-27 15:11:40 +00:00
}
2011-09-28 15:45:28 +00:00
2012-05-02 15:43:56 +00:00
map verify_file_name $( filter contain_no_s Projects/* )
< / code > < / pre >
2011-09-27 15:11:40 +00:00
< p > Also, the first verstion is a bit easier to read.
But the second one is clearly far superior in architecture.
2012-05-03 09:21:34 +00:00
I don’ t want to argue why here.
2011-09-28 15:47:13 +00:00
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 >
2011-09-27 15:11:40 +00:00
2012-05-02 15:43:56 +00:00
< div class = "codefile" > < a href = "/Scratch/en/blog/Higher-order-function-in-zsh/code/functional.sh" > ➥ functional.sh< / a > < / div >
2011-09-28 15:45:28 +00:00
2012-05-02 15:43:56 +00:00
< pre > < code class = "zsh" > #!/usr/bin/env zsh
2011-09-28 15:45:28 +00:00
2012-05-02 15:43:56 +00:00
# Provide higer-order functions
# usage:
#
# $ foo(){print "x: $1"}
# $ map foo a b c d
# x: a
# x: b
# x: c
# x: d
2011-09-28 15:45:28 +00:00
function map {
2012-05-02 15:43:56 +00:00
local func_name=$1
2011-09-28 15:45:28 +00:00
shift
2012-05-02 15:43:56 +00:00
for elem in $@; print -- $(eval $func_name $elem)
2011-09-28 15:45:28 +00:00
}
2012-05-02 15:43:56 +00:00
# $ bar() { print $(($1 + $2)) }
# $ fold bar 0 1 2 3 4 5
# 15
# -- but also
# $ fold bar 0 $( seq 1 100 )
2011-09-28 15:45:28 +00:00
function fold {
2012-05-02 15:43:56 +00:00
if (($#< 2)) {
print -- "ERROR fold use at least 2 arguments" > & 2
2011-09-28 15:45:28 +00:00
return 1
}
2012-05-02 15:43:56 +00:00
if (($#< 3)) {
print -- $2
2011-09-28 15:45:28 +00:00
return 0
2012-05-02 15:43:56 +00:00
} else {
2011-09-28 15:45:28 +00:00
local acc
local right
2012-05-02 15:43:56 +00:00
local func_name=$1
local init_value=$2
local first_value=$3
2011-09-28 15:45:28 +00:00
shift 3
2012-05-02 15:43:56 +00:00
right=$( fold $func_name $init_value $@ )
acc=$( eval "$func_name $first_value $right" )
print -- $acc
2011-09-28 15:45:28 +00:00
return 0
}
}
2012-05-02 15:43:56 +00:00
# usage:
#
# $ baz() { print $1 | grep baz }
# $ filter baz titi bazaar biz
# bazaar
2011-09-28 15:45:28 +00:00
function filter {
2012-05-02 15:43:56 +00:00
local predicate=$1
2011-09-28 15:45:28 +00:00
local result
typeset -a result
shift
2012-05-02 15:43:56 +00:00
for elem in $@; do
if eval $predicate $elem > /dev/null; then
result=( $result $elem )
fi
done
print $result
2011-09-28 15:45:28 +00:00
}
2012-05-02 15:43:56 +00:00
< / code > < / pre >
2011-09-28 15:45:28 +00:00
2011-09-27 15:11:40 +00:00
< / div >
2012-04-10 13:56:34 +00:00
< 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 >
2011-09-27 15:11:40 +00:00
< 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();
}
2012-04-10 13:56:34 +00:00
document.write('< div id = "clickcomment" > Comments & Share< / div > ');
2011-09-27 15:11:40 +00:00
< / script >
< div class = "flush" > < / div >
2012-04-10 13:56:34 +00:00
2011-09-27 15:11:40 +00:00
< 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
2011-09-28 15:47:13 +00:00
< div class = "previous_article" >
< a href = "/Scratch/en/blog/programming-language-experience/" > < span class = "nicer" > «< / span > Programming Language Experience< / a >
< / div >
2011-09-27 15:11:40 +00:00
< div class = "previous_article" >
< a href = "/Scratch/en/blog/Learn-Vim-Progressively/" > < span class = "nicer" > «< / span > Learn Vim Progressively< / a >
< / div >
< div class = "previous_article" >
2011-09-28 16:05:55 +00:00
< a href = "/Scratch/en/blog/A-more-convenient-diff/" > < span class = "nicer" > «< / span > A more convenient diff< / a >
2011-09-27 15:11:40 +00:00
< / div >
< / div >
< div id = "next_articles" >
next entries
2011-10-18 22:30:00 +00:00
< div class = "next_article" >
< a href = "/Scratch/en/blog/Yesod-excellent-ideas/" > Yesod excellent ideas < span class = "nicer" > »< / span > < / a >
< / div >
2011-09-27 15:11:40 +00:00
2011-11-16 12:08:26 +00:00
< div class = "next_article" >
2011-11-16 12:30:46 +00:00
< a href = "/Scratch/en/blog/SVG-and-m4-fractals/" > Increase the power of deficient languages. < span class = "nicer" > »< / span > < / a >
2011-11-16 12:08:26 +00:00
< / div >
2011-09-27 15:11:40 +00:00
2012-01-11 20:40:22 +00:00
< div class = "next_article" >
< a href = "/Scratch/en/blog/Yesod-tutorial-for-newbies/" > Haskell web programming < span class = "nicer" > »< / span > < / a >
< / div >
2011-09-27 15:11:40 +00:00
< / div >
< div class = "flush" > < / div >
< / div >
< / div >
< div id = "bottom" >
2012-04-02 21:43:39 +00:00
< div >
2012-04-10 13:56:34 +00:00
< a href = "https://twitter.com/yogsototh" > Follow @yogsototh< / a >
2012-04-02 21:43:39 +00:00
< / div >
2011-09-27 15:11:40 +00:00
< div >
< a rel = "license" href = "http://creativecommons.org/licenses/by-sa/3.0/" > Copyright ©, Yann Esposito< / a >
< / div >
< div id = "lastmod" >
2011-09-28 15:47:13 +00:00
Created: 09/28/2011
2012-05-02 15:43:56 +00:00
Modified: 04/26/2012
2011-09-27 15:11:40 +00:00
< / 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 >