# Swift's closure
#   https://d...content-available-to-author-only...e.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html


class String
    def self.unfoldl(b, &block)
        unless (pair = yield b).nil?
            a, b1 = pair

            self.unfoldl(b1, &block) + a
        else
            ''
        end
    end
end


DigitNames = {
    0 => "Zero", 1 => "One", 2 => "Two",   3 => "Three", 4 => "Four",
    5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine"
}
Numbers = [16, 58, 510]


strings = Numbers.map { |number|
    String.unfoldl(number) { |num|
        if num <= 0
            nil
        else
            [DigitNames[num % 10], num / 10]
        end
    }
}


for s in strings
    puts s
end