fork download
  1. # Swift's closure
  2. # https://d...content-available-to-author-only...e.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
  3.  
  4.  
  5. DigitNames = {
  6. 0 => "Zero", 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",
  7. 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine"
  8. }
  9. Numbers = [16, 58, 510]
  10.  
  11.  
  12. strings = Numbers.map { |number|
  13. loop.inject([number, '']) { |(num, output), _|
  14. if num > 0
  15. [num / 10, DigitNames[num % 10] + output]
  16. else
  17. break output
  18. end
  19. }
  20. }
  21.  
  22.  
  23. for s in strings
  24. puts s
  25. end
Success #stdin #stdout 0.02s 7412KB
stdin
Standard input is empty
stdout
OneSix
FiveEight
FiveOneZero