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 powered by Disqus