Visualize Church numerals
Retina, 74 67 63 bytes
Byte count assumes ISO 8859-1 encoding.
.
|
^
$.'$*----¶
\z
¶$` |
+`(.+\|) .+$
$&¶$1----
$
¶ |
¶
¶
Input is in unary, using any character except linefeeds as the digit.
Try it online!
Explanation
.
|
We start by turning each unary digit into |
(note the trailing spaces). This gives us the second line of the output (plus two trailing spaces if the input is at least 1
).
^
$.'$*----¶
We match the beginning of the string in order to prepend the first line. This is done using some Retina-specific substitution features. $*
repeats the character on the right as many times as its left arugment. $.'
evaluates the number of characters to the right of the match. Since the match is only the beginning of the string this gives as many -
as there are characters in the string and ---
appends three more. The ¶
is an alias for a linefeed. So now we've got the first two lines.
\z
¶$` |
Now we append the next two lines. We do this by matching the end of the string and appending a linefeed, the entire string again and then |
to get the fourth line right.
+`(.+\|) .+$
$&¶$1----
Time for the applications. The leading +
makes Retina repeat this stage until the output stops changing (in this case because the regex no long matches). The regex matches the entire last line, provided it contains a |
follows by a space. We capture everything up to the |
(which will be the second to last) in group 1
. We write the line back with $&
, a linefeed, then group 1 (thereby dropping the last |
) and then ----
.
$
¶ |
This just adds the final line containing only a single |
.
¶
¶
Finally we need to get rid of the trailing spaces on the second line.
JavaScript (ES6), 112 bytes
f=
n=>`${d=`-`.repeat(n*4+3)}
${(b=` | `.repeat(n)).slice(0,-2)}
${d}
${b} |
${b.replace(/ \| /g,`$' |----
`)} |`
;
<input id=i type=number min=0 oninput=o.textContent=f(this.value)>
<pre id=o></pre>
Python, 201 bytes
from pylab import*
n=input()
p=array(list(' -|\n'))
a=zeros((n+5,n*4+4),int)
for k in range(n):l=4*k+5;a[:-k-1,l]=2;a[-k-2,l-3:l+1]=1
a[:,1]=2
a[1,-3]=0
a[0]=a[2]=1
a[:,-1]=3
print''.join(ravel(p[a]))