|
|
|
|
|
by thibran
2472 days ago
|
|
Haskell is very terse, but it doesn't mean that this is true for every FP language. Code below is in Gerbil Scheme and should be readable even for OOP programmers. (def (alignCenter strings)
(def n (apply max (map string-length strings)))
(def (align s)
(let* ((count (round (/ (- n (string-length s)) 2)))
(whitespace (make-string count #\space)))
(string-append whitespace s)))
(map align strings))
Here also the same code in JS, which is almost identical. function alignCenter (strings) {
const n = Math.max(...strings.map(s => s.length))
const align = s => {
const count = (n - s.length) / 2
const whitespace = ' '.repeat(count)
return whitespace + s
}
return strings.map(align)
}
|
|