Created
April 29, 2015 18:36
-
-
Save randomphrase/cacf91a608b83324f4bd to your computer and use it in GitHub Desktop.
Example how bash function input/output can be used to compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Creates an XML element from the first parameter. Remaining parameters and all stdin are copied to stdout. | |
function elem() { | |
local name=$1 | |
echo -n "<$name>" | |
shift | |
echo -n $* | |
[[ ! -t 0 ]] && cat | |
echo "</$name>" | |
} | |
function title() { | |
elem "title" $* | |
} | |
function author() { | |
elem "author" $* | |
} | |
function book() { | |
elem "book" $* | |
} | |
function library() { | |
elem "library" $* | |
} | |
{ | |
{ | |
title "Moby Dick" | |
author "Melville" | |
} | book | |
{ | |
title "Das Capital" | |
author "Marx" | |
} | book | |
{ | |
title "The Cat In The Hat" | |
author "Suess" | |
} | book | |
} | library |
Still very readable - I like it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess the Python version would look like this:
Simpler but less DSLish, I guess.