r/tinycode • u/soveran • Mar 14 '18
Map is hopefully an improvement upon the ergonomics of tools like xargs, find -exec and shell for-loops
https://github.com/soveran/map#map
35
Upvotes
r/tinycode • u/soveran • Mar 14 '18
View all comments
Show parent comment
6
u/soveran Mar 15 '18
Yes! And yesterday I was thinking I should add a comment about these alternatives to the README.
Both
xargsandfindhave a broader scope, thus their implementation is not trivial as they have to parse the input, recognize placeholders, etc. Yet both did not cover a particular use case I was confronted with.The use case that prompted me to try
mapis this: I wanted to iterate over some files and run two commands on each. Here's how you can do it with the different tools:With
map:With
xargs:With
awk:With
find:With a
bashfor-loop:With a
cshfor-loop:One detail to note is that
findoperates on file hierarchies, unlikemap,xargsand for-loops that can operate on lines.As I mentioned, the idea is for
mapto improve on the ergonomics of existing tools. It's not only that you have to type less withmap, but also the mental model needed to operate it is simpler. In that category, I thinkfindis the hardest one to remember. As with anything in life, familiarity helps and if you use a tool in a certain way over and over it will seem simple to operate, but we can still analyze the conceptual models and determine how much information is needed in each case.One final note: I posted this to /r/tinycode because map.c has 16 lines of code. For comparison, here's the source code of GNU xargs. No doubt
xargswill offer a lot more features, but so far withmapI've completely stopped usingxargsand for-loops. Another way to think aboutmapvsxargs: ifmaphad been specified in POSIX andxargswere just released, I'm not sure I would install it unlessmapproved to be unfit for a given use case.