Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 1.8 KB

0181-trivial-left-pad.org

File metadata and controls

77 lines (52 loc) · 1.8 KB

trivial-left-pad

Did you hear the story of how one developer broke thousand JavaScript libraries by removing 11 lines of code from NPM?

Now we can to repeat this feat because we have our own left-pad for Common Lisp! :)

This library brings only one function, which adds some spaces, to make a string of given length:

POFTHEDAY> (trivial-left-pad:left-pad "Foo" 16)
"             Foo"

POFTHEDAY> (trivial-left-pad:left-pad "Bar" 16)
"             Bar"

POFTHEDAY> (trivial-left-pad:left-pad "Blah" 16)
"            Blah"

POFTHEDAY> (trivial-left-pad:left-pad "Minor" 16)
"           Minor"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16)
"    Hello world!"

You also can specify a custom padding as a character or a string:

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 ".")
"....Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 #\.)
"....Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 ".!")
".!.!Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 "->")
"->->Hello world!"

Of cause, this library is useful only if you need padding from more than one character. In other cases it is much easier to use standard format function:

POFTHEDAY> (format nil "~16@A" "Hello world!")
"    Hello world!"

POFTHEDAY> (format nil "~16,,,'_@A" "Hello world!")
"____Hello world!"

POFTHEDAY> (format nil "~16,,,'+@A" "Hello world!")
"++++Hello world!"

;; If you want to pass padding in runtime:

POFTHEDAY> (format nil "~v,,,v@A" 16 #\+ "Hello world!")
"++++Hello world!"