Higher order function in zsh

Title image

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.

The minimal to have better code are the functions map, filter and fold.

Let’s compare. First a program which convert all gif to png in many different directories of different projects.

Before ⇒

  • The (/N) means to select only directory and not to crash if there isn’t any.
  • The (.N) means to select only files and not to crash if there isn’t any.
  • The :t means tail; if toto=/path/to/file.ext then ${toto:t}=file.ext.

After ⇒

No more bloc! It might be a little bit harder to read if you’re not used to functional programming notation. But it is more concise and robusts.

Another example with some tests.

Find all files in project not containing an s which their name contains their project name:

Before ⇒

After ⇒

Also, the first verstion is a bit easier to read. But the second one is clearly far superior in architecture. I don’t want to argue why here. Just believe me that the functional programming approach is superior.

You can find an updated version of the code (thanks to Arash Rouhani). An older version is here thought. Here is the (first version) source code:

Comments

comments powered by Disqus
Published on 2011-09-28
Follow @yogsototh
Yann Esposito©
Done with Vim spacemacs & nanoc Hakyll

ADA: DdzFFzCqrhtAvdkmATx5Fm8NPJViDy85ZBw13p4XcNzVzvQg8e3vWLXq23JQWFxPEXK6Kvhaxxe7oJt4VMYHxpA2vtCFiP8fziohN6Yp